Merge pull request #1078 from yprez/fix_1072

Fix empty serializer bug
This commit is contained in:
Tom Christie 2013-09-02 01:13:51 -07:00
commit d4f5fe99ba
2 changed files with 10 additions and 1 deletions

View File

@ -264,7 +264,7 @@ class PrimaryKeyRelatedField(RelatedField):
# RelatedObject (reverse relationship)
try:
pk = getattr(obj, self.source or field_name).pk
except ObjectDoesNotExist:
except (ObjectDoesNotExist, AttributeError):
return None
# Forward relationship

View File

@ -283,6 +283,15 @@ class PKForeignKeyTests(TestCase):
self.assertFalse(serializer.is_valid())
self.assertEqual(serializer.errors, {'target': ['This field is required.']})
def test_foreign_key_with_empty(self):
"""
Regression test for #1072
https://github.com/tomchristie/django-rest-framework/issues/1072
"""
serializer = NullableForeignKeySourceSerializer()
self.assertEqual(serializer.data['target'], None)
class PKNullableForeignKeyTests(TestCase):
def setUp(self):