Keep Serializer.fields API consistent while caching values.

This commit is contained in:
jedavis83@gmail.com 2012-11-22 10:50:29 -08:00
parent e03bb9c2fe
commit e9944f82d1
2 changed files with 8 additions and 8 deletions

View File

@ -62,7 +62,7 @@ class BasePaginationSerializer(serializers.Serializer):
super(BasePaginationSerializer, self).__init__(*args, **kwargs) super(BasePaginationSerializer, self).__init__(*args, **kwargs)
results_field = self.results_field results_field = self.results_field
object_serializer = self.opts.object_serializer_class object_serializer = self.opts.object_serializer_class
self.serialize_fields[results_field] = object_serializer(source='object_list') self.fields[results_field] = object_serializer(source='object_list')
def to_native(self, obj): def to_native(self, obj):
""" """

View File

@ -60,7 +60,7 @@ def _get_declared_fields(bases, attrs):
# If this class is subclassing another Serializer, add that Serializer's # If this class is subclassing another Serializer, add that Serializer's
# fields. Note that we loop over the bases in *reverse*. This is necessary # fields. Note that we loop over the bases in *reverse*. This is necessary
# in order to the correct order of fields. # in order to maintain the correct order of fields.
for base in bases[::-1]: for base in bases[::-1]:
if hasattr(base, 'base_fields'): if hasattr(base, 'base_fields'):
fields = base.base_fields.items() + fields fields = base.base_fields.items() + fields
@ -94,7 +94,6 @@ class BaseSerializer(Field):
def __init__(self, instance=None, data=None, files=None, context=None, **kwargs): def __init__(self, instance=None, data=None, files=None, context=None, **kwargs):
super(BaseSerializer, self).__init__(**kwargs) super(BaseSerializer, self).__init__(**kwargs)
self.opts = self._options_class(self.Meta) self.opts = self._options_class(self.Meta)
self.fields = copy.deepcopy(self.base_fields)
self.parent = None self.parent = None
self.root = None self.root = None
@ -103,7 +102,7 @@ class BaseSerializer(Field):
self.init_data = data self.init_data = data
self.init_files = files self.init_files = files
self.object = instance self.object = instance
self.serialize_fields = self.get_fields() self.fields = self.get_fields()
self._data = None self._data = None
self._files = None self._files = None
@ -128,7 +127,8 @@ class BaseSerializer(Field):
ret = SortedDict() ret = SortedDict()
# Get the explicitly declared fields # Get the explicitly declared fields
for key, field in self.fields.items(): base_fields = copy.deepcopy(self.base_fields)
for key, field in base_fields.items():
ret[key] = field ret[key] = field
# Set up the field # Set up the field
field.initialize(parent=self, field_name=key) field.initialize(parent=self, field_name=key)
@ -181,7 +181,7 @@ class BaseSerializer(Field):
ret = self._dict_class() ret = self._dict_class()
ret.fields = {} ret.fields = {}
for field_name, field in self.serialize_fields.items(): for field_name, field in self.fields.items():
key = self.get_field_key(field_name) key = self.get_field_key(field_name)
value = field.field_to_native(obj, field_name) value = field.field_to_native(obj, field_name)
ret[key] = value ret[key] = value
@ -194,7 +194,7 @@ class BaseSerializer(Field):
Converts a dictionary of data into a dictionary of deserialized fields. Converts a dictionary of data into a dictionary of deserialized fields.
""" """
reverted_data = {} reverted_data = {}
for field_name, field in self.serialize_fields.items(): for field_name, field in self.fields.items():
try: try:
field.field_from_native(data, files, field_name, reverted_data) field.field_from_native(data, files, field_name, reverted_data)
except ValidationError as err: except ValidationError as err:
@ -206,7 +206,7 @@ class BaseSerializer(Field):
""" """
Run `validate_<fieldname>()` and `validate()` methods on the serializer Run `validate_<fieldname>()` and `validate()` methods on the serializer
""" """
for field_name, field in self.serialize_fields.items(): for field_name, field in self.fields.items():
try: try:
validate_method = getattr(self, 'validate_%s' % field_name, None) validate_method = getattr(self, 'validate_%s' % field_name, None)
if validate_method: if validate_method: