In the statement Book.objects.all()
, objects
is a special attribute through which you query your database. In Chapter 4, Models, we briefly identified this as the model's manager. Now it's time to dive a bit deeper into what managers are and how you can use them.
In short, a model's manager is an object through which Django models perform database queries. Each Django model has at least one manager, and you can create custom managers in order to customize database access. There are two reasons you might want to create a custom manager: to add extra manager methods, and/or to modify the initial QuerySet
the manager returns.
Adding extra manager methods is the preferred way to add table-level functionality to your models. (For row-level functionality-that is, functions that act on a single instance of a model object-use model methods, which are explained later in this chapter.)
For example, let's give our Book
model a manager method title_count()
that takes...