| 1 |
Managers & Querysets |
| 2 |
==================== |
| 3 |
|
| 4 |
Using a Custom Manager |
| 5 |
---------------------- |
| 6 |
|
| 7 |
A nice feature of Django is the possibility to define one's own custom object managers. |
| 8 |
This is fully supported with :pypi:`django-polymorphic`. For creating a custom polymorphic |
| 9 |
manager class, just derive your manager from :class:`~polymorphic.managers.PolymorphicManager` |
| 10 |
instead of :class:`~django.db.models.Manager`. As with vanilla Django, in your model class, you |
| 11 |
should explicitly add the default manager first, and then your custom manager: |
| 12 |
|
| 13 |
.. code-block:: python |
| 14 |
|
| 15 |
from polymorphic.models import PolymorphicModel |
| 16 |
from polymorphic.managers import PolymorphicManager |
| 17 |
|
| 18 |
class TimeOrderedManager(PolymorphicManager): |
| 19 |
def get_queryset(self): |
| 20 |
qs = super(TimeOrderedManager,self).get_queryset() |
| 21 |
return qs.order_by('-start_date') |
| 22 |
|
| 23 |
def most_recent(self): |
| 24 |
qs = self.get_queryset() # get my ordered queryset |
| 25 |
return qs[:10] # limit => get ten most recent entries |
| 26 |
|
| 27 |
class Project(PolymorphicModel): |
| 28 |
objects = PolymorphicManager() # add the default polymorphic manager first |
| 29 |
objects_ordered = TimeOrderedManager() # then add your own manager |
| 30 |
start_date = DateTimeField() # project start is this date/time |
| 31 |
|
| 32 |
The first manager defined (:attr:`~django.db.models.Model.objects` in the example) is used by Django |
| 33 |
as automatic manager for several purposes, including accessing related objects. It must not filter |
| 34 |
objects and it's safest to use the plain :class:`~polymorphic.managers.PolymorphicManager` here. |
| 35 |
|
| 36 |
Manager Inheritance |
| 37 |
------------------- |
| 38 |
|
| 39 |
Polymorphic models inherit/propagate all managers from their base models, as long as these are |
| 40 |
polymorphic. This means that all managers defined in polymorphic base models continue to work as |
| 41 |
expected in models inheriting from this base model: |
| 42 |
|
| 43 |
.. code-block:: python |
| 44 |
|
| 45 |
from polymorphic.models import PolymorphicModel |
| 46 |
from polymorphic.managers import PolymorphicManager |
| 47 |
|
| 48 |
class TimeOrderedManager(PolymorphicManager): |
| 49 |
def get_queryset(self): |
| 50 |
qs = super(TimeOrderedManager,self).get_queryset() |
| 51 |
return qs.order_by('-start_date') |
| 52 |
|
| 53 |
def most_recent(self): |
| 54 |
qs = self.get_queryset() # get my ordered queryset |
| 55 |
return qs[:10] # limit => get ten most recent entries |
| 56 |
|
| 57 |
class Project(PolymorphicModel): |
| 58 |
objects = PolymorphicManager() # add the default polymorphic manager first |
| 59 |
objects_ordered = TimeOrderedManager() # then add your own manager |
| 60 |
start_date = DateTimeField() # project start is this date/time |
| 61 |
|
| 62 |
class ArtProject(Project): # inherit from Project, inheriting its fields and managers |
| 63 |
artist = models.CharField(max_length=30) |
| 64 |
|
| 65 |
ArtProject inherited the managers ``objects`` and ``objects_ordered`` from Project. |
| 66 |
|
| 67 |
``ArtProject.objects_ordered.all()`` will return all art projects ordered regarding their start time |
| 68 |
and ``ArtProject.objects_ordered.most_recent()`` will return the ten most recent art projects. |
| 69 |
|
| 70 |
Using a Custom Queryset Class |
| 71 |
----------------------------- |
| 72 |
|
| 73 |
The :class:`~polymorphic.managers.PolymorphicManager` class accepts one initialization argument, |
| 74 |
which is the queryset class the manager should use. Just as with vanilla Django, you may define your |
| 75 |
own custom queryset classes. Just use :class:`~polymorphic.managers.PolymorphicQuerySet` instead of |
| 76 |
Django's :class:`~django.db.models.query.QuerySet` as the base class: |
| 77 |
|
| 78 |
.. code-block:: python |
| 79 |
|
| 80 |
from polymorphic.models import PolymorphicModel |
| 81 |
from polymorphic.managers import PolymorphicManager |
| 82 |
from polymorphic.query import PolymorphicQuerySet |
| 83 |
|
| 84 |
class MyQuerySet(PolymorphicQuerySet): |
| 85 |
def my_queryset_method(self): |
| 86 |
... |
| 87 |
|
| 88 |
class MyModel(PolymorphicModel): |
| 89 |
my_objects = PolymorphicManager.from_queryset(MyQuerySet)() |
| 90 |
... |
| 91 |
|
| 92 |
If you do not wish to extend from a custom :class:`~polymorphic.managers.PolymorphicManager` you |
| 93 |
may also prefer the :meth:`~polymorphic.managers.PolymorphicQuerySet.as_manager` |
| 94 |
shortcut: |
| 95 |
|
| 96 |
.. code-block:: python |
| 97 |
|
| 98 |
from polymorphic.models import PolymorphicModel |
| 99 |
from polymorphic.query import PolymorphicQuerySet |
| 100 |
|
| 101 |
class MyQuerySet(PolymorphicQuerySet): |
| 102 |
def my_queryset_method(self): |
| 103 |
... |
| 104 |
|
| 105 |
class MyModel(PolymorphicModel): |
| 106 |
my_objects = MyQuerySet.as_manager() |
| 107 |
... |
| 108 |
|
| 109 |
For further discussion see `this topic on the Q&A page |
| 110 |
<https://github.com/django-commons/django-polymorphic/discussions/696#discussioncomment-15223661>`_. |