Permalink
Browse files

QuerySet.as_manager()

  • Loading branch information...
1 parent 3632d28 commit 283af213d1873cefd642b6d9aeb97f285ef227c4 @akaariai committed Jul 2, 2013
@@ -5,6 +5,7 @@
from django.db.models.query import Q
from django.db.models.expressions import F
from django.db.models.manager import Manager
+from django.db.models.query import QuerySet
from django.db.models.base import Model
from django.db.models.aggregates import *
from django.db.models.fields import *
@@ -55,12 +55,13 @@ class RenameManagerMethods(RenameMethodsBase):
)
-class Manager(six.with_metaclass(RenameManagerMethods)):
+class _Manager(six.with_metaclass(RenameManagerMethods)):
# Tracks each time a Manager instance is created. Used to retain order.
creation_counter = 0
+ queryset_class = QuerySet
def __init__(self):
- super(Manager, self).__init__()
+ super(_Manager, self).__init__()
self._set_creation_counter()
self.model = None
self._inherited = False
@@ -91,8 +92,8 @@ def _set_creation_counter(self):
Sets the creation counter value for this instance and increments the
class-level copy.
"""
- self.creation_counter = Manager.creation_counter
- Manager.creation_counter += 1
+ self.creation_counter = _Manager.creation_counter
+ _Manager.creation_counter += 1
def _copy_to_model(self, model):
"""
@@ -116,118 +117,17 @@ def db_manager(self, using):
def db(self):
return self._db or router.db_for_read(self.model)
- #######################
- # PROXIES TO QUERYSET #
- #######################
-
def get_queryset(self):
"""Returns a new QuerySet object. Subclasses can override this method
to easily customize the behavior of the Manager.
"""
- return QuerySet(self.model, using=self._db)
-
- def none(self):
- return self.get_queryset().none()
+ return self.queryset_class(self.model, using=self._db)
def all(self):
+ # All can't be proxied to QuerySet, as prefetch_related is lost on
+ # clone().
return self.get_queryset()
- def count(self):
- return self.get_queryset().count()
-
- def dates(self, *args, **kwargs):
- return self.get_queryset().dates(*args, **kwargs)
-
- def datetimes(self, *args, **kwargs):
- return self.get_queryset().datetimes(*args, **kwargs)
-
- def distinct(self, *args, **kwargs):
- return self.get_queryset().distinct(*args, **kwargs)
-
- def extra(self, *args, **kwargs):
- return self.get_queryset().extra(*args, **kwargs)
-
- def get(self, *args, **kwargs):
- return self.get_queryset().get(*args, **kwargs)
-
- def get_or_create(self, **kwargs):
- return self.get_queryset().get_or_create(**kwargs)
-
- def create(self, **kwargs):
- return self.get_queryset().create(**kwargs)
-
- def bulk_create(self, *args, **kwargs):
- return self.get_queryset().bulk_create(*args, **kwargs)
-
- def filter(self, *args, **kwargs):
- return self.get_queryset().filter(*args, **kwargs)
-
- def aggregate(self, *args, **kwargs):
- return self.get_queryset().aggregate(*args, **kwargs)
-
- def annotate(self, *args, **kwargs):
- return self.get_queryset().annotate(*args, **kwargs)
-
- def complex_filter(self, *args, **kwargs):
- return self.get_queryset().complex_filter(*args, **kwargs)
-
- def exclude(self, *args, **kwargs):
- return self.get_queryset().exclude(*args, **kwargs)
-
- def in_bulk(self, *args, **kwargs):
- return self.get_queryset().in_bulk(*args, **kwargs)
-
- def iterator(self, *args, **kwargs):
- return self.get_queryset().iterator(*args, **kwargs)
-
- def earliest(self, *args, **kwargs):
- return self.get_queryset().earliest(*args, **kwargs)
-
- def latest(self, *args, **kwargs):
- return self.get_queryset().latest(*args, **kwargs)
-
- def first(self):
- return self.get_queryset().first()
-
- def last(self):
- return self.get_queryset().last()
-
- def order_by(self, *args, **kwargs):
- return self.get_queryset().order_by(*args, **kwargs)
-
- def select_for_update(self, *args, **kwargs):
- return self.get_queryset().select_for_update(*args, **kwargs)
-
- def select_related(self, *args, **kwargs):
- return self.get_queryset().select_related(*args, **kwargs)
-
- def prefetch_related(self, *args, **kwargs):
- return self.get_queryset().prefetch_related(*args, **kwargs)
-
- def values(self, *args, **kwargs):
- return self.get_queryset().values(*args, **kwargs)
-
- def values_list(self, *args, **kwargs):
- return self.get_queryset().values_list(*args, **kwargs)
-
- def update(self, *args, **kwargs):
- return self.get_queryset().update(*args, **kwargs)
-
- def reverse(self, *args, **kwargs):
- return self.get_queryset().reverse(*args, **kwargs)
-
- def defer(self, *args, **kwargs):
- return self.get_queryset().defer(*args, **kwargs)
-
- def only(self, *args, **kwargs):
- return self.get_queryset().only(*args, **kwargs)
-
- def using(self, *args, **kwargs):
- return self.get_queryset().using(*args, **kwargs)
-
- def exists(self, *args, **kwargs):
- return self.get_queryset().exists(*args, **kwargs)
-
def _insert(self, objs, fields, **kwargs):
return insert_query(self.model, objs, fields, **kwargs)
@@ -237,6 +137,8 @@ def _update(self, values, **kwargs):
def raw(self, raw_query, params=None, *args, **kwargs):
return RawQuerySet(raw_query=raw_query, model=self.model, params=params, using=self._db, *args, **kwargs)
+Manager = QuerySet.manager_cls(base_cls=_Manager)
+
class ManagerDescriptor(object):
# This class ensures managers aren't accessible via model instances.
Oops, something went wrong.

0 comments on commit 283af21

Please sign in to comment.