How do I convert a Django QuerySet into list of dicts?

前端 未结 9 598
臣服心动
臣服心动 2020-12-02 07:28

How can I convert a Django QuerySet into a list of dicts? I haven\'t found an answer to this so I\'m wondering if I\'m missing some sort of common helper function that every

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 08:01

    You can use the values() method on the dict you got from the Django model field you make the queries on and then you can easily access each field by a index value.

    Call it like this -

    myList = dictOfSomeData.values()
    itemNumberThree = myList[2] #If there's a value in that index off course...
    

提交回复
热议问题