diff options
Diffstat (limited to 'rest_framework')
| -rw-r--r-- | rest_framework/fields.py | 11 | ||||
| -rw-r--r-- | rest_framework/serializers.py | 4 |
2 files changed, 8 insertions, 7 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 99498da7..15e59861 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -15,6 +15,7 @@ from rest_framework.compat import ( from rest_framework.exceptions import ValidationError from rest_framework.settings import api_settings from rest_framework.utils import html, representation, humanize_datetime +import collections import copy import datetime import decimal @@ -60,14 +61,12 @@ def get_attribute(instance, attrs): # Break out early if we get `None` at any point in a nested lookup. return None try: - instance = getattr(instance, attr) + if isinstance(instance, collections.Mapping): + instance = instance[attr] + else: + instance = getattr(instance, attr) except ObjectDoesNotExist: return None - except AttributeError as exc: - try: - return instance[attr] - except (KeyError, TypeError, AttributeError): - raise exc if is_simple_callable(instance): instance = instance() return instance diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index b0c0efa7..9226895e 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -517,7 +517,9 @@ class ListSerializer(BaseSerializer): """ List of object instances -> List of dicts of primitive datatypes. """ - iterable = data.all() if (hasattr(data, 'all')) else data + # Dealing with nested relationships, data can be a Manager, + # so, first get a queryset from the Manager if needed + iterable = data.all() if isinstance(data, models.Manager) else data return [ self.child.to_representation(item) for item in iterable ] |
