mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-08 06:14:47 +03:00
Merge 2e24eae49a
into 9b56dda918
This commit is contained in:
commit
f16c9485f0
|
@ -75,7 +75,14 @@ def get_attribute(instance, attrs):
|
|||
return None
|
||||
try:
|
||||
if isinstance(instance, collections.Mapping):
|
||||
instance = instance[attr]
|
||||
try:
|
||||
instance = instance[attr]
|
||||
except KeyError as keyerror:
|
||||
# Does the mapping has a method or property with that name?
|
||||
try:
|
||||
instance = getattr(instance, attr)
|
||||
except AttributeError:
|
||||
raise keyerror
|
||||
else:
|
||||
instance = getattr(instance, attr)
|
||||
except ObjectDoesNotExist:
|
||||
|
|
|
@ -309,3 +309,28 @@ class TestCacheSerializerData:
|
|||
pickled = pickle.dumps(serializer.data)
|
||||
data = pickle.loads(pickled)
|
||||
assert data == {'field1': 'a', 'field2': 'b'}
|
||||
|
||||
|
||||
class TestMapping:
|
||||
def setup(self):
|
||||
class ExampleSerializer(serializers.Serializer):
|
||||
entries = serializers.ListField()
|
||||
last_entry = serializers.CharField()
|
||||
self.Serializer = ExampleSerializer
|
||||
self.data = {'entries': ['a', 'b', 'c']}
|
||||
self.expected = {'entries': ['a', 'b', 'c'], 'last_entry': 'c'}
|
||||
|
||||
def test_mapping_with_method(self):
|
||||
class Mapping(dict):
|
||||
def last_entry(self):
|
||||
return self['entries'][-1]
|
||||
serializer = self.Serializer(Mapping(self.data))
|
||||
assert serializer.data == self.expected
|
||||
|
||||
def test_mapping_with_property(self):
|
||||
class Mapping(dict):
|
||||
@property
|
||||
def last_entry(self):
|
||||
return self['entries'][-1]
|
||||
serializer = self.Serializer(Mapping(self.data))
|
||||
assert serializer.data == self.expected
|
||||
|
|
Loading…
Reference in New Issue
Block a user