참고 사이트
https://stackoverflow.com/questions/7811556/how-do-i-convert-a-django-queryset-into-list-of-dicts
from django.forms.models import model_to_dict
def queryset_to_list(qs,fields=None, exclude=None):
return [model_to_dict(x,fields,exclude) for x in qs]
Suppose your Model has the following fields
id
name
email
Run the following commands in Django shell
>>>qs=<yourmodel>.objects.all()
>>>list=queryset_to_list(qs)
>>>list
[{'id':1, 'name':'abc', 'email':'abc@ab.co'},{'id':2, 'name':'xyz', 'email':'xy@xy.co'}]
Say you want only the id and the name in the list of queryset dictionary
>>>qs=<yourmodel>.objects.all()
>>>list=queryset_to_list(qs,fields=['id','name'])
>>>list
[{'id':1, 'name':'abc'},{'id':2, 'name':'xyz'}]
Similarly, you can exclude fields in your output.