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 copy
|
||||||
import inspect
|
import inspect
|
||||||
import traceback
|
import traceback
|
||||||
from collections import OrderedDict, defaultdict
|
from collections import ChainMap, OrderedDict, defaultdict
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
|
|
||||||
from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
|
from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
|
||||||
|
@ -215,12 +215,17 @@ class BaseSerializer(Field):
|
||||||
'passed when instantiating the serializer instance.'
|
'passed when instantiating the serializer instance.'
|
||||||
)
|
)
|
||||||
|
|
||||||
if not hasattr(self, '_validated_data'):
|
if not hasattr(self, '_validated_data'):
|
||||||
validation_data = self.to_representation(self.instance) if self.instance else {}
|
if self.instance and getattr(getattr(self, 'Meta', None), 'validate_entire_instance', api_settings.VALIDATE_ENTIRE_INSTANCE):
|
||||||
validation_data.update(self.initial_data)
|
data = ChainMap(
|
||||||
|
self.initial_data,
|
||||||
|
self.to_representation(self.instance)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
data = self.initial_data
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._validated_data = self.run_validation(validation_data)
|
self._validated_data = self.run_validation(data)
|
||||||
except ValidationError as exc:
|
except ValidationError as exc:
|
||||||
self._validated_data = {}
|
self._validated_data = {}
|
||||||
self._errors = exc.detail
|
self._errors = exc.detail
|
||||||
|
|
|
@ -124,6 +124,9 @@ DEFAULTS = {
|
||||||
'retrieve': 'read',
|
'retrieve': 'read',
|
||||||
'destroy': 'delete'
|
'destroy': 'delete'
|
||||||
},
|
},
|
||||||
|
|
||||||
|
# Validation
|
||||||
|
'VALIDATE_ENTIRE_INSTANCE': False,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1348,6 +1348,7 @@ class Issue7489Serializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Issue7489Model
|
model = Issue7489Model
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
validate_entire_instance = True
|
||||||
|
|
||||||
|
|
||||||
class Issue7489Test(TestCase):
|
class Issue7489Test(TestCase):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user