mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-07 22:04:48 +03:00
Exclude read_only=True fields from unique_together validation
This commit is contained in:
parent
9bffd35432
commit
3812f63fcd
|
@ -1243,6 +1243,11 @@ class ModelSerializer(Serializer):
|
|||
|
||||
read_only_fields = getattr(self.Meta, 'read_only_fields', None)
|
||||
if read_only_fields is not None:
|
||||
if not isinstance(read_only_fields, (list, tuple)):
|
||||
raise TypeError(
|
||||
'The `read_only_fields` option must be a list or tuple. '
|
||||
'Got %s.' % type(read_only_fields).__name__
|
||||
)
|
||||
for field_name in read_only_fields:
|
||||
kwargs = extra_kwargs.get(field_name, {})
|
||||
kwargs['read_only'] = True
|
||||
|
@ -1390,6 +1395,7 @@ class ModelSerializer(Serializer):
|
|||
field_names = {
|
||||
field.source for field in self.fields.values()
|
||||
if (field.source != '*') and ('.' not in field.source)
|
||||
and not field.read_only
|
||||
}
|
||||
|
||||
# Note that we make sure to check `unique_together` both on the
|
||||
|
|
|
@ -521,8 +521,6 @@ class TestRelationalFieldMappings(TestCase):
|
|||
one_to_one = NestedSerializer(read_only=True):
|
||||
url = HyperlinkedIdentityField(view_name='onetoonetargetmodel-detail')
|
||||
name = CharField(max_length=100)
|
||||
class Meta:
|
||||
validators = [<UniqueTogetherValidator(queryset=UniqueTogetherModel.objects.all(), fields=('foreign_key', 'one_to_one'))>]
|
||||
""")
|
||||
if six.PY2:
|
||||
# This case is also too awkward to resolve fully across both py2
|
||||
|
|
|
@ -239,6 +239,26 @@ class TestUniquenessTogetherValidation(TestCase):
|
|||
""")
|
||||
assert repr(serializer) == expected
|
||||
|
||||
def test_ignore_read_only_fields(self):
|
||||
"""
|
||||
When serializer fields are read only, then uniqueness
|
||||
validators should not be added for that field.
|
||||
"""
|
||||
class ReadOnlyFieldSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = UniquenessTogetherModel
|
||||
fields = ('id', 'race_name', 'position')
|
||||
read_only_fields = ('race_name',)
|
||||
|
||||
serializer = ReadOnlyFieldSerializer()
|
||||
expected = dedent("""
|
||||
ReadOnlyFieldSerializer():
|
||||
id = IntegerField(label='ID', read_only=True)
|
||||
race_name = CharField(read_only=True)
|
||||
position = IntegerField(required=True)
|
||||
""")
|
||||
assert repr(serializer) == expected
|
||||
|
||||
def test_ignore_validation_for_null_fields(self):
|
||||
# None values that are on fields which are part of the uniqueness
|
||||
# constraint cause the instance to ignore uniqueness validation.
|
||||
|
|
Loading…
Reference in New Issue
Block a user