Related serializers passed in via include now work as expected.
This commit is contained in:
Shawn Lewis 2012-02-23 16:30:44 -08:00
parent 98c16e6da8
commit 9c92f96ce2

View File

@ -25,16 +25,9 @@ def _field_to_tuple(field):
def _fields_to_list(fields):
"""
Return a list of field names.
Return a list of field tuples.
"""
return [_field_to_tuple(field)[0] for field in fields or ()]
def _fields_to_dict(fields):
"""
Return a `dict` of field name -> None, or tuple of fields, or Serializer class
"""
return dict([_field_to_tuple(field) for field in fields or ()])
return [_field_to_tuple(field) for field in fields or ()]
class _SkipField(Exception):
@ -110,9 +103,6 @@ class Serializer(object):
self.stack = stack
def get_fields(self, obj):
"""
Return the set of field names/keys to use for a model instance/dict.
"""
fields = self.fields
# If `fields` is not set, we use the default fields and modify
@ -123,9 +113,6 @@ class Serializer(object):
exclude = self.exclude or ()
fields = set(default + list(include)) - set(exclude)
else:
fields = _fields_to_list(self.fields)
return fields
def get_default_fields(self, obj):
@ -139,9 +126,7 @@ class Serializer(object):
else:
return obj.keys()
def get_related_serializer(self, key):
info = _fields_to_dict(self.fields).get(key, None)
def get_related_serializer(self, info):
# If an element in `fields` is a 2-tuple of (str, tuple)
# then the second element of the tuple is the fields to
# set on the related serializer
@ -175,11 +160,11 @@ class Serializer(object):
"""
return self.rename.get(smart_str(key), smart_str(key))
def serialize_val(self, key, obj):
def serialize_val(self, key, obj, related_info):
"""
Convert a model field or dict value into a serializable representation.
"""
related_serializer = self.get_related_serializer(key)
related_serializer = self.get_related_serializer(related_info)
if self.depth is None:
depth = None
@ -219,7 +204,7 @@ class Serializer(object):
fields = self.get_fields(instance)
# serialize each required field
for fname in fields:
for fname, related_info in _fields_to_list(fields):
try:
# we first check for a method 'fname' on self,
# 'fname's signature must be 'def fname(self, instance)'
@ -237,7 +222,7 @@ class Serializer(object):
continue
key = self.serialize_key(fname)
val = self.serialize_val(fname, obj)
val = self.serialize_val(fname, obj, related_info)
data[key] = val
except _SkipField:
pass