Nicer write_only fields implementation. Closes #1355

This commit is contained in:
Tom Christie 2014-01-15 08:53:23 +00:00
parent 9d6129a95f
commit e9fda70b4a
2 changed files with 5 additions and 11 deletions

View File

@ -114,10 +114,6 @@ def strip_multiple_choice_msg(help_text):
return help_text.replace(multiple_choice_msg, '') return help_text.replace(multiple_choice_msg, '')
class IgnoreFieldException(Exception):
pass
class Field(object): class Field(object):
read_only = True read_only = True
creation_counter = 0 creation_counter = 0
@ -329,7 +325,7 @@ class WritableField(Field):
def field_to_native(self, obj, field_name): def field_to_native(self, obj, field_name):
if self.write_only: if self.write_only:
raise IgnoreFieldException() return None
return super(WritableField, self).field_to_native(obj, field_name) return super(WritableField, self).field_to_native(obj, field_name)
def field_from_native(self, data, files, field_name, into): def field_from_native(self, data, files, field_name, into):

View File

@ -346,14 +346,12 @@ class BaseSerializer(WritableField):
continue continue
field.initialize(parent=self, field_name=field_name) field.initialize(parent=self, field_name=field_name)
key = self.get_field_key(field_name) key = self.get_field_key(field_name)
try: value = field.field_to_native(obj, field_name)
value = field.field_to_native(obj, field_name)
except IgnoreFieldException:
continue
method = getattr(self, 'transform_%s' % field_name, None) method = getattr(self, 'transform_%s' % field_name, None)
if callable(method): if callable(method):
value = method(obj, value) 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) ret.fields[key] = self.augment_field(field, field_name, key, value)
return ret return ret
@ -389,7 +387,7 @@ class BaseSerializer(WritableField):
across relationships. across relationships.
""" """
if self.write_only: if self.write_only:
raise IgnoreFieldException() return None
if self.source == '*': if self.source == '*':
return self.to_native(obj) return self.to_native(obj)