Table of contents
- Django queryset to list using list() method
- Django Queryset to list using values_list() method
- It returns tuples when iterated over. Each tuple contains the value from the respective field or expression passed into the values_list(). For example:
- If you only want single values in a list, then you have to pass a single field and a flat parameter set as True.
- If you want multiple values in a list, then you have to pass multiple arguments in values_list().
While working with Django queryset, at some point you need more than just a queryset object type like converting a queryset to a list.
Django queryset can be converted to a list using the python's built-in list method (list(queryset.objects.all())) but note that it is not ideal to load the whole result into memory via list() in terms of memory and time optimization.
Let's cover other approaches we can work to convert django queryset to list.
Django queryset to list using list() method
Python's built-in list() method to convert a queryset to a list is not an optimized approach as this will load all the objects in your memory.
from apps.blog.models import *
articles = Blog.objects.all()
# This will return a queryset type
print(type(articles))
# Convert articles queryset to a python's list type
articles_list = list(articles)
# This will return a list type
print(type(articles_list))
<class 'django.db.models.query.QuerySet'>
<class 'list'>
Django Queryset to list using values_list() method
Django's built-in model.objects.values_list() method is one of the ideal ways of converting a queryset to a list.
It returns tuples when iterated over. Each tuple contains the value from the respective field or expression passed into the values_list(). For example:
from apps.blog.models import Blog
articles = Blog.objects.values_list()
print(articles)
print(list(articles))
<QuerySet [(32, 'List of useful websites for software engineers', 5, 'list-useful-websites-software-engineers', None, datetime.date(2022, 3, 3), datetime.date(2022, 4, 20), 18, 'List of useful websites for software engineers', '', None, 1, True, <StreamValue [<block richtext: <wagtail.core.rich_text.RichText object at 0x7f61cefeb20>>]>)]>
[(26, 'List of useful websites for software engineers', 5, 'list-useful-websites-software-engineers', None, datetime.date(2022, 3, 3), datetime.date(2022, 4, 20), 18, 'List of useful websites for software engineers', '', None, 1, True, <StreamValue [<block richtext: <wagtail.core.rich_text.RichText object at 0x7f61cefeb20>>]>)]
If you only want single values in a list, then you have to pass a single field and a flat parameter set as True.
from apps.blog.models import Blog
articles = Blog.objects.values_list('id',flat=True)
print(articles)
print(list(articles))
<QuerySet [87, 83, 35, 84, 86]>
[87, 83, 35, 84, 86]
If you want multiple values in a list, then you have to pass multiple arguments in values_list().
from apps.blog.models import Blog
articles = Blog.objects.values_list('id','category')
print(articles)
print(list(articles))
<QuerySet [(87, 18)]>
[(87, 18)]
If you want to read about Django queryset in detail then I want you to go through this guide on Django ORM in detail.