mirror of
https://github.com/Hopiu/django-model-utils.git
synced 2026-03-17 04:10:24 +00:00
added documentation about manager mixins
This commit is contained in:
parent
33c600e28c
commit
1cf5454ee8
2 changed files with 35 additions and 0 deletions
|
|
@ -3,6 +3,7 @@ Alex Orange <crazycasta@gmail.com>
|
|||
Andy Freeland <andy@andyfreeland.net>
|
||||
Carl Meyer <carl@dirtcircle.com>
|
||||
Donald Stufft <donald.stufft@gmail.com>
|
||||
Douglas Meehan <dmeehan@gmail.com>
|
||||
Facundo Gaich <facugaich@gmail.com>
|
||||
Felipe Prenholato <philipe.rp@gmail.com>
|
||||
Gregor Müllegger <gregor@muellegger.de>
|
||||
|
|
|
|||
|
|
@ -172,3 +172,37 @@ directly on the manager:
|
|||
|
||||
Post.objects.published()
|
||||
Post.objects.by_author(user=request.user).unpublished()
|
||||
|
||||
Mixins
|
||||
------
|
||||
|
||||
Each of the above manager classes has a corresponding mixin that can be
|
||||
used to add functionality to any manager. For example, to create a GeoDjango
|
||||
GeoManager that includes 'pass through' functionality, you can write the
|
||||
following code:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from django.contrib.gis.db import models
|
||||
from django.contrib.gis.db.models.query import GeoQuerySet
|
||||
|
||||
from model_utils.managers import PassThroughManagerMixin
|
||||
|
||||
class PassThroughGeoManager(PassThroughManagerMixin, models.GeoManager):
|
||||
pass
|
||||
|
||||
class LocationQuerySet(GeoQuerySet):
|
||||
def within_boundary(self, geom):
|
||||
return self.filter(point__within=geom)
|
||||
|
||||
def public(self):
|
||||
return self.filter(public=True)
|
||||
|
||||
class Location(models.Model):
|
||||
point = models.PointField()
|
||||
public = models.BooleanField(default=True)
|
||||
objects = PassThroughGeoManager.for_queryset_class(LocationQuerySet)()
|
||||
|
||||
Location.objects.public()
|
||||
Location.object.within_boundary(geom=geom)
|
||||
Location.objects.within_boundary(geom=geom).public()
|
||||
|
|
|
|||
Loading…
Reference in a new issue