Javatpoint Logo
Javatpoint Logo

Django ORM Queries | How to work with the ORM Queries

In this tutorial, we will have the complete discussion over the Django ORM queries and how we can use them to manipulate the data.

Django ORM is one of the best tools of Django and plays very essential role to perform database related tasks. It provides abstractions with the database, in a mostly database agnostic way.

Django ORM consists of ease of use abstraction. It keeps "Simple things easy and hard things possible."

Here we will have the detailed explanation of each ORM queries and also see their associated SQL queries.

Creating Table in Database using Model

First, we will create a sample database using the Django model that consists of some data and we will run the query on that database.

model.py

And then run the following command.

We are set to run the query.

How to get all records from table(Model)

We have a model called Student. To get all records from model, we will use the Student.objects.all(). To do so, open the Django shell to run the query.

You might be wonder how Django ORM makes our queries executed or what the corresponding query of the code we are writing. It is quite simple to get the SQL query, we need to use the str() and pass the queryset object along with query.

Corresponding SQL Query


Django ORM Queries

How to add record to table(Model)

We will use the Student.objects.create() and pass the fields along with its value as argument. Let's see the below example.

Note that, we need to use the .save() method on the query object to save the newly created record in table, otherwise it will not show in database.

Retrieving Single Objects from QuerySets

Suppose we need a specific object from a queryset to matching the result. We can do this using the get() method. The get() returns the single object directly. Let's see the following example.

Example -

Example - 2

As we can see in both examples, we get the single object not a queryset of a single object. If there is no result then match the query, get() will raise a DoesNotExist exception. On the other hand, if there is multiple field matches, it will raise the MultipleObjectReturned, which is an attribute of the model class itself.

Filtering the Records

In the earlier example, the QuerySet returned by all() describes the all record in the database table. But sometimes, we need to select the subset of complete set of object and it can be done by adding the filter conditions.

In the below example, we will fetch the data which first name starts with the R.

Note - The difference between get() and filter() method is that, the filter() method returns the queryset of object(s) where get() method returns single object.

Using exclude() Method

It returns a new QuerySet containing objects that do not match the given lookup parameter. In other words, it excluded the records according the lookup condition. Let's understand the following example.

Output:

, , , , ]>

How to make OR queries in Django ORM?

The OR operation is performed when we need the record filtering with two or more conditions. In the below example, we will get the student whose first_name starts with 'A' and last_name starts with 'M'.

Django allows us to do this in two ways.

  • queryset_1 |queryset_2
  • filter(Q(<condition_1>) | Q(<condition_2>

We get the student details those first name starts with 'A' and last name starts with 'S'.

Let's the SQL query of corresponding OR operator.

How to make AND queries in Django ORM?

The AND operation is performed when we need the record matching with two or more conditions. In the below example, we will get the student whose first_name starts with 'P' and last_name starts with 'S'.

Django allows us to do this in three ways.

  • queryset_1 & queryset_2
  • filter(<condition_1>, <condition_2>)
  • filter(Q(condition_1) & Q(condition_2))

Only one object satisfied the given conditions.

We can also use the following query.

Or

All queries will give the same result.

Creating Multiple Object in One Shot

Sometimes we want create multiple objects in one shot. Suppose we want to create new objects at once and we don't want to run the multiple queries to the database. Django ORM provides the bulk_create to create multiple objects in one way.

Let's create the multiple records in one query.

Now, our database table will update. The bulk_create takes a list of unsaved objects.

Limiting QuerySets

We can set the limit on the queryset using the Python list's slicing syntax. This is equivalent operation of SQL's LIMIT and OFFSET clauses. Let's see the following query.

Below query will return first record to fifth record.

Negative indexing is not supported. However, we can use the step in QuerySets.

To fetching the single record, we can do the following operation.

How to order a QuerySets in ascending or descending order?

Django provides the order_by method for ordering the queryset. This method takes the field name which we want to Order (ascending and descending) the result. Let's see the following example.

Example - Ascending order

For descending order, we will use the Not '-' before the query field.

We can also pass the multiple fields in the order_by function.

How to order on a field from a related model (with foreign key)?

Now, we will learn how we can order the data in the relation model. We create another model called Teacher which is a related model of Student model.

Models

We have added teachers name and each teacher is associated with the student. Now we want to order Student by teacher_name inside each teacher_name by the Student. We can do as follows.

Important Field Lookups

Query field lookups are nothing but a condition which specifies same as the SQL WHERE clause. They are stated as keyword arguments to the QuerySet methods such as filter(), exclude(), and get().

Example -

This is same as the following SQL query

Let's understand some important lookups.

  • exact

It returns the exact result according to the search.

Lookup should be used after the __ double underscore. We can use the case-insensitive version called iexact.

  • contains

It is used to case-sensitive test. Let's see the following example.

If we translate the SQL query then it will look like as below.

There is also case-incentive version called icontains.

How to perform join operations in Django

The SQL join combines data or rows from two or more tables based on a common field between them. We can perform join operation in many ways. Let's understand the following example.

How to group record in Django ORM?

Django ORM provides the grouping facility using the aggregation functions like Max, Min, Avg, and Sum. Sometimes we need to get the aggregate values from the objects. Let's understand the following example.

How to perform truncate like operation using Django ORM?

Truncate in SQL means clear the table data for future use. Django doesn't provide the built-in methods to truncate the table, but we can use the delete() method to get the similar result. Let's understand the following example.

If you want to delete the individual object instance, you need to call delete() method on the individual instances of that model. We have called the delete() method on model so it deleted the entire data.

How to get union of Data

Union means getting the record which are common in both query sets. Let's see how we can do this.

What is difference between null=True and blank=True?

In Django, we use null and blank often, by default their values are False. Both of these value work at field level where we want to keep a field null or blank. Both values seem similar but they are different in use.

If null=True means the field value is set as NULL i.e. no data. It is basically for the database column value.

The blank = True specifies whether field is required in forms.

If we set null=True blank=True, means that the field is optional in all circumstances.

Conclusion

In this tutorial, we have learned some important ORM queries. Django ORM is a powerful tool and one of the key pillars of Django. Django comes with the built-in database called SQLite. And we have described the ORM queries which acts same as the SQL queries.


Next TopicDjango Form Widget





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA