mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 16:24:18 +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.
|
||||
for key, value in uniqueness_extra_kwargs.items():
|
||||
if key in extra_kwargs:
|
||||
extra_kwargs[key].update(value)
|
||||
else:
|
||||
extra_kwargs[key] = value
|
||||
value.update(extra_kwargs[key])
|
||||
extra_kwargs[key] = value
|
||||
|
||||
return extra_kwargs, hidden_fields
|
||||
|
||||
|
|
|
@ -976,3 +976,22 @@ class TestModelFieldValues(TestCase):
|
|||
source = OneToOneSourceTestModel(target=target)
|
||||
serializer = ExampleSerializer(source)
|
||||
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