mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-25 00:34:21 +03:00
extra_kwargs takes precedence over uniqueness kwargs (#4349)
This commit is contained in:
parent
5500b265bc
commit
a9a097496e
|
@ -1324,9 +1324,8 @@ class ModelSerializer(Serializer):
|
||||||
# Update `extra_kwargs` with any new options.
|
# Update `extra_kwargs` with any new options.
|
||||||
for key, value in uniqueness_extra_kwargs.items():
|
for key, value in uniqueness_extra_kwargs.items():
|
||||||
if key in extra_kwargs:
|
if key in extra_kwargs:
|
||||||
extra_kwargs[key].update(value)
|
value.update(extra_kwargs[key])
|
||||||
else:
|
extra_kwargs[key] = value
|
||||||
extra_kwargs[key] = value
|
|
||||||
|
|
||||||
return extra_kwargs, hidden_fields
|
return extra_kwargs, hidden_fields
|
||||||
|
|
||||||
|
|
|
@ -976,3 +976,22 @@ class TestModelFieldValues(TestCase):
|
||||||
source = OneToOneSourceTestModel(target=target)
|
source = OneToOneSourceTestModel(target=target)
|
||||||
serializer = ExampleSerializer(source)
|
serializer = ExampleSerializer(source)
|
||||||
self.assertEqual(serializer.data, {'target': 1})
|
self.assertEqual(serializer.data, {'target': 1})
|
||||||
|
|
||||||
|
|
||||||
|
class TestUniquenessOverride(TestCase):
|
||||||
|
def test_required_not_overwritten(self):
|
||||||
|
class TestModel(models.Model):
|
||||||
|
field_1 = models.IntegerField(null=True)
|
||||||
|
field_2 = models.IntegerField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
unique_together = (('field_1', 'field_2'),)
|
||||||
|
|
||||||
|
class TestSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = TestModel
|
||||||
|
extra_kwargs = {'field_1': {'required': False}}
|
||||||
|
|
||||||
|
fields = TestSerializer().fields
|
||||||
|
self.assertFalse(fields['field_1'].required)
|
||||||
|
self.assertTrue(fields['field_2'].required)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user