mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-11 12:17:24 +03:00
Merge pull request #722 from Bouke/patch-1
PrimaryKeyRelatedField with OneToOneField serializes wrong object's id
This commit is contained in:
commit
99b18d7485
|
@ -235,7 +235,6 @@ class PrimaryKeyRelatedField(RelatedField):
|
||||||
pk = getattr(obj, self.source or field_name).pk
|
pk = getattr(obj, self.source or field_name).pk
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
return None
|
return None
|
||||||
return self.to_native(obj.pk)
|
|
||||||
|
|
||||||
# Forward relationship
|
# Forward relationship
|
||||||
return self.to_native(pk)
|
return self.to_native(pk)
|
||||||
|
|
|
@ -407,14 +407,14 @@ class PKNullableOneToOneTests(TestCase):
|
||||||
target.save()
|
target.save()
|
||||||
new_target = OneToOneTarget(name='target-2')
|
new_target = OneToOneTarget(name='target-2')
|
||||||
new_target.save()
|
new_target.save()
|
||||||
source = NullableOneToOneSource(name='source-1', target=target)
|
source = NullableOneToOneSource(name='source-1', target=new_target)
|
||||||
source.save()
|
source.save()
|
||||||
|
|
||||||
def test_reverse_foreign_key_retrieve_with_null(self):
|
def test_reverse_foreign_key_retrieve_with_null(self):
|
||||||
queryset = OneToOneTarget.objects.all()
|
queryset = OneToOneTarget.objects.all()
|
||||||
serializer = NullableOneToOneTargetSerializer(queryset, many=True)
|
serializer = NullableOneToOneTargetSerializer(queryset, many=True)
|
||||||
expected = [
|
expected = [
|
||||||
{'id': 1, 'name': 'target-1', 'nullable_source': 1},
|
{'id': 1, 'name': 'target-1', 'nullable_source': None},
|
||||||
{'id': 2, 'name': 'target-2', 'nullable_source': None},
|
{'id': 2, 'name': 'target-2', 'nullable_source': 1},
|
||||||
]
|
]
|
||||||
self.assertEqual(serializer.data, expected)
|
self.assertEqual(serializer.data, expected)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user