【问题标题】:tastypie - related resources returning null美味派 - 相关资源返回 null
【发布时间】:2014-05-13 06:31:29
【问题描述】:

我有一个表,其中的外键很少,我正在尝试获取嵌套资源,但是资源返回 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>

【问题讨论】:

  • 确保该值确实不是0(github.com/toastdriven/django-tastypie/issues/238)。我会尽量避免使用自定义主键。 Tastypie 是特定于 ORM 的。我当然认为摆脱自定义主键会解决问题。如果可能,请使用默认值 object.pkobject.id
  • @BartoszDabrowski 我的 testuns 的 id 为 0,我将更正但 phone_id 不是 0。当您的意思是摆脱自定义主键时,您的意思是例如将 model_number_id 更改为 modelnumber_id 并将 device_id 更改为 devices_id ?我不明白您所说的“默认”object.pk 是什么意思。有你所说的链接吗?
  • Django 为每个模型创建 primary_key,除非你自己做。 Django 创建的主要字段称为id,代理字段称为“pk”。我从未尝试在模型中定义自己的primery_key=True 字段。但我非常了解 Tastypie,并认为使用 primey_key=True 删除字段可以解决问题,因为我从未见过类似的情况。但我不确定您是否出于某种原因需要保留它们。但这些只是我的想法,这就是我评论而不是回答的原因。
  • 好的,我刚刚实现了 mysql db,并使用了“./manage.py inpsectdb”,它为我自动创建了模型。我唯一改变的唯一想法是 models.IntegerField(primary_key=True) 到 models.AutoField(primary_key=True)

标签: python django tastypie


【解决方案1】:

不经意间,您的评论指出了我对这个解决方案的看法:

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 的存在,该问题具有误导性。这使它正常工作,但不符合您的计划。我在文档中找不到解释,但在文档字符串中找到了一些东西:

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

编辑:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 2016-05-08
    • 2012-04-03
    相关资源
    最近更新 更多