4

I try to use updating version of DRF. I used code from tutorial

serializer = SnippetSerializer(Snippet.objects.all(), many=True)
serializer.data

I should get

[{
  'pk': 1, 'title': u'', 'code': u'foo = "bar"\n', 'linenos': False, 
  'language': u'python', 'style': u'friendly'
 }, {
  'pk': 2, 'title': u'', 'code': u'print "hello, world"\n', 'linenos': False, 
  'language': u'python', 'style': u'friendly'
}]

but I got:

[OrderedDict([
  ('pk', 1), ('title', u''), ('code', u'foo = "bar"\n'), 
  ('linenos', False), ('language', 'python'), ('style', 'friendly')
 ]), 
 OrderedDict([
  ('pk', 2), ('title', u''), ('code', u'print "hello, world"\n'), ('linenos', False), 
  ('language', 'python'), ('style', 'friendly')
 ])
]

Please explain how get correctly results?

5
  • 1
    Result is correct. OrderedDict is like a normal dictionary (that you expect), except it will retain its order, which a normal dictionary does not do. Other than this, it works exactly like a normal dictionary. In case you wonder, OrderedDict is from python's collections module (docs.python.org/3/library/collections.html). Commented Jun 26, 2015 at 12:52
  • 1
    What results aren't correct? You can convert an OrderedDict to a dict by calling dict(serializer.data), but then you lose any field ordering. Commented Jun 26, 2015 at 15:55
  • then is tutorial in documentation old? Commented Jun 29, 2015 at 7:06
  • when I did dict(serializer.data) got ValueError: dictionary update sequence element #0 has length 6; 2 is required Commented Jun 29, 2015 at 7:07
  • @AlexYar they are wrong Commented Sep 7, 2017 at 12:10

2 Answers 2

3

if you need valid json, you can do

import json
serializer = SnippetSerializer(Snippet.objects.all(), many=True)
json.dumps(serializer.data)
Sign up to request clarification or add additional context in comments.

1 Comment

Actually this is better: from rest_framework.renderers import JSONRenderer json = JSONRenderer().render(serializer.data)
1

Results are correct. DRF explicitly uses OrderedDict in the serialization process.

OrderedDict:

OrderedDict is a subclass of dict. You can perform all the operations of a normal python dictionary on an OrderedDict.

As per the docs,

Ordered dictionaries are just like regular dictionaries but they remember the order that items were inserted. When iterating over an ordered dictionary, the items are returned in the order their keys were first added.

Also, if you need a regular python dictionary, you can use dict() on serializer.data as Kevin also suggested.

dict(serializer.data)  # Converts to regular python dict

1 Comment

Wrong. You can't call dict on serializer.data because it is of type rest_framework.utils.serializer_helpers.ReturnList

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.