tastypie - 相关资源返回null

时间:2014-05-13 06:31:30

标签: python django tastypie

我有一个包含很少外键的表,我试图获取嵌套资源但是资源返回null,即进行以下调用时

http://localhost:8080/api/v1/testuns/?format=json

我回来了

{
meta: {
    limit: 20,
    next: null,
    offset: 0,
    previous: null,
    total_count: 1
},
objects: [
     {
       phone: {
           devices: null,
           modelnumber: null,
           phone_id: "2",
           resource_uri: "/api/v1/phone/2/"
       },
       resource_uri: "/api/v1/testuns/0/",
       result: "PASS",
       score: 90,
       test_run_id: "0"
     }
   ]
}

如您所见,我的设备和型号没有嵌套资源

我的api.py就是这样

class DevicesResource(ModelResource):
    class Meta:
        queryset = Devices.objects.all()
        authorization = Authorization()
        resource_name = 'devices'

class ModelnumberResource(ModelResource):
    class Meta:
        queryset = Modelnumber.objects.all()
        authorization = Authorization()
        resource_name = 'modelnumber'

class PhoneResource(ModelResource):
    devices = fields.ForeignKey(DevicesResource, 'devices', full=True, null=True)
    modelnumber = fields.ForeignKey(ModelnumberResource, 'modelnumber', full=True, null=True)

    class Meta:
        queryset = Phone.objects.all()
        authorization = Authorization()
        resource_name = 'phone'

class TestunsResource(ModelResource):
    phone = fields.ForeignKey(PhoneResource, 'phone', full=True, null=True)
    class Meta:
        queryset = Testuns.objects.all()
        authorization = Authorization()
        resource_name = 'testuns'

和我的models.py是

class Devices(models.Model):
    device_id = models.AutoField(primary_key=True)
    device_name = models.CharField(max_length=135, unique=True, blank=True)
    class Meta:
        db_table = u'devices'

class Modelnumber(models.Model):
    model_number_id = models.AutoField(primary_key=True)
    model_name = models.CharField(max_length=135, unique=True, blank=True)
    class Meta:
        db_table = u'modelnumber'

class Phone(models.Model):
    phone_id = models.AutoField(primary_key=True)
    model_number = models.ForeignKey(Modelnumber)
    device = models.ForeignKey(Devices)
    class Meta:
        db_table = u'phone'

class Testuns(models.Model):
    test_run_id = models.AutoField(primary_key=True)
    score = models.IntegerField()
    phone = models.ForeignKey(Phone)
    result = models.CharField(max_length=135)

为什么我的一些嵌套资源返回null?

更新: 添加mysql查询

mysql> select * from testuns;
+-------------+-------+----------+--------+
| test_run_id | score | phone_id | result |
+-------------+-------+----------+--------+
|           0 |    90 |        2 | PASS   |
+-------------+-------+----------+--------+
1 row in set (0.00 sec)

mysql> select * from phone;
+----------+-----------------+-----------+
| phone_id | model_number_id | device_id |
+----------+-----------------+-----------+
|        2 |               1 |         1 |
+----------+-----------------+-----------+
1 row in set (0.00 sec)

mysql>

1 个答案:

答案 0 :(得分:0)

无意中,您的评论指出了我的解决方案:

class PhoneResource(ModelResource):
    device = fields.ForeignKey(DevicesResource, 'device', full=True, null=True)
    model_number = fields.ForeignKey(ModelnumberResource, 'model_number', full=True, null=True)

    class Meta:
        queryset = Phone.objects.all()
        authorization = Authorization()
        resource_name = 'phone'

将资源字段命名为与模型中命名的完全相同。

我在自己的项目中测试了它,当资源中的字段名称不同时,在模型中返回null

由于null=Trueblank=True存在,该问题具有误导性。这使它正常工作,但不符合你的计划。我在文档中找不到explenation,但在docstrings中找到了一些东西:

The ``attribute`` argument should specify what field/callable points to the related data on the instance object. Required. docstring

编辑:

这里line of code你的字段不是外键,但它可以为null,这就是它没有引发任何异常的原因。