mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 17:47:04 +03:00
Nicer write_only fields implementation. Closes #1355
This commit is contained in:
parent
9d6129a95f
commit
e9fda70b4a
|
@ -114,10 +114,6 @@ def strip_multiple_choice_msg(help_text):
|
|||
return help_text.replace(multiple_choice_msg, '')
|
||||
|
||||
|
||||
class IgnoreFieldException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class Field(object):
|
||||
read_only = True
|
||||
creation_counter = 0
|
||||
|
@ -329,7 +325,7 @@ class WritableField(Field):
|
|||
|
||||
def field_to_native(self, obj, field_name):
|
||||
if self.write_only:
|
||||
raise IgnoreFieldException()
|
||||
return None
|
||||
return super(WritableField, self).field_to_native(obj, field_name)
|
||||
|
||||
def field_from_native(self, data, files, field_name, into):
|
||||
|
|
|
@ -346,14 +346,12 @@ class BaseSerializer(WritableField):
|
|||
continue
|
||||
field.initialize(parent=self, field_name=field_name)
|
||||
key = self.get_field_key(field_name)
|
||||
try:
|
||||
value = field.field_to_native(obj, field_name)
|
||||
except IgnoreFieldException:
|
||||
continue
|
||||
value = field.field_to_native(obj, field_name)
|
||||
method = getattr(self, 'transform_%s' % field_name, None)
|
||||
if callable(method):
|
||||
value = method(obj, value)
|
||||
ret[key] = value
|
||||
if not getattr(field, 'write_only', False):
|
||||
ret[key] = value
|
||||
ret.fields[key] = self.augment_field(field, field_name, key, value)
|
||||
|
||||
return ret
|
||||
|
@ -389,7 +387,7 @@ class BaseSerializer(WritableField):
|
|||
across relationships.
|
||||
"""
|
||||
if self.write_only:
|
||||
raise IgnoreFieldException()
|
||||
return None
|
||||
|
||||
if self.source == '*':
|
||||
return self.to_native(obj)
|
||||
|
|
Loading…
Reference in New Issue
Block a user