mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-06-30 18:33:21 +03:00
Added _post_clean() behaviour by adding a .perform_model_validation() method.
Fixed some tests that were failing due to extra strict validation.
This commit is contained in:
parent
ac84c2ed2e
commit
ca5b99486d
|
@ -428,10 +428,6 @@ class ModelSerializer(Serializer):
|
||||||
kwargs['choices'] = model_field.flatchoices
|
kwargs['choices'] = model_field.flatchoices
|
||||||
return ChoiceField(**kwargs)
|
return ChoiceField(**kwargs)
|
||||||
|
|
||||||
max_length = getattr(model_field, 'max_length', None)
|
|
||||||
if max_length:
|
|
||||||
kwargs['max_length'] = max_length
|
|
||||||
|
|
||||||
field_mapping = {
|
field_mapping = {
|
||||||
models.FloatField: FloatField,
|
models.FloatField: FloatField,
|
||||||
models.IntegerField: IntegerField,
|
models.IntegerField: IntegerField,
|
||||||
|
@ -455,6 +451,59 @@ class ModelSerializer(Serializer):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return ModelField(model_field=model_field, **kwargs)
|
return ModelField(model_field=model_field, **kwargs)
|
||||||
|
|
||||||
|
def from_native(self, data, files):
|
||||||
|
restored_object = super(ModelSerializer, self).from_native(data, files)
|
||||||
|
|
||||||
|
if restored_object is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
self.perform_model_validation(restored_object)
|
||||||
|
return restored_object
|
||||||
|
|
||||||
|
def perform_model_validation(self, restored_object):
|
||||||
|
|
||||||
|
# if hasattr(restored_object, '__iter__'): # Iterables are not model instances
|
||||||
|
# return restored_object
|
||||||
|
#self._errors[field_name] = list(err.messages)
|
||||||
|
|
||||||
|
# opts = self._meta
|
||||||
|
# Update the model instance with self.cleaned_data.
|
||||||
|
# instance = construct_instance(self, self.instance, opts.fields, opts.exclude)
|
||||||
|
|
||||||
|
# exclude = self._get_validation_exclusions()
|
||||||
|
|
||||||
|
# Foreign Keys being used to represent inline relationships
|
||||||
|
# are excluded from basic field value validation. This is for two
|
||||||
|
# reasons: firstly, the value may not be supplied (#12507; the
|
||||||
|
# case of providing new values to the admin); secondly the
|
||||||
|
# object being referred to may not yet fully exist (#12749).
|
||||||
|
# However, these fields *must* be included in uniqueness checks,
|
||||||
|
# so this can't be part of _get_validation_exclusions().
|
||||||
|
# for f_name, field in self.fields.items():
|
||||||
|
# if isinstance(field, InlineForeignKeyField):
|
||||||
|
# exclude.append(f_name)
|
||||||
|
|
||||||
|
# Clean the model instance's fields.
|
||||||
|
try:
|
||||||
|
restored_object.clean_fields() # exclude=exclude)
|
||||||
|
except ValidationError as e:
|
||||||
|
for field_name, error_messages in e.message_dict.items():
|
||||||
|
self._errors[field_name] = self._errors.get(field_name, []) + list(error_messages)
|
||||||
|
|
||||||
|
# Call the model instance's clean method.
|
||||||
|
try:
|
||||||
|
restored_object.clean()
|
||||||
|
except ValidationError as e:
|
||||||
|
for field_name, error_messages in e.message_dict.items():
|
||||||
|
self._errors[field_name] = self._errors.get(field_name, []) + list(error_messages)
|
||||||
|
|
||||||
|
# Validate uniqueness if needed.
|
||||||
|
# exclude = self._get_validation_exclusions()
|
||||||
|
# try:
|
||||||
|
# restored_object.validate_unique() # exclude=exclude)
|
||||||
|
# except ValidationError as e:
|
||||||
|
# model_errors.append(e.message_dict)
|
||||||
|
|
||||||
def restore_object(self, attrs, instance=None):
|
def restore_object(self, attrs, instance=None):
|
||||||
"""
|
"""
|
||||||
Restore the model instance.
|
Restore the model instance.
|
||||||
|
|
|
@ -61,7 +61,7 @@ class BasicModel(RESTFrameworkModel):
|
||||||
|
|
||||||
class SlugBasedModel(RESTFrameworkModel):
|
class SlugBasedModel(RESTFrameworkModel):
|
||||||
text = models.CharField(max_length=100)
|
text = models.CharField(max_length=100)
|
||||||
slug = models.SlugField(max_length=32)
|
slug = models.SlugField(max_length=32, blank=True)
|
||||||
|
|
||||||
|
|
||||||
class DefaultValueModel(RESTFrameworkModel):
|
class DefaultValueModel(RESTFrameworkModel):
|
||||||
|
@ -159,7 +159,7 @@ class Person(RESTFrameworkModel):
|
||||||
|
|
||||||
# Model for issue #324
|
# Model for issue #324
|
||||||
class BlankFieldModel(RESTFrameworkModel):
|
class BlankFieldModel(RESTFrameworkModel):
|
||||||
title = models.CharField(max_length=100, blank=True)
|
title = models.CharField(max_length=100, blank=True, null=True)
|
||||||
|
|
||||||
|
|
||||||
# Model for issue #380
|
# Model for issue #380
|
||||||
|
|
|
@ -169,7 +169,7 @@ class ValidationTests(TestCase):
|
||||||
'content': 'x' * 1001,
|
'content': 'x' * 1001,
|
||||||
'created': datetime.datetime(2012, 1, 1)
|
'created': datetime.datetime(2012, 1, 1)
|
||||||
}
|
}
|
||||||
self.actionitem = ActionItem('Some to do item',
|
self.actionitem = ActionItem(title='Some to do item',
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_create(self):
|
def test_create(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user