mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 19:14:01 +03:00
Merge pull request #179 from shawnlewis/master
Okay, that looks good to me!
This commit is contained in:
commit
f041ee023e
|
@ -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
|
||||
|
|
|
@ -104,6 +104,27 @@ class TestFieldNesting(TestCase):
|
|||
self.assertEqual(SerializerM2().serialize(self.m2), {'field': {'field1': u'foo'}})
|
||||
self.assertEqual(SerializerM3().serialize(self.m3), {'field': {'field2': u'bar'}})
|
||||
|
||||
def test_serializer_no_fields(self):
|
||||
"""
|
||||
Test related serializer works when the fields attr isn't present. Fix for
|
||||
#178.
|
||||
"""
|
||||
class NestedM2(Serializer):
|
||||
fields = ('field1', )
|
||||
|
||||
class NestedM3(Serializer):
|
||||
fields = ('field2', )
|
||||
|
||||
class SerializerM2(Serializer):
|
||||
include = [('field', NestedM2)]
|
||||
exclude = ('id', )
|
||||
|
||||
class SerializerM3(Serializer):
|
||||
fields = [('field', NestedM3)]
|
||||
|
||||
self.assertEqual(SerializerM2().serialize(self.m2), {'field': {'field1': u'foo'}})
|
||||
self.assertEqual(SerializerM3().serialize(self.m3), {'field': {'field2': u'bar'}})
|
||||
|
||||
def test_serializer_classname_nesting(self):
|
||||
"""
|
||||
Test related model serialization
|
||||
|
|
Loading…
Reference in New Issue
Block a user