123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- import copy
- import inspect
- from importlib import import_module
- from django.db import router
- from django.db.models.query import QuerySet
- from django.utils import six
- from django.utils.encoding import python_2_unicode_compatible
- @python_2_unicode_compatible
- class BaseManager(object):
-
- creation_counter = 0
-
- auto_created = False
-
-
- use_in_migrations = False
- def __new__(cls, *args, **kwargs):
-
- obj = super(BaseManager, cls).__new__(cls)
- obj._constructor_args = (args, kwargs)
- return obj
- def __init__(self):
- super(BaseManager, self).__init__()
- self._set_creation_counter()
- self.model = None
- self.name = None
- self._db = None
- self._hints = {}
- def __str__(self):
- """ Return "app_label.model_label.manager_name". """
- return '%s.%s' % (self.model._meta.label, self.name)
- def deconstruct(self):
- """
- Returns a 5-tuple of the form (as_manager (True), manager_class,
- queryset_class, args, kwargs).
- Raises a ValueError if the manager is dynamically generated.
- """
- qs_class = self._queryset_class
- if getattr(self, '_built_with_as_manager', False):
-
- return (
- True,
- None,
- '%s.%s' % (qs_class.__module__, qs_class.__name__),
- None,
- None,
- )
- else:
- module_name = self.__module__
- name = self.__class__.__name__
-
- module = import_module(module_name)
- if not hasattr(module, name):
- raise ValueError(
- "Could not find manager %s in %s.\n"
- "Please note that you need to inherit from managers you "
- "dynamically generated with 'from_queryset()'."
- % (name, module_name)
- )
- return (
- False,
- '%s.%s' % (module_name, name),
- None,
- self._constructor_args[0],
- self._constructor_args[1],
- )
- def check(self, **kwargs):
- return []
- @classmethod
- def _get_queryset_methods(cls, queryset_class):
- def create_method(name, method):
- def manager_method(self, *args, **kwargs):
- return getattr(self.get_queryset(), name)(*args, **kwargs)
- manager_method.__name__ = method.__name__
- manager_method.__doc__ = method.__doc__
- return manager_method
- new_methods = {}
-
- predicate = inspect.isfunction if six.PY3 else inspect.ismethod
- for name, method in inspect.getmembers(queryset_class, predicate=predicate):
-
- if hasattr(cls, name):
- continue
-
- queryset_only = getattr(method, 'queryset_only', None)
- if queryset_only or (queryset_only is None and name.startswith('_')):
- continue
-
- new_methods[name] = create_method(name, method)
- return new_methods
- @classmethod
- def from_queryset(cls, queryset_class, class_name=None):
- if class_name is None:
- class_name = '%sFrom%s' % (cls.__name__, queryset_class.__name__)
- class_dict = {
- '_queryset_class': queryset_class,
- }
- class_dict.update(cls._get_queryset_methods(queryset_class))
- return type(class_name, (cls,), class_dict)
- def contribute_to_class(self, model, name):
- if not self.name:
- self.name = name
- self.model = model
- setattr(model, name, ManagerDescriptor(self))
- model._meta.add_manager(self)
- def _set_creation_counter(self):
- """
- Sets the creation counter value for this instance and increments the
- class-level copy.
- """
- self.creation_counter = BaseManager.creation_counter
- BaseManager.creation_counter += 1
- def db_manager(self, using=None, hints=None):
- obj = copy.copy(self)
- obj._db = using or self._db
- obj._hints = hints or self._hints
- return obj
- @property
- def db(self):
- return self._db or router.db_for_read(self.model, **self._hints)
-
-
-
- def get_queryset(self):
- """
- Returns a new QuerySet object. Subclasses can override this method to
- easily customize the behavior of the Manager.
- """
- return self._queryset_class(model=self.model, using=self._db, hints=self._hints)
- def all(self):
-
-
-
-
-
-
- return self.get_queryset()
- def __eq__(self, other):
- return (
- isinstance(other, self.__class__) and
- self._constructor_args == other._constructor_args
- )
- def __ne__(self, other):
- return not (self == other)
- def __hash__(self):
- return id(self)
- class Manager(BaseManager.from_queryset(QuerySet)):
- pass
- class ManagerDescriptor(object):
- def __init__(self, manager):
- self.manager = manager
- def __get__(self, instance, cls=None):
- if instance is not None:
- raise AttributeError("Manager isn't accessible via %s instances" % cls.__name__)
- if cls._meta.abstract:
- raise AttributeError("Manager isn't available; %s is abstract" % (
- cls._meta.object_name,
- ))
- if cls._meta.swapped:
- raise AttributeError(
- "Manager isn't available; '%s.%s' has been swapped for '%s'" % (
- cls._meta.app_label,
- cls._meta.object_name,
- cls._meta.swapped,
- )
- )
- return cls._meta.managers_map[self.manager.name]
- class EmptyManager(Manager):
- def __init__(self, model):
- super(EmptyManager, self).__init__()
- self.model = model
- def get_queryset(self):
- return super(EmptyManager, self).get_queryset().none()
|