mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-02 11:30:12 +03:00
Merge bf79e781f9
into 7d64b7016d
This commit is contained in:
commit
517ad3d55d
|
@ -534,8 +534,21 @@ class Field(object):
|
|||
if hasattr(validator, 'set_context'):
|
||||
validator.set_context(self)
|
||||
|
||||
# Add missing fields to `value` for partial update if these fields exists in validator, but missing in
|
||||
# `value`
|
||||
value_ext = copy.copy(value)
|
||||
if getattr(self.root, 'partial', False):
|
||||
if hasattr(validator, 'date_field') and validator.date_field not in value_ext:
|
||||
value_ext[validator.date_field] = \
|
||||
self.fields.fields.get(validator.date_field).to_internal_value(
|
||||
getattr(self.instance, validator.date_field))
|
||||
if hasattr(validator, 'field') and validator.field not in value_ext:
|
||||
value_ext[validator.field] = \
|
||||
self.fields.fields.get(validator.field).to_internal_value(
|
||||
getattr(self.instance, validator.field))
|
||||
|
||||
try:
|
||||
validator(value)
|
||||
validator(value_ext)
|
||||
except ValidationError as exc:
|
||||
# If the validation error contains a mapping of fields to
|
||||
# errors then simply raise it immediately rather than
|
||||
|
|
|
@ -372,6 +372,7 @@ class TestUniquenessTogetherValidation(TestCase):
|
|||
class UniqueForDateModel(models.Model):
|
||||
slug = models.CharField(max_length=100, unique_for_date='published')
|
||||
published = models.DateField()
|
||||
body = models.CharField(max_length=100, null=True)
|
||||
|
||||
|
||||
class UniqueForDateSerializer(serializers.ModelSerializer):
|
||||
|
@ -384,7 +385,8 @@ class TestUniquenessForDateValidation(TestCase):
|
|||
def setUp(self):
|
||||
self.instance = UniqueForDateModel.objects.create(
|
||||
slug='existing',
|
||||
published='2000-01-01'
|
||||
published='2000-01-01',
|
||||
body='some body'
|
||||
)
|
||||
|
||||
def test_repr(self):
|
||||
|
@ -394,6 +396,7 @@ class TestUniquenessForDateValidation(TestCase):
|
|||
id = IntegerField(label='ID', read_only=True)
|
||||
slug = CharField(max_length=100)
|
||||
published = DateField(required=True)
|
||||
body = CharField(allow_null=True, max_length=100, required=False)
|
||||
class Meta:
|
||||
validators = [<UniqueForDateValidator(queryset=UniqueForDateModel.objects.all(), field='slug', date_field='published')>]
|
||||
""")
|
||||
|
@ -435,6 +438,28 @@ class TestUniquenessForDateValidation(TestCase):
|
|||
'published': datetime.date(2000, 1, 1)
|
||||
}
|
||||
|
||||
def test_missing_date_field_in_partial_update(self):
|
||||
"""
|
||||
When performing partial update if `date_field` is missed it takes its current value
|
||||
"""
|
||||
data = {'slug': 'existing2'}
|
||||
serializer = UniqueForDateSerializer(instance=self.instance, data=data, partial=True)
|
||||
assert serializer.is_valid()
|
||||
assert serializer.validated_data == {
|
||||
'slug': 'existing2'
|
||||
}
|
||||
|
||||
def test_missing_field_in_partial_update(self):
|
||||
"""
|
||||
When performing partial update if `field` is missed it takes its current value
|
||||
"""
|
||||
data = {'body': 'new body'}
|
||||
serializer = UniqueForDateSerializer(instance=self.instance, data=data, partial=True)
|
||||
assert serializer.is_valid()
|
||||
assert serializer.validated_data == {
|
||||
'body': 'new body'
|
||||
}
|
||||
|
||||
# Tests for `UniqueForMonthValidator`
|
||||
# ----------------------------------
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user