【发布时间】: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.pk或object.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)