Nitin Raturi's Software Engineering Blog

Nitin Raturi's Software Engineering Blog

Share this post

Nitin Raturi's Software Engineering Blog
Nitin Raturi's Software Engineering Blog
Convert A Django Queryset To List
Copy link
Facebook
Email
Notes
More
User's avatar
Discover more from Nitin Raturi's Software Engineering Blog
tech.raturi.in is a personal programming blog owned and maintained by Nitin Raturi. The primary goal of this blog is to share insights on Python programming language and fullstack web development.
Already have an account? Sign in

Convert A Django Queryset To List

Use list(queryset) to convert a queryset to a list. Read more for alternate methods

Nitin Raturi's avatar
Nitin Raturi
Mar 10, 2025

Share this post

Nitin Raturi's Software Engineering Blog
Nitin Raturi's Software Engineering Blog
Convert A Django Queryset To List
Copy link
Facebook
Email
Notes
More
Share

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.


Subscribe to Nitin Raturi's Software Engineering Blog

Launched 3 months ago
tech.raturi.in is a personal programming blog owned and maintained by Nitin Raturi. The primary goal of this blog is to share insights on Python programming language and fullstack web development.

Share this post

Nitin Raturi's Software Engineering Blog
Nitin Raturi's Software Engineering Blog
Convert A Django Queryset To List
Copy link
Facebook
Email
Notes
More
Share

Discussion about this post

User's avatar
Django Custom User Model: Abstractuser
Extend or create a custom user model in django using AbstractUser
Mar 10 â€¢ 
Nitin Raturi

Share this post

Nitin Raturi's Software Engineering Blog
Nitin Raturi's Software Engineering Blog
Django Custom User Model: Abstractuser
Copy link
Facebook
Email
Notes
More
Django Mixin: Custom Class Based Mixins, Example, And Types
A Mixin is a special kind of inheritance in Python to allow classes to share methods between any class.
Mar 10 â€¢ 
Nitin Raturi

Share this post

Nitin Raturi's Software Engineering Blog
Nitin Raturi's Software Engineering Blog
Django Mixin: Custom Class Based Mixins, Example, And Types
Copy link
Facebook
Email
Notes
More
Python-Decouple: Manage Environment Variables In Django
Learn to manage environment variables for multiple django settings: development, production, testing, stage, etc
Mar 10 â€¢ 
Nitin Raturi

Share this post

Nitin Raturi's Software Engineering Blog
Nitin Raturi's Software Engineering Blog
Python-Decouple: Manage Environment Variables In Django
Copy link
Facebook
Email
Notes
More

Ready for more?

© 2025 Nitin Raturi
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Copy link
Facebook
Email
Notes
More

Create your profile

User's avatar

Only paid subscribers can comment on this post

Already a paid subscriber? Sign in

Check your email

For your security, we need to re-authenticate you.

Click the link we sent to , or click here to sign in.