mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 21:10:13 +03:00
simplify the handling of the non-native, validation only fields
This commit is contained in:
parent
0fd48e0365
commit
f5d223f782
|
@ -411,9 +411,7 @@ You can add extra non-native `validation only` fields to a `ModelSerializer` pro
|
||||||
|
|
||||||
**A)** The extra `validation only` fields are removed from the `attrs` parameter prior to invoking the `restore_object()` method on the `parent` serializer class.
|
**A)** The extra `validation only` fields are removed from the `attrs` parameter prior to invoking the `restore_object()` method on the `parent` serializer class.
|
||||||
|
|
||||||
**B)** The extra `validation only` fields are removed from the `fields` attribute prior to invoking the `to_native()` method on the `parent` serializer class during the `object attribute` validations.
|
**B)** The extra `validation only` fields are removed from the `fields` attribute prior to invoking the `to_native()` method on the `parent` serializer class when `obj` is None.
|
||||||
|
|
||||||
**Note:** You should `not` remove the extra `validation only` fields from the `fields` attribute prior to invoking the `to_native()` method on the `parent` serializer class during the `form creation` process if you are using the built-in browsable API.
|
|
||||||
|
|
||||||
# Example of overriding restore_object() and to_native() attributes
|
# Example of overriding restore_object() and to_native() attributes
|
||||||
|
|
||||||
|
@ -443,21 +441,15 @@ You can add extra non-native `validation only` fields to a `ModelSerializer` pro
|
||||||
model = User
|
model = User
|
||||||
fields = ('username', 'email', 'password', 'password_confirmation', 'accept_our_terms_and_conditions',)
|
fields = ('username', 'email', 'password', 'password_confirmation', 'accept_our_terms_and_conditions',)
|
||||||
write_only_fields = ('password',)
|
write_only_fields = ('password',)
|
||||||
validation_only_fields = ('password_confirmation', 'accept_our_terms_and_conditions',)
|
|
||||||
|
|
||||||
def restore_object(self, attrs, instance=None):
|
def restore_object(self, attrs, instance=None):
|
||||||
# Flow: south-bound -- object creation: model instance
|
for attr in ('password_confirmation', 'accept_our_terms_and_conditions'):
|
||||||
for attr in self.Meta.validation_only_fields:
|
|
||||||
attrs.pop(attr)
|
attrs.pop(attr)
|
||||||
return super(CreateUserSerializer, self).restore_object(attrs, instance)
|
return super(CreateUserSerializer, self).restore_object(attrs, instance)
|
||||||
|
|
||||||
def to_native(self, obj):
|
def to_native(self, obj):
|
||||||
try:
|
if obj is not None:
|
||||||
# Flow: north-bound -- form creation: browser API
|
for field in ('password_confirmation', 'accept_our_terms_and_conditions'):
|
||||||
return super(CreateUserSerializer, self).to_native(obj)
|
|
||||||
except AttributeError as e:
|
|
||||||
# Flow: south-bound -- object validation: model class
|
|
||||||
for field in self.Meta.validation_only_fields:
|
|
||||||
self.fields.pop(field)
|
self.fields.pop(field)
|
||||||
return super(CreateUserSerializer, self).to_native(obj)
|
return super(CreateUserSerializer, self).to_native(obj)
|
||||||
|
|
||||||
|
|
|
@ -40,21 +40,15 @@ class ValidationOnlyFieldsExampleSerializer(serializers.ModelSerializer):
|
||||||
model = ValidationOnlyFieldsExampleModel
|
model = ValidationOnlyFieldsExampleModel
|
||||||
fields = ('email', 'password', 'password_confirmation', 'accept_our_terms_and_conditions',)
|
fields = ('email', 'password', 'password_confirmation', 'accept_our_terms_and_conditions',)
|
||||||
write_only_fields = ('password',)
|
write_only_fields = ('password',)
|
||||||
validation_only_fields = ('password_confirmation', 'accept_our_terms_and_conditions',)
|
|
||||||
|
|
||||||
def restore_object(self, attrs, instance=None):
|
def restore_object(self, attrs, instance=None):
|
||||||
# Flow: south-bound -- object creation: model instance
|
for attr in ('password_confirmation', 'accept_our_terms_and_conditions'):
|
||||||
for attr in self.Meta.validation_only_fields:
|
|
||||||
attrs.pop(attr)
|
attrs.pop(attr)
|
||||||
return super(ValidationOnlyFieldsExampleSerializer, self).restore_object(attrs, instance)
|
return super(ValidationOnlyFieldsExampleSerializer, self).restore_object(attrs, instance)
|
||||||
|
|
||||||
def to_native(self, obj):
|
def to_native(self, obj):
|
||||||
try:
|
if obj is not None:
|
||||||
# Flow: north-bound -- form creation: browser API
|
for field in ('password_confirmation', 'accept_our_terms_and_conditions'):
|
||||||
return super(ValidationOnlyFieldsExampleSerializer, self).to_native(obj)
|
|
||||||
except AttributeError as e:
|
|
||||||
# Flow: south-bound -- object validation: model class
|
|
||||||
for field in self.Meta.validation_only_fields:
|
|
||||||
self.fields.pop(field)
|
self.fields.pop(field)
|
||||||
return super(ValidationOnlyFieldsExampleSerializer, self).to_native(obj)
|
return super(ValidationOnlyFieldsExampleSerializer, self).to_native(obj)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user