toastdriven/django-tastypie — Issue #326: Related resources referenced via PK not handled correctly
Author: pennersr
Created: 2012-01-08 22:09:23+00:00
State: open

When creating a resource that has a foreign key relation to another resource one can pass either the URI of the related resource, or one should be able to pass the PK of that resource.

However, the latter approach seems to be wrongly implemented, see:

https://github.com/toastdriven/django-tastypie/blob/master/tastypie/fields.py#L589

The issue is that the JSON {"pk": 123} ends up as a Python dictionary for which hasattr(value, 'items') is True.

def build_related_resource(self, value, request=None, related_obj=None, related_name=None):
...
if isinstance(value, basestring):
# We got a URI. Load the object and assign it.
return self.resource_from_uri(self.fk_resource, value, **kwargs)
elif hasattr(value, 'items'):
# We've got a data dictionary.
# Since this leads to creation, this is the only one of these
# methods that might care about "parent" data.
return self.resource_from_data(self.fk_resource, value, **kwargs)
elif hasattr(value, 'pk'):
# We've got an object with a primary key.
return self.resource_from_pk(self.fk_resource, value, **kwargs)