From 1cf5454ee8a403659122f7c322e644c4fbf4f167 Mon Sep 17 00:00:00 2001 From: Douglas Meehan Date: Fri, 10 Jan 2014 22:01:46 +0000 Subject: [PATCH] added documentation about manager mixins --- AUTHORS.rst | 1 + docs/managers.rst | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 6d57e7d..f518600 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -3,6 +3,7 @@ Alex Orange Andy Freeland Carl Meyer Donald Stufft +Douglas Meehan Facundo Gaich Felipe Prenholato Gregor Müllegger diff --git a/docs/managers.rst b/docs/managers.rst index e5095a0..058aace 100644 --- a/docs/managers.rst +++ b/docs/managers.rst @@ -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()