diff options
| author | Tom Christie | 2013-02-07 12:57:40 +0000 |
|---|---|---|
| committer | Tom Christie | 2013-02-07 12:57:40 +0000 |
| commit | 670ac25b25a3c7fb54fca6aa9344b8250ab49edb (patch) | |
| tree | fdd12baeb96f10d8824d172553dbd6e469e8798d /rest_framework/fields.py | |
| parent | 8113d661260834a91d91481f31860398cde9212d (diff) | |
| download | django-rest-framework-670ac25b25a3c7fb54fca6aa9344b8250ab49edb.tar.bz2 | |
Allow serializers to handle dicts as well as objects. Fixes #447.
Diffstat (limited to 'rest_framework/fields.py')
| -rw-r--r-- | rest_framework/fields.py | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 2c3e59b5..aa6fa3ab 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -30,6 +30,21 @@ def is_simple_callable(obj): ) +def get_component(obj, attr_name): + """ + Given an object, and an attribute name, + return that attribute on the object. + """ + if isinstance(obj, dict): + val = obj[attr_name] + else: + val = getattr(obj, attr_name) + + if is_simple_callable(val): + return val() + return val + + class Field(object): read_only = True creation_counter = 0 @@ -82,11 +97,9 @@ class Field(object): if self.source: value = obj for component in self.source.split('.'): - value = getattr(value, component) - if is_simple_callable(value): - value = value() + value = get_component(value, component) else: - value = getattr(obj, field_name) + value = get_component(obj, field_name) return self.to_native(value) def to_native(self, value): |
