Paul McLanahan avatar Paul McLanahan committed 4bebd41

Added PassThroughManager to managers.
Added me to authors.

Comments (0)

Files changed (2)

 Jannis Leidel <jannis@leidel.info>
 Gregor Müllegger <gregor@muellegger.de>
 Jeff Elmore <jeffelmore.org>
+Paul McLanahan <paul@mclanahan.net>

model_utils/managers.py

         return qs
 
 
+class PassThroughManager(models.Manager):
+    '''
+    Inherit from this Manager to enable you to call any methods from your
+    custom QuerySet class from your manager. Simply define your QuerySet
+    class, and return an instance of it from your manager's `get_query_set`
+    method.
+    
+    Alternately, if you don't need any extra methods on your manager that
+    aren't on your QuerySet, then just pass your QuerySet class to this
+    class' constructer.
+    
+    class PostQuerySet(QuerySet):
+        def enabled(self):
+            return self.filter(disabled=False)
+    
+    class Post(models.Model):
+        objects = PassThroughManager(PostQuerySet)
+    '''
+    # pickling causes recursion errors
+    _deny_methods = ['__getstate__', '__setstate__']
+    
+    def __init__(self, queryset_cls=None):
+        self._queryset_cls = queryset_cls
+        super(PassthroughManager, self).__init__()
+    
+    def __getattr__(self, name):
+        if name in self._deny_methods:
+            raise AttributeError(name)
+        return getattr(self.get_query_set(), name)
+    
+    def get_query_set(self):
+        if self._queryset_cls is not None:
+            return self._queryset_cls(self.model, using=self._db)
+        return super(PassThroughManager, self).get_query_set()
+
+
 def manager_from(*mixins, **kwds):
     '''
     Returns a Manager instance with extra methods, also available and
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.