我试图将外键添加到我的tastypie资源中,但django抛出了这个错误:
"error_message": "'BB' object is not iterable",
我创建了一个最小的工作示例:
models.py
class AA(models.Model):
n = models.IntegerField()
class BB(models.Model):
aa = models.ForeignKey(AA, related_name='xox')
t = models.CharField(max_length=2)
resources.py
from ..models import AA, BB
from tastypie.authorization import Authorization
from tastypie.fields import ForeignKey
class AResource(ModelResource):
class Meta:
queryset = AA.objects.all()
authorization = Authorization()
class BBResource(ModelResource):
aa = ForeignKey(AResource, 'aa', related_name="xox", full=False, blank=True, null=True)
class Meta:
queryset = BB.objects.all()
authorization = Authorization()
现在使用curl执行后期操作:
$ curl -XPOST --dump-header - --header 'Content-Type: application/json' localhost:8000/api/v1/a/ --data '{"n": 18}'
$ curl -XPOST --dump-header - --header 'Content-Type: application/json' localhost:8000/api/v1/bb/ --data '{"t": "di", "aa": "/api/v1/a/1/"}'
的回溯:
{
"error_message": "'BB' object is not iterable",
"traceback": "Traceback (most recent call last):\n\n File \"/home/mee/.venvs/env_pro/lib/python2.7/site-packages/tastypie/resources.py\", line 219, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"/home/mee/.venvs/env_pro/lib/python2.7/site-packages/tastypie/resources.py\", line 450, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File \"/home/mee/.venvs/env_pro/lib/python2.7/site-packages/tastypie/resources.py\", line 482, in dispatch\n response = method(request, **kwargs)\n\n File \"/home/mee/.venvs/env_pro/lib/python2.7/site-packages/tastypie/resources.py\", line 1384, in post_list\n updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))\n\n File \"/home/mee/.venvs/env_pro/lib/python2.7/site-packages/tastypie/resources.py\", line 2175, in obj_create\n return self.save(bundle)\n\n File \"/home/mee/.venvs/env_pro/lib/python2.7/site-packages/tastypie/resources.py\", line 2322, in save\n self.save_related(bundle)\n\n File \"/home/mee/.venvs/env_pro/lib/python2.7/site-packages/tastypie/resources.py\", line 2382, in save_related\n setattr(related_obj, field_object.related_name, bundle.obj)\n\n File \"/home/mee/.venvs/env_pro/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py\", line 481, in __set__\n manager.set(value)\n\n File \"/home/mee/.venvs/env_pro/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py\", line 639, in set\n objs = tuple(objs)\n\nTypeError: 'BB' object is not iterable\n"
}
套餐版本:
Django==1.9.7
django-tastypie==0.13.3
修改
通过查询bb对象,我可以看到正在存储关联
$ curl localhost:8000/api/v1/bb/
{
"meta": {
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total_count": 1
},
"objects": [
{
"aa": "/api/v1/a/1/",
"id": 1,
"resource_uri": "/api/v1/bb/1/",
"t": "di"
}
]
通过检查回溯,会在django代码中引发错误。
任何建议将不胜感激。提前谢谢。
答案 0 :(得分:0)
这是一个简单(和新手)的问题,让我感觉像是一个似曾相识。这里的问题是要记住何时使用related_name
,在这种情况下,该参数不是必需的。
Link to the docs解释了related_name
param。