Best solution for retrieving dictionary values

15 views
Skip to first unread message

Ian Clark

unread,
Jan 3, 2017, 11:53:06 AM1/3/17
to Django REST framework
Hi,

I had a quick look for relevant issues (closed and open) on GH, but couldn't find anything. Currently you're unable to use the `source` attribute for a serializer to retrieve a dictionary's keys.

That's because the get_attribute() call (https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/fields.py#L99) behaves differently for mappings than with other objects (i.e. it attempts to perform a key lookup rather than an attribute one).

It's obvious why this decision was taken, and I can see the potential gotchas caused by allowing for all attributes on mappings to be retrieved, but I wondered if it might make sense to make an exception for dictionaries and `values`? The alternatives are to either provide a `dict_values` attribute onto the instance being serialized, or to use a SerializerMethodField, both of which would be uglier than simply using `dict.values`.

Interested to get input from others.

Thanks,

Ian
Reply all
Reply to author
Forward
0 new messages