tastypie 保存外键字段抛出异常
tastypie saving foreignkey field throws exception
我正在尝试将外键添加到我的 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 执行 post 操作:
$ 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 代码中引发了错误。
如有任何建议,我们将不胜感激。提前致谢。
这是一个简单的(新手)问题,让我觉得自己似曾相识。这里的问题是记住什么时候使用 related_name
,在这种情况下不需要参数。
Link to the docs 解释了 related_name
参数。
我正在尝试将外键添加到我的 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 执行 post 操作:
$ 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 代码中引发了错误。
如有任何建议,我们将不胜感激。提前致谢。
这是一个简单的(新手)问题,让我觉得自己似曾相识。这里的问题是记住什么时候使用 related_name
,在这种情况下不需要参数。
Link to the docs 解释了 related_name
参数。