slackmart slackmart - 28 days ago 10x
Python Question

tastypie saving foreignkey field throws exception

I'm trying to add the foreign key to my tastypie resources, but django throws out this error:

"error_message": "'BB' object is not iterable",

I've created a minimal working example:

class AA(models.Model):
n = models.IntegerField()

class BB(models.Model):
aa = models.ForeignKey(AA, related_name='xox')
t = models.CharField(max_length=2)

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()

Now using curl to perform the post action:

$ 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/\", line 219, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"/home/mee/.venvs/env_pro/lib/python2.7/site-packages/tastypie/\", 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/\", line 482, in dispatch\n response = method(request, **kwargs)\n\n File \"/home/mee/.venvs/env_pro/lib/python2.7/site-packages/tastypie/\", 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/\", line 2175, in obj_create\n return\n\n File \"/home/mee/.venvs/env_pro/lib/python2.7/site-packages/tastypie/\", line 2322, in save\n self.save_related(bundle)\n\n File \"/home/mee/.venvs/env_pro/lib/python2.7/site-packages/tastypie/\", 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/\", 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/\", line 639, in set\n objs = tuple(objs)\n\nTypeError: 'BB' object is not iterable\n"

Package versions:



By querying the bb objects I can see the association is being stored

$ 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"

And by inspecting the traceback, the error is raised in the django code.

Any suggestion will be appreciated. Thanks in advance.


it was a simple (and newbie) question that make me feel like a déjà vu. The issue here is to remember when to use related_name, in this case that param isn't necessary.

Link to the docs where the related_name param is explained.