mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-16 20:00:23 +00:00
Update PassThroughManager docs to recommend alternatives; fixes GH-201.
This commit is contained in:
parent
0e5c42377f
commit
9eeca10835
1 changed files with 3 additions and 44 deletions
|
|
@ -128,50 +128,9 @@ not required).
|
|||
PassThroughManager
|
||||
------------------
|
||||
|
||||
A common "gotcha" when defining methods on a custom manager class is that those
|
||||
same methods are not automatically also available on the QuerySets returned by
|
||||
that manager, so are not "chainable". This can be counterintuitive, as most of
|
||||
the public QuerySet API is mirrored on managers. It is possible to create a
|
||||
custom Manager that returns QuerySets that have the same additional methods,
|
||||
but this requires boilerplate code. The ``PassThroughManager`` class
|
||||
(`contributed by Paul McLanahan`_) removes this boilerplate.
|
||||
|
||||
.. _contributed by Paul McLanahan: http://paulm.us/post/3717466639/passthroughmanager-for-django
|
||||
|
||||
To use ``PassThroughManager``, rather than defining a custom manager with
|
||||
additional methods, define a custom ``QuerySet`` subclass with the additional
|
||||
methods you want, and pass that ``QuerySet`` subclass to the
|
||||
``PassThroughManager.for_queryset_class()`` class method. The returned
|
||||
``PassThroughManager`` subclass will always return instances of your custom
|
||||
``QuerySet``, and you can also call methods of your custom ``QuerySet``
|
||||
directly on the manager:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from datetime import datetime
|
||||
from django.db import models
|
||||
from django.db.models.query import QuerySet
|
||||
from model_utils.managers import PassThroughManager
|
||||
|
||||
class PostQuerySet(QuerySet):
|
||||
def by_author(self, user):
|
||||
return self.filter(user=user)
|
||||
|
||||
def published(self):
|
||||
return self.filter(published__lte=datetime.now())
|
||||
|
||||
def unpublished(self):
|
||||
return self.filter(published__gte=datetime.now())
|
||||
|
||||
|
||||
class Post(models.Model):
|
||||
user = models.ForeignKey(User)
|
||||
published = models.DateTimeField()
|
||||
|
||||
objects = PassThroughManager.for_queryset_class(PostQuerySet)()
|
||||
|
||||
Post.objects.published()
|
||||
Post.objects.by_author(user=request.user).unpublished()
|
||||
`PassThroughManager` was removed in django-model-utils 2.4. Use Django's
|
||||
built-in `QuerySet.as_manager()` and/or `Manager.from_queryset()` utilities
|
||||
instead.
|
||||
|
||||
Mixins
|
||||
------
|
||||
|
|
|
|||
Loading…
Reference in a new issue