Commit e9fda70b by Tom Christie

Nicer write_only fields implementation. Closes #1355

parent 9d6129a9
......@@ -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,13 +346,11 @@ 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
method = getattr(self, 'transform_%s' % field_name, None)
if callable(method):
value = method(obj, value)
if not getattr(field, 'write_only', False):
ret[key] = value
ret.fields[key] = self.augment_field(field, field_name, key, value)
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment