-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Description
Checklist
- I have verified that that issue exists against the
master
branch of Django REST framework. - I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
It seems related to PKOnlyObject optimization may break HyperlinkedRelatedField #4653, but that one was closed with no action taken. - This is not a usage question. (Those should be directed to the discussion group instead.)
- This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
- I have reduced the issue to the simplest possible case.
- I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)
Steps to reproduce
Define the following models and serializer:
class Foo(models.Model):
pass
class Bar(models.Model):
@property
def foo(self):
return Foo.objects.first()
class BarSerializer(serializers.ModelSerializer):
foo = serializers.PrimaryKeyRelatedField(
queryset=Foo.objects.all()
)
class Meta:
model = Bar
fields = ('id', 'foo')
Create Bar
instance and attempt to render it:
>>> Foo.objects.create()
<Foo: Foo object (2)>
>>> bar = Bar.objects.create()
>>> bar.foo
<Foo: Foo object (1)>
>>> serializer = BarSerializer(bar)
>>> serializer.data
{'foo': <Foo: Foo object (1)>, 'id': 2}
>>> JSONRenderer().render(serializer.data)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/clara/.virtualenvs/django_example_project/lib/python3.5/site-packages/rest_framework/renderers.py", line 107, in render
allow_nan=not self.strict, separators=separators
File "/home/clara/.virtualenvs/django_example_project/lib/python3.5/site-packages/rest_framework/utils/json.py", line 28, in dumps
return json.dumps(*args, **kwargs)
File "/usr/lib/python3.5/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
return _iterencode(o, 0)
File "/home/clara/.virtualenvs/django_example_project/lib/python3.5/site-packages/rest_framework/utils/encoders.py", line 68, in default
return super(JSONEncoder, self).default(obj)
File "/usr/lib/python3.5/json/encoder.py", line 179, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <Foo: Foo object (1)> is not JSON serializable
Expected behavior
The serialization and deserialization should behave the same way they would if the field was a real FK in the model, that is, extract the PK from the related object in the serialization, and obtain the related object from the PK in the deserialization.
serializer.data
should be
{
'id': 2,
'foo': 1
}
Which can be rendered to JSON.
Actual behavior
serializer.data
is
{
'id': 2,
'foo': <Foo: Foo object (1)>
}
Which causes the error in the JSON rendering.
If I understand it correctly, this situation should fall into the edge case handled in RelatedField.get_attribute()
:
value = instance.serializable_value(self.source_attrs[-1])
if is_simple_callable(value):
# Handle edge case where the relationship `source` argument
# points to a `get_relationship()` method on the model
value = value().pk
but because it is a @property
method, it does not return a callable and causes the method to assume it is a PK.
I tried changing the source to foo.pk
, that corrects the serialization behaviour, but messes up the deserialization behaviour (which was correct).