Django Custom Managers

Learn via video courses
Topics Covered

Overview

A Manager is a Django class that provides inbuilt methods that act as a bridge between database queries and Django models. Every model in a Django application has at least one Manager. Django managers give the user access to the model's records via manager methods.

Introduction

A Manager is a Django class that acts as a bridge between database queries and Django models. In other words, the manager is the interface that interacts with the database in a Django model. For example, if the user wants to retrieve objects from the database, then we use the inbuilt methods of the manager to get the desired records from the database table.

Syntax of Django Manager

To use the default Django manager object, use the syntax below.

In the above syntax, we mention the model name first followed by .objects and the method that we want to use. Say we have model details, including name, description, contact_no, and age.

With this we just created a table without records, we used the 'create' method to add data to it.

Custom Managers in Django

Django manager works as an interface between the queries and the model. Django provides us with a feature of customizing the Django manager which is creating a custom manager to work accordingly. When we don't want to keep typing lengthy queries, custom managers are extremely useful. However, the custom managers must be able to retrieve all the results when replacing the normal manager, therefore avoid overriding get_required_qs() to remove any rows from the results. Django will produce insufficient results if you do this.

Creating Custom Managers

Let us consider we have model details, when we use the all() method, the manager returns all the records but if we want the manager to return only the list of records where the age is 20. Therefore, we create a custom manager in the models.py file, then link it to the model details.

In the above code,

  • We have created a class MyCustomManager.
  • Under the function get_required_qs, we return super().get_queryset().filter(age= '20') so that we get all the records where the age is 20.

Now in the model details, we link it.

  • In the field obj, we linked the manager we created.

Using Custom Managers

Once we are done with the creation of the model and the custom manager, we register the models in the admin.py file. After making migrations and migrating the changes in the command prompt we activate the shell.

Inside the interactive console, to enter the records in the model we write the following.

We use .obj instead of objects as we are using a custom manager and we specified the name of the field as obj where we linked the MyCustomManager, hence we are using the .obj as a method created by us in the custom manager.

The .obj.all() displays the list of all the records we entered, while the .obj.get_required_qs returns the record where the age is 20 as mentioned in the class MyCustomManager under the function get_required_qs.

Methods of Manager Class

Apart from the 'create' method for creating the records, there are some other methods also for performing other queries.

MethodsDescription
all()returns a query set containing all objects created.
filter()returns a query set with a list of objects that match the given arguments.
get()returns a single object that corresponds to the given argument.
create()creates a new object.
order_by()returns the query arranged in a particular order as mentioned in the argument.

Conclusion

Hello developer!!! I am sure by now you must have understood what Django custom managers are. Let us summarize what we have learned so far

  • A Manager is a Django class that acts as a bridge between database queries and Django models.
  • Django manager enables us to get the records from the model using the manager methods.
  • Django manager works as an interface between the queries and the model.
  • Django provides us with a feature of customizing the Django manager to work accordingly.