From 4762455af33cf1dd23d0580ec7dd5b3387d75490 Mon Sep 17 00:00:00 2001 From: Samuel Sutch Date: Tue, 8 Jul 2014 10:19:10 -0600 Subject: [PATCH 1/4] get_component should work with custom mapping types --- rest_framework/compat.py | 6 ++++++ rest_framework/fields.py | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index fdf12448a..5649370ea 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -79,6 +79,12 @@ except ImportError: from collections import UserDict from collections import MutableMapping as DictMixin +# Mapping is new in python 2.6 +try: + from collections import Mapping +except ImportError: + Mapping = dict + # Try to import PIL in either of the two ways it can end up installed. try: from PIL import Image diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 6caae9242..36782e163 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -24,7 +24,7 @@ from django.utils.datastructures import SortedDict from rest_framework import ISO_8601 from rest_framework.compat import ( timezone, parse_date, parse_datetime, parse_time, BytesIO, six, smart_text, - force_text, is_non_str_iterable + force_text, is_non_str_iterable, Mapping ) from rest_framework.settings import api_settings @@ -50,7 +50,7 @@ def get_component(obj, attr_name): Given an object, and an attribute name, return that attribute on the object. """ - if isinstance(obj, dict): + if isinstance(obj, Mapping): val = obj.get(attr_name) else: val = getattr(obj, attr_name) From 467bc56e08b20423df08b75d860560295394108b Mon Sep 17 00:00:00 2001 From: Samuel Sutch Date: Thu, 10 Jul 2014 14:26:53 -0600 Subject: [PATCH 2/4] __getitem__ to check for container-like objects --- rest_framework/fields.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 36782e163..db98c62a5 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -24,7 +24,7 @@ from django.utils.datastructures import SortedDict from rest_framework import ISO_8601 from rest_framework.compat import ( timezone, parse_date, parse_datetime, parse_time, BytesIO, six, smart_text, - force_text, is_non_str_iterable, Mapping + force_text, is_non_str_iterable ) from rest_framework.settings import api_settings @@ -50,8 +50,8 @@ def get_component(obj, attr_name): Given an object, and an attribute name, return that attribute on the object. """ - if isinstance(obj, Mapping): - val = obj.get(attr_name) + if hasattr(obj, '__getitem__'): + val = obj[attr_name] else: val = getattr(obj, attr_name) From 96b2700516454b213569869ba11d84525d45d233 Mon Sep 17 00:00:00 2001 From: Samuel Sutch Date: Thu, 10 Jul 2014 14:27:30 -0600 Subject: [PATCH 3/4] Remove unneeded Mapping compat code --- rest_framework/compat.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 5649370ea..fdf12448a 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -79,12 +79,6 @@ except ImportError: from collections import UserDict from collections import MutableMapping as DictMixin -# Mapping is new in python 2.6 -try: - from collections import Mapping -except ImportError: - Mapping = dict - # Try to import PIL in either of the two ways it can end up installed. try: from PIL import Image From a346a269ca99caf3d0a3ae4e1cb798c9d7563f42 Mon Sep 17 00:00:00 2001 From: Samuel Sutch Date: Sat, 12 Jul 2014 22:07:58 -0600 Subject: [PATCH 4/4] go back to checking with Mapping --- rest_framework/fields.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index db98c62a5..d9ef0144f 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -10,6 +10,7 @@ import datetime import inspect import re import warnings +import collections from decimal import Decimal, DecimalException from django import forms from django.core import validators @@ -50,7 +51,7 @@ def get_component(obj, attr_name): Given an object, and an attribute name, return that attribute on the object. """ - if hasattr(obj, '__getitem__'): + if isinstance(obj, collections.Mapping): val = obj[attr_name] else: val = getattr(obj, attr_name)