Merge pull request #722 from Bouke/patch-1

PrimaryKeyRelatedField with OneToOneField serializes wrong object's id
This commit is contained in:
Tom Christie 2013-03-10 13:34:02 -07:00
commit 99b18d7485
2 changed files with 3 additions and 4 deletions

View File

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

View File

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