Django ORM and QuerySets – Free Django Tutorials

What is a queryset?

For a given any model,a queryset is a list of objects.With the use of queryset user can read the data from database and can also order it.
Let’s learn it by example.


Django shell

Go to the directory where we have blog app saved which we are creating in this post https://padhle.com/writing-your-first-blog-app-in-django-part-2/ in the command prompt and type this command to open the shell:

python manage.py shell

After you type the command and press enter you will see the screen like below:

Writing Your First Blog App In Django-Part3

After this you are now in django’s interactive console.It is just like python prompt,but have some additions.We can use python command here also.


All objects

Let’s try to display all the articles(for storing articles we are creating model in this post “Writing your first blog app in django-Part 3“)of our blog.For that type this in the command prompt,you will see the screen like this:

Writing Your First Blog App In Django-Part3

It’s error,because we first have to import that model.So import it with this:from articles.models import Article.We import the Article model from articles app.Now try Article.objects.all() command again

Writing Your First Blog App In Django-Part3

Till now we don’t have any article created so we were shown the empty list.We can create articles with django admin interface which we will learn later,but for now we will create new articles using python in shell only.


Create object

This is how we will create a new article object in the database:

Writing Your First Blog App In Django-Part3

After creating articles we can try Article.objects.all() again.We can see from above that now it will see the title of each article.Create some more articles as following for testing purpose.

Writing Your First Blog App In Django-Part3


Filter objects

Most important ability in QuerySets is the ability to filter them.Let’s say we want to filter all articles which are created on 19 may 2018.For that filtering purpose we will use filter instead of all in Article.objects.all().In parentheses we state what the condition for which we want to filter.In our case, the condition is that title should be equal to Sample title.So we have to write following command in shell:

Article.objects.filter(title=”Sample title”)

Writing Your First Blog App In Django-Part3

Or maybe we want to see all the articles that contain the word ‘title’ in the title field? for that we have to type the following command
in shell

Article.objects.filter(title__contains=”title”)

If we have a field of published_date in model Article then we can also do the filtering of all the posts that have published_date set in the past by following:

from django.utils import timezone
Article.objects.filter(published_date__lte=timezone.now())


Ordering objects

Queryset also provide us the ordering of list of objects.Suppose we have created field in the Article model.So we can order them by created_date field by following command:

Article.objects.order_by(‘created_date’)

We can also reverse the ordering of objects by adding - at the beginning of created_date field in paranthesis.

Article.objects.order_by(‘-created_date’)


Chaining QuerySets

We can also combine different QuerySets if needed,by chaining them together as following:

Article.objects.filter(published_date__lte=timezone.now()).order_by(‘published_date’)

This is really powerful and will allow users to write quite complex queries.


Close and Clear

To close the shell user can type exit() in the shell.It will close the shell.For clear the screen user can type this:
import os
os.system(‘cls’)
It will clear the shell.


This is all about how to create objects,filer objects,ordering objects and to chaining querysets.

Leave a Comment

Your email address will not be published. Required fields are marked *