What is Django Query Set API | Django Tutorial for Beginners

Query Set API copy

What is Django Query Set API | Django Tutorial for Beginners

What is Django Query Set API

Django is a great and good-featured python-based web framework that is famous all over the world of internet in the field of web development. We are providing a Django tutorial for beginners, in every lesson we learned a new topic, but today we will discuss Django Query set API. Today’s topic is about communicating with the database to retrieve data, update/edit, and delete the data. We can perform different other operations like filtering etc. So, let’s start to learn more about query set API.

As we have published different lessons for Django beginners students about Djagno Models, Template inheritance, Django SEO, Django Security, These are important lessons, you should take to learn. To continue your reading!

What is Query?

This is the topic of Database subject, the query is a collection of meaningful words which have a complete meaning, which is used to communicate with an object, after communication with an object, the object return anything else that is required to that object which sends the query. If we learn it in the case of Database technology then:
The query is the way to communicate with database tables or a collection of tables.
When a developer sends a query to the database, after reading the query, the database returns the required material to the developer. SQL is the structured query language that is used to communicate with the database to retrieve, update delete, etc the data.

What is Query Set?

Actually when one record about an entity is stored in the table, is called an object. When multiple objects are stored in the table they are a set of objects, to retrieve that set of objects, we called query a query set. Query set returned the set of objects the result of a query set may be zero if there is no record in a database, one or many filters.

When we use a query set to get a set of objects it may be filtering to get the specific record, for example, you want to get all the students with the name of ‘Faisal’, so it will return a set of objects because it will contain multiple records which contain ‘Faisal’ name.

Or slicing can be used in the query set to get a required record for example if you want to get last or first records or between records etc.

Or we can also use ordering techniques, in which we can order the records as Ascending or descending or in order to down to up alphabetical or down to up number etc.

What is Django Query Set API?

It is the query that is used to communicate with a database to perform different operations on the data stored in the tables.

  • Create: We can create new records or insert new data
  • Filter: We can filter out the data to find required records
  • Slicing: It is used to get required records, for example, if you want to retrieve the first 3 or last 3 record
  • Order: Very important, because it can change the order of the record which we retrieve as ascending or descending order or alphabetically order.
  • Update: We can update the record in the table if we want
  • Delete: You can delete an object or set of objects or a complete table using the delete query
  • Much more
If you want to run any command to communicate with a database, you can use Django query set API in the views section or in the Django shell section.

WORKING with Django Query Set API

As we have discussed we can perform different operations on that database table data to get the required record about an entity.

So, in the following part, we will create a reocrd using Djagno Query set API.

1. Create an object

This query will create an object with the following information as we have assigned.

from app_name.models import Person

person_Info = Person(name=’Lala’, email=’lala@gamil.com’)

person_Info .save() # it will save all the information

2. Retrieve an object

If you want to get all the objects stored in the specific table, you can use the following query, where Person is the table name in this case.

3. Update object

In this example, you will be cleared how you can update a record in the database table using query set API


It will udpat the record for a specefic id with new value in specefic column you mention.


You can use anpother method also, if you want to update anything else in the table.

return_obj = Person.objects.get(pk=mention_id)
return_obj .name = “new_value”
return_obj .save()

So, it will also update the record. It is the same logic but we have broken the line in 4.

4. Delete an object

As we have mentioned we can perform all the crud operations with Django Query Set API. So the record or set of records you can delete using query set API.

The following query will delete the obejcts in the Person table.


Or to delete a specific record in the person table, you can filter at first then delete the specific record.


5. Filter object

With the default manager class, it is the same as:

It is very important, when we want to update or delete, we have to find or filter specific records which we want to update or delete, then we use filter query.

If we run the following query, all the records will be retrieved with the city name ‘DIKHAN’


7. Order by object

We can retrieve the object in a different order, ascending or descending. For example, you want to get all the student’s names in alphabetical order, then will use the order by the query.

This command will return all the person belong from city Paharpurin descending order


Now! what you have to do?

As you have understood this article about “Django Query Set API”, now you must follow instructions as discussed in this article. In case of any issues or problems, don’t worry, you can discuss them with us.

On other hand, If you have any suggestions or knowledge about this article, you can share it with us, we will appreciate you!

If this article is good then share it on Facebook, Twitter, Pinterest, Instagram, etc.

Advanced Tutorials for Beginners

Complete Guide on Django ORM | Django Tutorials

Django Form with Full Explanation | Complete Django Tutorials 

Complete Django Models | Django Tutorials

Python for Backend Web Development | Complete Guide

What is Django template | Django Tutorials

Leave your thought here

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

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar

Get your Enrollment


Leave your details below and receive a discount coupon in your inbox