I am asking this question again (it was asked back in 2009),
We know there is a
values()
method of QuerySet, when there is a foreignkey (author, for example), it result like:[{ 'author_id':3, ... }, ...]
I want a result like:
[{ 'author':{'name':'dave',...}, ... }, ...]
Has something changed in the newer versions of Django?
I want to convert the query set into a combination of lists and dictionnaries, is it possible?
I will then take this object and place it into a bigger object to serialize it. It is the reason why I don't want to serialize it right away.
You can access related fields via
values()
without the necessity to useselect_related()
(only these will be fetched without subsequent lookup):This will return the following structure:
If you need it in a nested structure you would have to transform it afterwards.
Note that for M2M relations, this returns one list entry per M2M relation, so probably not a desired effect. But for OneToOne/ForeignKey relations it works just fine.