Do not add uniquness_extra_kwargs when validators are explicitly declared.

This commit is contained in:
Tom Christie 2016-06-13 12:38:49 +01:00
parent 85c7a15c62
commit 3e45e1e8b0
2 changed files with 7 additions and 4 deletions

View File

@ -1263,6 +1263,9 @@ class ModelSerializer(Serializer):
('dict of updated extra kwargs', 'mapping of hidden fields') ('dict of updated extra kwargs', 'mapping of hidden fields')
""" """
if getattr(self.Meta, 'validators', None) is not None:
return (extra_kwargs, {})
model = getattr(self.Meta, 'model') model = getattr(self.Meta, 'model')
model_fields = self._get_model_fields( model_fields = self._get_model_fields(
field_names, declared_fields, extra_kwargs field_names, declared_fields, extra_kwargs
@ -1313,7 +1316,7 @@ class ModelSerializer(Serializer):
else: else:
uniqueness_extra_kwargs[unique_constraint_name] = {'default': default} uniqueness_extra_kwargs[unique_constraint_name] = {'default': default}
elif default is not empty: elif default is not empty:
# The corresponding field is not present in the, # The corresponding field is not present in the
# serializer. We have a default to use for it, so # serializer. We have a default to use for it, so
# add in a hidden field that populates it. # add in a hidden field that populates it.
hidden_fields[unique_constraint_name] = HiddenField(default=default) hidden_fields[unique_constraint_name] = HiddenField(default=default)

View File

@ -261,7 +261,7 @@ class TestUniquenessTogetherValidation(TestCase):
def test_allow_explict_override(self): def test_allow_explict_override(self):
""" """
Ensure unique_together can be explicitly removed Ensure validators can be explicitly removed..
""" """
class NoValidatorsSerializer(serializers.ModelSerializer): class NoValidatorsSerializer(serializers.ModelSerializer):
class Meta: class Meta:
@ -273,8 +273,8 @@ class TestUniquenessTogetherValidation(TestCase):
expected = dedent(""" expected = dedent("""
NoValidatorsSerializer(): NoValidatorsSerializer():
id = IntegerField(label='ID', read_only=True) id = IntegerField(label='ID', read_only=True)
race_name = CharField(max_length=100, required=True) race_name = CharField(max_length=100)
position = IntegerField(required=True) position = IntegerField()
""") """)
assert repr(serializer) == expected assert repr(serializer) == expected