Selecting & Deferring Fields

Learn via video courses
Topics Covered

Overview

We'll delve into Django's models in great detail and now we'll examine the typical data management features that Django includes, about QuerySets and different type fields in Django.

Introduction

Nowadays, a database is used to implement the majority of web applications. In the Django application, a queryset is employed to get records by filtering, slicing, or ordering the database table without altering the original data. The database table was built using Django by the model.

To understand how to utilize queryset, one must be familiar with Django's model usage. The queryset's primary purpose is to iterate database tables' records by turning those records into SQL queries. It can be accessed from the Python command line or by creating a Python script to show the browser's output. This tutorial has described the numerous ways that a queryset can be used to retrieve data from a database table.

QuerySet in Django

A QuerySet is a grouping of information from a database. A list of items forms the foundation of a query set. By enabling filtering and ordering, QuerySets makes it simpler to obtain the data you require. Django's object-relational mapper (ORM) makes it simple for developers to work effectively even without prior experience with databases and SQL. Without ever touching the database, QuerySets represent a group of items from the database and can be built, filtered, divided, or otherwise handed around. Until we perform the QuerySet evaluation, there is no database activity. When utilizing the Django ORM, adding a new row to a table is as easy as adding a new object to the Model class. Model objects from Django ORM are mapped to relational database queries. It is simple to write a Django queryset for any SQL query. In the views.py file or the Django Shell, you can independently test the questions for Create, Filter, Update, Order, etc.

filter()

filter() returns a new QuerySet with objects in it that match the lookup criteria. Field lookups below outline the format that the lookup parameters (** kwargs) should take. You can restrict the results of your search by using the filter() method, which enables you to only return the rows that exactly match your search phrase.

Example :

To Return the records where the first name is 'XYZ'.

By separating each field's arguments with a comma, you can filter on several fields because the filter() method accepts parameters as **kwargs (keyword arguments).

Example :

Return records where the first name is "XYZ" and the course is Django :

It is more difficult to retrieve entries where the first name is XYZ or the first name is ABC (i.e., records that match either query, not necessarily both). Multiple filter() methods can be used, each separated by the pipe | character.

Example :

Return records where the first name is "XYZ" or "ABC" :

exclude()

A new QuerySet that contains objects that don't match the specified lookup parameters is returned.

The exclude() function of the QuerySet class returns every item that does not match the supplied function parameters. Therefore, any data that matches the parameter supplied to the exclude() method will be removed from the QuerySet that is returned.

Example :

Removes records where the first name is "XYZ" :

all()

all() copies the current QuerySet and returns it (or QuerySet subclass). This can come in handy when you wish to pass in a model manager or a query set and further filter the output. You will undoubtedly have a QuerySet to deal with after using all() on either object.

A QuerySet often caches the results of the evaluation. You can retrieve updated results for the same query if the database's data may have changed since a QuerySet was assessed by executing all() on one that has already been evaluated.

distinct()

distinct() returns a new QuerySet with the SQL statement SELECT DISTINCT. By doing this, duplicate entries are removed from the query results. A QuerySet does not automatically remove redundant rows. Due to the absence of duplicate result rows in straightforward queries like Blog.objects.all(), this issue rarely arises in practice. When a QuerySet is examined, duplicate results may appear if your query spans multiple tables. When that happens, utilize distinct().

If you use a values() query to limit the columns chosen, any order_by() (or default model ordering) will still be in play and may have an impact on the uniqueness of the results.

The lesson here is to exercise caution when ordering by related models while using distinct(). Be cautious when ordering by fields, not in the values() call when using distinct() and values() combined.

Example :

raw()

Django allows you to use raw SQL queries when the model query API isn't performing adequately or you need extra performance. The Django Object Relational Mapper (ORM) facilitates communication between our code and the database, executing unfiltered queries.

raw() never takes into account previous filtering; it always starts a new query. The Manager or a brand-new QuerySet object should typically be used to call it as a result.

Fields in the query are automatically mapped to fields on the model by raw().

defer()

In some data modeling scenarios, your models may have a large number of fields, some of which may include a lot of data (like text fields), or require expensive processing to convert them to Python objects. You can instruct Django not to fetch certain fields from the database if you plan to use the queryset's results in a circumstance where you are unsure if you will use them at the time you first fetch the data. Model instances will still be returned by a queryset with deferred fields. If you access a deferred field, the database for that field will be obtained(one by one at a time, not all the deferred fields together).

Example :

To defer, you can place numerous calls(). New fields are added to the deferred set with each call, for example :

Passing None as an argument to defer() will clear the list of deferred fields.

Example :

only()

The only() function is essentially the opposite of the defer(). It receives the fields that shouldn't be delayed while fetching a model when you call it. The code may be simpler if you use just() to indicate the complementary set of fields in a model where nearly all of the fields must be postponed.

Example :

Consider a model with the following fields : first name, courses, and topics. Regarding deferred fields, the next two querysets are identical :

Every time you call only(), it replaces the list of fields that are loaded right away. The name of the method is a mnemonic: just those fields are loaded right away, and the rest are postponed. As a result, just the final fields are taken into account after each call to only().

Example :

only() is also subject to all of the warnings listed in the notice for the defer() documentation. Use it with caution and only after exploring all other possibilities.

Difference Between defer() and only()

  • The parameter list of columns supplied to defer() will not be queried. only() will query the list of columns supplied to it as a parameter.
  • only() receives the fields that shouldn't be delayed while fetching a model when you call it whereas if you access a deferred field, the database for that field will be obtained one by one, not all the deferred fields together.

Difference Between filter() and exclude()

  • When using exclude(), you can delete the data you do not want to be included while using filter() to obtain the specific data you require.
  • By using the filter() method, you may limit your search results to only the rows that fit your search criteria whereas the return value of Django's exclude() method is essentially a new QuerySet with all the objects that don't match the specified argument given.

Conclusion

Let's quickly revise what we learned in this article.

  • The queryset's primary purpose is to iterate database tables' records by turning those records into SQL queries.
  • QuerySets represent a group of items from the database and can be built, filtered, divided, or otherwise handed around. Until we perform the QuerySet evaluation, there is no database activity.
  • filter() returns a new QuerySet with objects in it that match the lookup criteria.
  • The exclude() function of the QuerySet class returns every item that does not match the supplied function parameters.
  • all() copies the current QuerySet and returns it.
  • distinct() returns a new QuerySet with the SQL statement SELECT DISTINCT that duplicate entries are removed from the query results.
  • If you access a deferred field, the database for that field will be obtained one by one, not all the deferred fields together. It can be done by defer().
  • only() receives the fields that shouldn't be delayed while fetching a model when you call it.