tastypie saving foreignkey field throws exception
<p>I'm trying to add the foreign key to my tastypie resources, but django throws out this error:</p>

<pre><code>"error_message": "'BB' object is not iterable",
</code></pre>

<p>I've created a minimal working example:</p>

<p><strong>models.py</strong></p>

<pre><code>class AA(models.Model):
    n = models.IntegerField()


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

<p><strong>resources.py</strong></p>

<pre><code>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()
</code></pre>

<p>Now using curl to perform the post action:</p>

<pre><code>$ 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/"}'
</code></pre>

<p>
    <strong>Traceback:</strong></p>

<pre><code> {
    "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"
}
</code></pre>

<p><strong>Package versions:</strong></p>

<pre><code>Django==1.9.7
django-tastypie==0.13.3
</code></pre>

<p><strong>EDIT</strong></p>

<p>By querying the bb objects I can see the association is being stored</p>

<pre><code>$  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"
    }
]
</code></pre>

<p>And by inspecting the traceback, the error is raised in the django code.</p>

<p>Any suggestion will be appreciated. Thanks in advance.</p>

<p>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 <code>related_name</code>, in this case that param isn't necessary.</p>

<p><a href="http://django-tastypie.readthedocs.io/en/latest/fields.html#related-name" rel="nofollow">Link to the docs</a> where the <code>related_name</code> param is explained.</p>

python django tastypie restful-architecture