2011-03-28 10:11:19 +08:00
|
|
|
import copy
|
2010-01-22 22:30:06 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db import router
|
2009-12-20 10:46:58 +08:00
|
|
|
from django.db.models.query import QuerySet, EmptyQuerySet, insert_query, RawQuerySet
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.db.models import signals
|
2006-06-15 22:05:33 +08:00
|
|
|
from django.db.models.fields import FieldDoesNotExist
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-01-22 22:30:06 +08:00
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
def ensure_default_manager(sender, **kwargs):
|
2009-03-15 11:41:33 +08:00
|
|
|
"""
|
|
|
|
Ensures that a Model subclass contains a default manager and sets the
|
|
|
|
_default_manager attribute on the class. Also sets up the _base_manager
|
|
|
|
points to a plain Manager instance (which could be the same as
|
|
|
|
_default_manager if it's not a subclass of Manager).
|
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
cls = sender
|
2009-03-15 11:41:33 +08:00
|
|
|
if cls._meta.abstract:
|
|
|
|
return
|
|
|
|
if not getattr(cls, '_default_manager', None):
|
2006-05-02 09:31:56 +08:00
|
|
|
# Create the default manager, if needed.
|
2006-06-15 22:05:33 +08:00
|
|
|
try:
|
|
|
|
cls._meta.get_field('objects')
|
2010-01-11 02:36:20 +08:00
|
|
|
raise ValueError("Model %s must specify a custom Manager, because it has a field named 'objects'" % cls.__name__)
|
2006-06-15 22:05:33 +08:00
|
|
|
except FieldDoesNotExist:
|
|
|
|
pass
|
2006-05-02 09:31:56 +08:00
|
|
|
cls.add_to_class('objects', Manager())
|
2009-03-15 11:41:33 +08:00
|
|
|
cls._base_manager = cls.objects
|
|
|
|
elif not getattr(cls, '_base_manager', None):
|
|
|
|
default_mgr = cls._default_manager.__class__
|
|
|
|
if (default_mgr is Manager or
|
|
|
|
getattr(default_mgr, "use_for_related_fields", False)):
|
|
|
|
cls._base_manager = cls._default_manager
|
|
|
|
else:
|
|
|
|
# Default manager isn't a plain Manager class, or a suitable
|
|
|
|
# replacement, so we walk up the base class hierarchy until we hit
|
|
|
|
# something appropriate.
|
|
|
|
for base_class in default_mgr.mro()[1:]:
|
|
|
|
if (base_class is Manager or
|
|
|
|
getattr(base_class, "use_for_related_fields", False)):
|
|
|
|
cls.add_to_class('_base_manager', base_class())
|
|
|
|
return
|
|
|
|
raise AssertionError("Should never get here. Please report a bug, including your model and model manager setup.")
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.class_prepared.connect(ensure_default_manager)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
class Manager(object):
|
|
|
|
# Tracks each time a Manager instance is created. Used to retain order.
|
|
|
|
creation_counter = 0
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(Manager, self).__init__()
|
2008-09-02 17:04:54 +08:00
|
|
|
self._set_creation_counter()
|
2006-05-02 09:31:56 +08:00
|
|
|
self.model = None
|
2008-09-02 17:04:54 +08:00
|
|
|
self._inherited = False
|
2009-12-22 23:18:51 +08:00
|
|
|
self._db = None
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def contribute_to_class(self, model, name):
|
|
|
|
# TODO: Use weakref because of possible memory leak / circular reference.
|
|
|
|
self.model = model
|
|
|
|
setattr(model, name, ManagerDescriptor(self))
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
if not getattr(model, '_default_manager', None) or self.creation_counter < model._default_manager.creation_counter:
|
2006-05-02 09:31:56 +08:00
|
|
|
model._default_manager = self
|
2009-05-11 18:10:03 +08:00
|
|
|
if model._meta.abstract or (self._inherited and not self.model._meta.proxy):
|
2008-09-02 17:04:54 +08:00
|
|
|
model._meta.abstract_managers.append((self.creation_counter, name,
|
|
|
|
self))
|
2009-03-18 17:47:08 +08:00
|
|
|
else:
|
|
|
|
model._meta.concrete_managers.append((self.creation_counter, name,
|
|
|
|
self))
|
2008-09-02 17:04:54 +08:00
|
|
|
|
|
|
|
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
|
2006-05-02 09:31:56 +08:00
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def _copy_to_model(self, model):
|
|
|
|
"""
|
|
|
|
Makes a copy of the manager and assigns it to 'model', which should be
|
|
|
|
a child of the existing model (used when inheriting a manager from an
|
|
|
|
abstract base class).
|
|
|
|
"""
|
|
|
|
assert issubclass(model, self.model)
|
|
|
|
mgr = copy.copy(self)
|
2008-09-02 17:04:54 +08:00
|
|
|
mgr._set_creation_counter()
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
mgr.model = model
|
2008-09-02 17:04:54 +08:00
|
|
|
mgr._inherited = True
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
return mgr
|
|
|
|
|
2010-01-22 22:30:06 +08:00
|
|
|
def db_manager(self, using):
|
2009-12-22 23:18:51 +08:00
|
|
|
obj = copy.copy(self)
|
2010-01-22 22:30:06 +08:00
|
|
|
obj._db = using
|
2009-12-22 23:18:51 +08:00
|
|
|
return obj
|
|
|
|
|
|
|
|
@property
|
|
|
|
def db(self):
|
2010-01-22 22:30:06 +08:00
|
|
|
return self._db or router.db_for_read(self.model)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
#######################
|
|
|
|
# PROXIES TO QUERYSET #
|
|
|
|
#######################
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2007-01-23 10:11:08 +08:00
|
|
|
def get_empty_query_set(self):
|
2010-01-22 22:30:06 +08:00
|
|
|
return EmptyQuerySet(self.model, using=self._db)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def get_query_set(self):
|
|
|
|
"""Returns a new QuerySet object. Subclasses can override this method
|
2007-08-07 04:27:04 +08:00
|
|
|
to easily customize the behavior of the Manager.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2010-01-22 22:30:06 +08:00
|
|
|
return QuerySet(self.model, using=self._db)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2007-01-23 10:11:08 +08:00
|
|
|
def none(self):
|
|
|
|
return self.get_empty_query_set()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def all(self):
|
|
|
|
return self.get_query_set()
|
|
|
|
|
|
|
|
def count(self):
|
|
|
|
return self.get_query_set().count()
|
|
|
|
|
|
|
|
def dates(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().dates(*args, **kwargs)
|
|
|
|
|
|
|
|
def distinct(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().distinct(*args, **kwargs)
|
|
|
|
|
|
|
|
def extra(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().extra(*args, **kwargs)
|
|
|
|
|
|
|
|
def get(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().get(*args, **kwargs)
|
|
|
|
|
2006-06-28 04:36:25 +08:00
|
|
|
def get_or_create(self, **kwargs):
|
|
|
|
return self.get_query_set().get_or_create(**kwargs)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
|
2006-06-28 04:36:25 +08:00
|
|
|
def create(self, **kwargs):
|
|
|
|
return self.get_query_set().create(**kwargs)
|
2006-06-07 08:09:29 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def filter(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().filter(*args, **kwargs)
|
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
def aggregate(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().aggregate(*args, **kwargs)
|
|
|
|
|
|
|
|
def annotate(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().annotate(*args, **kwargs)
|
|
|
|
|
2006-05-06 08:26:24 +08:00
|
|
|
def complex_filter(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().complex_filter(*args, **kwargs)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def exclude(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().exclude(*args, **kwargs)
|
|
|
|
|
|
|
|
def in_bulk(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().in_bulk(*args, **kwargs)
|
|
|
|
|
|
|
|
def iterator(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().iterator(*args, **kwargs)
|
|
|
|
|
|
|
|
def latest(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().latest(*args, **kwargs)
|
|
|
|
|
|
|
|
def order_by(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().order_by(*args, **kwargs)
|
|
|
|
|
|
|
|
def select_related(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().select_related(*args, **kwargs)
|
|
|
|
|
|
|
|
def values(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().values(*args, **kwargs)
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def values_list(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().values_list(*args, **kwargs)
|
|
|
|
|
|
|
|
def update(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().update(*args, **kwargs)
|
|
|
|
|
|
|
|
def reverse(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().reverse(*args, **kwargs)
|
|
|
|
|
2009-03-19 17:06:04 +08:00
|
|
|
def defer(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().defer(*args, **kwargs)
|
|
|
|
|
|
|
|
def only(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().only(*args, **kwargs)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def using(self, *args, **kwargs):
|
|
|
|
return self.get_query_set().using(*args, **kwargs)
|
|
|
|
|
2009-10-24 08:28:39 +08:00
|
|
|
def exists(self, *args, **kwargs):
|
2009-10-24 08:37:01 +08:00
|
|
|
return self.get_query_set().exists(*args, **kwargs)
|
2009-10-24 08:28:39 +08:00
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def _insert(self, values, **kwargs):
|
|
|
|
return insert_query(self.model, values, **kwargs)
|
|
|
|
|
|
|
|
def _update(self, values, **kwargs):
|
|
|
|
return self.get_query_set()._update(values, **kwargs)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def raw(self, raw_query, params=None, *args, **kwargs):
|
2010-01-22 22:30:06 +08:00
|
|
|
return RawQuerySet(raw_query=raw_query, model=self.model, params=params, using=self._db, *args, **kwargs)
|
2009-12-20 10:46:58 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class ManagerDescriptor(object):
|
|
|
|
# This class ensures managers aren't accessible via model instances.
|
|
|
|
# For example, Poll.objects works, but poll_obj.objects raises AttributeError.
|
|
|
|
def __init__(self, manager):
|
|
|
|
self.manager = manager
|
|
|
|
|
|
|
|
def __get__(self, instance, type=None):
|
|
|
|
if instance != None:
|
2010-01-11 02:36:20 +08:00
|
|
|
raise AttributeError("Manager isn't accessible via %s instances" % type.__name__)
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.manager
|
2007-09-16 02:01:29 +08:00
|
|
|
|
|
|
|
class EmptyManager(Manager):
|
|
|
|
def get_query_set(self):
|
|
|
|
return self.get_empty_query_set()
|