Manager¶
Managers are the descriptor layer between model classes and querysets.
The default query manager is how most code enters the ORM, but custom
managers are also the standard way to package reusable queryset defaults or
project-specific query helpers.
Descriptor behavior¶
Managers are class-aware and instance-aware:
- on a model class, the manager is bound to the class
- on a model instance, the manager is shallow-copied and bound to that instance
That instance binding is what allows schema-aware or database-aware managers to follow an instance context without mutating the class-level manager.
Typical customization¶
class ActiveUsers(saffier.Manager):
def get_queryset(self) -> saffier.QuerySet:
return super().get_queryset().filter(is_active=True)
class User(saffier.Model):
query: ClassVar[saffier.Manager] = saffier.Manager()
active: ClassVar[ActiveUsers] = ActiveUsers()
saffier.Manager
¶
Manager(
model_class=None,
*,
owner=None,
inherit=True,
name="",
instance=None,
)
Bases: BaseManager
Default manager descriptor for Saffier models.
A manager is both a descriptor and a queryset factory. Accessing it on a model class returns a class-bound manager; accessing it on an instance returns a shallow copy bound to that instance so schema and database context can follow the instance.
Examples:
class PublishedManager(saffier.Manager): def get_queryset(self) -> saffier.QuerySet: return super().get_queryset().filter(is_published=True)
Source code in saffier/core/db/models/managers.py
24 25 26 27 28 29 30 31 32 33 34 35 36 | |
__get__
¶
__get__(instance, owner)
Source code in saffier/core/db/models/managers.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
get_queryset
¶
get_queryset()
Build a queryset bound to the current model, instance, and schema context.
| RETURNS | DESCRIPTION |
|---|---|
QuerySet
|
Queryset configured for the active model, schema, and
TYPE:
|
QuerySet
|
database context. |
| RAISES | DESCRIPTION |
|---|---|
ImproperlyConfigured
|
If the manager is used on an abstract model. |
Source code in saffier/core/db/models/managers.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |