mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 08:29:59 +03:00
Make validating entire model opt-in
This commit is contained in:
parent
11621ad5f7
commit
df5e2bd276
|
@ -13,7 +13,7 @@ response content is handled by parsers and renderers.
|
|||
import copy
|
||||
import inspect
|
||||
import traceback
|
||||
from collections import OrderedDict, defaultdict
|
||||
from collections import ChainMap, OrderedDict, defaultdict
|
||||
from collections.abc import Mapping
|
||||
|
||||
from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
|
||||
|
@ -216,11 +216,16 @@ class BaseSerializer(Field):
|
|||
)
|
||||
|
||||
if not hasattr(self, '_validated_data'):
|
||||
validation_data = self.to_representation(self.instance) if self.instance else {}
|
||||
validation_data.update(self.initial_data)
|
||||
if self.instance and getattr(getattr(self, 'Meta', None), 'validate_entire_instance', api_settings.VALIDATE_ENTIRE_INSTANCE):
|
||||
data = ChainMap(
|
||||
self.initial_data,
|
||||
self.to_representation(self.instance)
|
||||
)
|
||||
else:
|
||||
data = self.initial_data
|
||||
|
||||
try:
|
||||
self._validated_data = self.run_validation(validation_data)
|
||||
self._validated_data = self.run_validation(data)
|
||||
except ValidationError as exc:
|
||||
self._validated_data = {}
|
||||
self._errors = exc.detail
|
||||
|
|
|
@ -124,6 +124,9 @@ DEFAULTS = {
|
|||
'retrieve': 'read',
|
||||
'destroy': 'delete'
|
||||
},
|
||||
|
||||
# Validation
|
||||
'VALIDATE_ENTIRE_INSTANCE': False,
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1348,6 +1348,7 @@ class Issue7489Serializer(serializers.ModelSerializer):
|
|||
class Meta:
|
||||
model = Issue7489Model
|
||||
fields = '__all__'
|
||||
validate_entire_instance = True
|
||||
|
||||
|
||||
class Issue7489Test(TestCase):
|
||||
|
|
Loading…
Reference in New Issue
Block a user