Add test confirming that required=False is valid on a relational field (#4195)

This commit is contained in:
Tom Christie 2016-06-13 16:52:45 +01:00 committed by GitHub
parent 2e7fae7698
commit 1633a0a2b1

View File

@ -340,6 +340,18 @@ class PKForeignKeyTests(TestCase):
serializer = NullableForeignKeySourceSerializer()
self.assertEqual(serializer.data['target'], None)
def test_foreign_key_not_required(self):
"""
Let's say we wanted to fill the non-nullable model field inside
Model.save(), we would make it empty and not required.
"""
class ModelSerializer(ForeignKeySourceSerializer):
class Meta(ForeignKeySourceSerializer.Meta):
extra_kwargs = {'target': {'required': False}}
serializer = ModelSerializer(data={'name': 'test'})
serializer.is_valid(raise_exception=True)
self.assertNotIn('target', serializer.validated_data)
class PKNullableForeignKeyTests(TestCase):
def setUp(self):