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

前端 未结 9 635
臣服心动
臣服心动 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 could define a function using model_to_dict as follows:

    def queryset_to_dict(qs,fields=None, exclude=None):
        my_array=[]
        for x in qs:
            my_array.append(model_to_dict(x,fields=fields,exclude=exclude))
        return my_array
    

提交回复
热议问题