mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 17:47:04 +03:00
Add test confirming that required=False is valid on a relational field (#4195)
This commit is contained in:
parent
2e7fae7698
commit
1633a0a2b1
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user