Skip to content

Commit 81d0b74

Browse files
committed
Improve field lookup behavior for dicts/mappings. Closes #2244. Closes #2243.
1 parent 76956be commit 81d0b74

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

rest_framework/fields.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from rest_framework.exceptions import ValidationError
1616
from rest_framework.settings import api_settings
1717
from rest_framework.utils import html, representation, humanize_datetime
18+
import collections
1819
import copy
1920
import datetime
2021
import decimal
@@ -60,14 +61,12 @@ def get_attribute(instance, attrs):
6061
# Break out early if we get `None` at any point in a nested lookup.
6162
return None
6263
try:
63-
instance = getattr(instance, attr)
64+
if isinstance(instance, collections.Mapping):
65+
instance = instance[attr]
66+
else:
67+
instance = getattr(instance, attr)
6468
except ObjectDoesNotExist:
6569
return None
66-
except AttributeError as exc:
67-
try:
68-
return instance[attr]
69-
except (KeyError, TypeError, AttributeError):
70-
raise exc
7170
if is_simple_callable(instance):
7271
instance = instance()
7372
return instance

0 commit comments

Comments
 (0)