2010-07-31 02:09:46 +00:00
|
|
|
from types import ClassType
|
2011-04-16 19:51:14 +00:00
|
|
|
import warnings
|
2010-07-31 02:09:46 +00:00
|
|
|
|
2010-10-01 18:43:22 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2009-07-30 21:11:36 +00:00
|
|
|
from django.db import models
|
2011-03-29 12:59:50 +00:00
|
|
|
from django.db.models.fields.related import OneToOneField
|
2010-07-31 02:09:46 +00:00
|
|
|
from django.db.models.manager import Manager
|
|
|
|
|
from django.db.models.query import QuerySet
|
2012-11-05 12:43:42 +00:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2009-07-30 21:11:36 +00:00
|
|
|
|
2012-01-03 22:47:41 +00:00
|
|
|
|
2010-11-23 17:48:23 +00:00
|
|
|
class InheritanceQuerySet(QuerySet):
|
|
|
|
|
def select_subclasses(self, *subclasses):
|
|
|
|
|
if not subclasses:
|
2011-03-29 12:59:50 +00:00
|
|
|
subclasses = [rel.var_name for rel in self.model._meta.get_all_related_objects()
|
|
|
|
|
if isinstance(rel.field, OneToOneField)
|
|
|
|
|
and issubclass(rel.field.model, self.model)]
|
2010-11-23 17:48:23 +00:00
|
|
|
new_qs = self.select_related(*subclasses)
|
|
|
|
|
new_qs.subclasses = subclasses
|
|
|
|
|
return new_qs
|
|
|
|
|
|
|
|
|
|
def _clone(self, klass=None, setup=False, **kwargs):
|
2012-01-03 23:47:33 +00:00
|
|
|
for name in ['subclasses', '_annotated']:
|
|
|
|
|
if hasattr(self, name):
|
|
|
|
|
kwargs[name] = getattr(self, name)
|
2010-11-23 17:48:23 +00:00
|
|
|
return super(InheritanceQuerySet, self)._clone(klass, setup, **kwargs)
|
|
|
|
|
|
2012-01-03 22:47:41 +00:00
|
|
|
def annotate(self, *args, **kwargs):
|
|
|
|
|
qset = super(InheritanceQuerySet, self).annotate(*args, **kwargs)
|
|
|
|
|
qset._annotated = [a.default_alias for a in args] + kwargs.keys()
|
|
|
|
|
return qset
|
|
|
|
|
|
2010-11-23 17:48:23 +00:00
|
|
|
def iterator(self):
|
|
|
|
|
iter = super(InheritanceQuerySet, self).iterator()
|
|
|
|
|
if getattr(self, 'subclasses', False):
|
|
|
|
|
for obj in iter:
|
2013-01-27 00:46:51 +00:00
|
|
|
for s in self.subclasses:
|
2012-07-06 20:12:58 +00:00
|
|
|
try:
|
2013-01-27 00:46:51 +00:00
|
|
|
sub_obj = getattr(obj, s)
|
2012-11-05 12:43:42 +00:00
|
|
|
except ObjectDoesNotExist:
|
2013-01-27 00:46:51 +00:00
|
|
|
sub_obj = None
|
|
|
|
|
if sub_obj:
|
|
|
|
|
break
|
|
|
|
|
if not sub_obj:
|
|
|
|
|
sub_obj = obj
|
2012-01-03 23:47:33 +00:00
|
|
|
if getattr(self, '_annotated', False):
|
2012-01-03 22:47:41 +00:00
|
|
|
for k in self._annotated:
|
|
|
|
|
setattr(sub_obj, k, getattr(obj, k))
|
|
|
|
|
|
|
|
|
|
yield sub_obj
|
2010-11-23 17:48:23 +00:00
|
|
|
else:
|
|
|
|
|
for obj in iter:
|
|
|
|
|
yield obj
|
|
|
|
|
|
2012-01-03 22:47:41 +00:00
|
|
|
|
2010-11-23 17:48:23 +00:00
|
|
|
class InheritanceManager(models.Manager):
|
2011-04-16 21:19:55 +00:00
|
|
|
use_for_related_fields = True
|
|
|
|
|
|
2010-11-23 17:48:23 +00:00
|
|
|
def get_query_set(self):
|
|
|
|
|
return InheritanceQuerySet(self.model)
|
|
|
|
|
|
|
|
|
|
def select_subclasses(self, *subclasses):
|
|
|
|
|
return self.get_query_set().select_subclasses(*subclasses)
|
|
|
|
|
|
2011-10-26 06:01:37 +00:00
|
|
|
def get_subclass(self, *args, **kwargs):
|
|
|
|
|
return self.get_query_set().select_subclasses().get(*args, **kwargs)
|
|
|
|
|
|
2010-10-01 18:43:22 +00:00
|
|
|
|
2010-10-05 13:01:26 +00:00
|
|
|
class InheritanceCastMixin(object):
|
2010-10-01 18:43:22 +00:00
|
|
|
def cast(self):
|
|
|
|
|
results = tuple(self.values_list('pk', 'real_type'))
|
|
|
|
|
type_to_pks = {}
|
|
|
|
|
for pk, real_type_id in results:
|
|
|
|
|
type_to_pks.setdefault(real_type_id, []).append(pk)
|
|
|
|
|
content_types = ContentType.objects.in_bulk(type_to_pks.keys())
|
|
|
|
|
pk_to_child = {}
|
|
|
|
|
for real_type_id, pks in type_to_pks.iteritems():
|
|
|
|
|
content_type = content_types[real_type_id]
|
|
|
|
|
child_type = content_type.model_class()
|
|
|
|
|
children = child_type._default_manager.in_bulk(pks)
|
|
|
|
|
for pk, child in children.iteritems():
|
|
|
|
|
pk_to_child[pk] = child
|
|
|
|
|
children = []
|
|
|
|
|
# sort children into same order as parents where returned
|
|
|
|
|
for pk, real_type_id in results:
|
|
|
|
|
children.append(pk_to_child[pk])
|
|
|
|
|
return children
|
|
|
|
|
|
|
|
|
|
|
2009-07-30 21:11:36 +00:00
|
|
|
class QueryManager(models.Manager):
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
if args:
|
|
|
|
|
self._q = args[0]
|
|
|
|
|
else:
|
|
|
|
|
self._q = models.Q(**kwargs)
|
|
|
|
|
super(QueryManager, self).__init__()
|
|
|
|
|
|
|
|
|
|
def order_by(self, *args):
|
|
|
|
|
self._order_by = args
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def get_query_set(self):
|
|
|
|
|
qs = super(QueryManager, self).get_query_set().filter(self._q)
|
|
|
|
|
if hasattr(self, '_order_by'):
|
|
|
|
|
return qs.order_by(*self._order_by)
|
|
|
|
|
return qs
|
2010-07-31 02:09:46 +00:00
|
|
|
|
|
|
|
|
|
2011-03-08 18:52:32 +00:00
|
|
|
class PassThroughManager(models.Manager):
|
2011-03-29 17:10:50 +00:00
|
|
|
"""
|
2011-03-08 18:52:32 +00:00
|
|
|
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.
|
2011-03-29 17:10:50 +00:00
|
|
|
|
2011-03-08 18:52:32 +00:00
|
|
|
Alternately, if you don't need any extra methods on your manager that
|
2012-09-27 15:48:04 +00:00
|
|
|
aren't on your QuerySet, then just pass your QuerySet class to the
|
|
|
|
|
``for_queryset_class`` class method.
|
2011-03-29 17:10:50 +00:00
|
|
|
|
2011-03-08 18:52:32 +00:00
|
|
|
class PostQuerySet(QuerySet):
|
|
|
|
|
def enabled(self):
|
|
|
|
|
return self.filter(disabled=False)
|
2011-03-29 17:10:50 +00:00
|
|
|
|
2011-03-08 18:52:32 +00:00
|
|
|
class Post(models.Model):
|
2012-09-27 15:48:04 +00:00
|
|
|
objects = PassThroughManager.for_queryset_class(PostQuerySet)()
|
2011-03-29 17:10:50 +00:00
|
|
|
|
|
|
|
|
"""
|
2011-03-08 18:52:32 +00:00
|
|
|
# pickling causes recursion errors
|
2012-11-08 08:18:19 +00:00
|
|
|
_deny_methods = ['__getstate__', '__setstate__', '__getinitargs__',
|
|
|
|
|
'__getnewargs__', '__copy__', '__deepcopy__', '_db']
|
2011-03-29 17:10:50 +00:00
|
|
|
|
2011-03-08 18:52:32 +00:00
|
|
|
def __init__(self, queryset_cls=None):
|
|
|
|
|
self._queryset_cls = queryset_cls
|
2011-03-09 19:44:10 +00:00
|
|
|
super(PassThroughManager, self).__init__()
|
2011-03-29 17:10:50 +00:00
|
|
|
|
2011-03-08 18:52:32 +00:00
|
|
|
def __getattr__(self, name):
|
|
|
|
|
if name in self._deny_methods:
|
|
|
|
|
raise AttributeError(name)
|
|
|
|
|
return getattr(self.get_query_set(), name)
|
2011-03-29 17:10:50 +00:00
|
|
|
|
2011-03-08 18:52:32 +00:00
|
|
|
def get_query_set(self):
|
|
|
|
|
if self._queryset_cls is not None:
|
2011-03-30 01:57:49 +00:00
|
|
|
kargs = {'model': self.model}
|
|
|
|
|
if hasattr(self, '_db'):
|
|
|
|
|
kargs['using'] = self._db
|
|
|
|
|
return self._queryset_cls(**kargs)
|
2011-03-08 18:52:32 +00:00
|
|
|
return super(PassThroughManager, self).get_query_set()
|
|
|
|
|
|
2011-12-04 13:57:16 +00:00
|
|
|
@classmethod
|
|
|
|
|
def for_queryset_class(cls, queryset_cls):
|
|
|
|
|
class _PassThroughManager(cls):
|
2011-12-06 00:08:51 +00:00
|
|
|
def __init__(self):
|
|
|
|
|
return super(_PassThroughManager, self).__init__()
|
|
|
|
|
|
2011-12-04 13:57:16 +00:00
|
|
|
def get_query_set(self):
|
|
|
|
|
kwargs = {}
|
|
|
|
|
if hasattr(self, "_db"):
|
|
|
|
|
kwargs["using"] = self._db
|
|
|
|
|
return queryset_cls(self.model, **kwargs)
|
|
|
|
|
|
|
|
|
|
return _PassThroughManager
|
|
|
|
|
|
2011-03-08 18:52:32 +00:00
|
|
|
|
2010-07-31 02:09:46 +00:00
|
|
|
def manager_from(*mixins, **kwds):
|
2011-03-29 17:10:50 +00:00
|
|
|
"""
|
2010-07-31 02:09:46 +00:00
|
|
|
Returns a Manager instance with extra methods, also available and
|
|
|
|
|
chainable on generated querysets.
|
|
|
|
|
|
|
|
|
|
(By George Sakkis, originally posted at
|
|
|
|
|
http://djangosnippets.org/snippets/2117/)
|
|
|
|
|
|
|
|
|
|
:param mixins: Each ``mixin`` can be either a class or a function. The
|
|
|
|
|
generated manager and associated queryset subclasses extend the mixin
|
|
|
|
|
classes and include the mixin functions (as methods).
|
|
|
|
|
|
|
|
|
|
:keyword queryset_cls: The base queryset class to extend from
|
|
|
|
|
(``django.db.models.query.QuerySet`` by default).
|
|
|
|
|
|
|
|
|
|
:keyword manager_cls: The base manager class to extend from
|
|
|
|
|
(``django.db.models.manager.Manager`` by default).
|
2011-03-29 17:10:50 +00:00
|
|
|
|
|
|
|
|
"""
|
2011-04-16 19:51:14 +00:00
|
|
|
warnings.warn(
|
|
|
|
|
"manager_from is pending deprecation; use PassThroughManager instead.",
|
|
|
|
|
PendingDeprecationWarning,
|
|
|
|
|
stacklevel=2)
|
2010-08-16 21:58:16 +00:00
|
|
|
# collect separately the mixin classes and methods
|
2010-07-31 02:09:46 +00:00
|
|
|
bases = [kwds.get('queryset_cls', QuerySet)]
|
2010-08-16 21:58:16 +00:00
|
|
|
methods = {}
|
2010-07-31 02:09:46 +00:00
|
|
|
for mixin in mixins:
|
|
|
|
|
if isinstance(mixin, (ClassType, type)):
|
|
|
|
|
bases.append(mixin)
|
|
|
|
|
else:
|
2010-08-16 21:58:16 +00:00
|
|
|
try: methods[mixin.__name__] = mixin
|
2010-07-31 02:09:46 +00:00
|
|
|
except AttributeError:
|
|
|
|
|
raise TypeError('Mixin must be class or function, not %s' %
|
|
|
|
|
mixin.__class__)
|
|
|
|
|
# create the QuerySet subclass
|
|
|
|
|
id = hash(mixins + tuple(kwds.iteritems()))
|
2010-08-16 21:58:16 +00:00
|
|
|
new_queryset_cls = type('Queryset_%d' % id, tuple(bases), methods)
|
2010-07-31 02:09:46 +00:00
|
|
|
# create the Manager subclass
|
2010-08-16 21:58:16 +00:00
|
|
|
bases[0] = manager_cls = kwds.get('manager_cls', Manager)
|
|
|
|
|
new_manager_cls = type('Manager_%d' % id, tuple(bases), methods)
|
|
|
|
|
# and finally override new manager's get_query_set
|
|
|
|
|
super_get_query_set = manager_cls.get_query_set
|
|
|
|
|
def get_query_set(self):
|
|
|
|
|
# first honor the super manager's get_query_set
|
|
|
|
|
qs = super_get_query_set(self)
|
|
|
|
|
# and then try to bless the returned queryset by reassigning it to the
|
|
|
|
|
# newly created Queryset class, though this may not be feasible
|
|
|
|
|
if not issubclass(new_queryset_cls, qs.__class__):
|
|
|
|
|
raise TypeError('QuerySet subclass conflict: cannot determine a '
|
|
|
|
|
'unique class for queryset instance')
|
|
|
|
|
qs.__class__ = new_queryset_cls
|
|
|
|
|
return qs
|
|
|
|
|
new_manager_cls.get_query_set = get_query_set
|
|
|
|
|
return new_manager_cls()
|