2009-03-19 17:06:04 +08:00
|
|
|
"""
|
|
|
|
The main QuerySet implementation. This provides the public API for the ORM.
|
|
|
|
"""
|
|
|
|
|
2011-03-28 10:11:19 +08:00
|
|
|
import copy
|
2011-10-08 00:06:02 +08:00
|
|
|
import itertools
|
2012-01-03 05:30:47 +08:00
|
|
|
import sys
|
2012-11-02 23:49:29 +08:00
|
|
|
import warnings
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
from django.conf import settings
|
2012-04-12 05:11:22 +08:00
|
|
|
from django.core import exceptions
|
2010-01-22 22:30:06 +08:00
|
|
|
from django.db import connections, router, transaction, IntegrityError
|
2012-09-09 07:51:36 +08:00
|
|
|
from django.db.models.constants import LOOKUP_SEP
|
2011-09-10 03:22:28 +08:00
|
|
|
from django.db.models.fields import AutoField
|
2010-11-05 00:03:05 +08:00
|
|
|
from django.db.models.query_utils import (Q, select_related_descend,
|
2010-11-10 00:46:42 +08:00
|
|
|
deferred_class_factory, InvalidQuery)
|
|
|
|
from django.db.models.deletion import Collector
|
2011-09-11 06:31:38 +08:00
|
|
|
from django.db.models import sql
|
2011-09-10 03:22:28 +08:00
|
|
|
from django.utils.functional import partition
|
2012-07-20 18:18:38 +08:00
|
|
|
from django.utils import six
|
2013-02-10 23:15:49 +08:00
|
|
|
from django.utils import timezone
|
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
|
|
|
# Used to control how many objects are worked with at once in some cases (e.g.
|
|
|
|
# when deleting objects).
|
|
|
|
CHUNK_SIZE = 100
|
|
|
|
ITER_CHUNK_SIZE = CHUNK_SIZE
|
|
|
|
|
2008-10-08 16:38:33 +08:00
|
|
|
# The maximum number of items to display in a QuerySet.__repr__
|
|
|
|
REPR_OUTPUT_SIZE = 20
|
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
# Pull into this namespace for backwards compatibility.
|
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
|
|
|
EmptyResultSet = sql.EmptyResultSet
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-01-12 16:37:19 +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
|
|
|
class QuerySet(object):
|
2008-07-09 05:53:38 +08:00
|
|
|
"""
|
|
|
|
Represents a lazy database lookup for a set of objects.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
def __init__(self, model=None, query=None, using=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
self.model = model
|
2009-12-22 23:18:51 +08:00
|
|
|
self._db = using
|
|
|
|
self.query = query or sql.Query(self.model)
|
2006-05-02 09:31:56 +08:00
|
|
|
self._result_cache = None
|
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
|
|
|
self._iter = None
|
2008-08-23 06:00:28 +08:00
|
|
|
self._sticky_filter = False
|
2010-01-22 22:30:06 +08:00
|
|
|
self._for_write = False
|
2011-10-06 07:14:52 +08:00
|
|
|
self._prefetch_related_lookups = []
|
|
|
|
self._prefetch_done = False
|
2013-01-03 04:42:52 +08:00
|
|
|
self._known_related_objects = {} # {rel_field, {pk: rel_obj}}
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
########################
|
|
|
|
# PYTHON MAGIC METHODS #
|
|
|
|
########################
|
|
|
|
|
2009-06-06 21:35:33 +08:00
|
|
|
def __deepcopy__(self, memo):
|
|
|
|
"""
|
|
|
|
Deep copy of a QuerySet doesn't populate the cache
|
|
|
|
"""
|
|
|
|
obj = self.__class__()
|
2010-04-13 23:18:10 +08:00
|
|
|
for k,v in self.__dict__.items():
|
|
|
|
if k in ('_iter','_result_cache'):
|
|
|
|
obj.__dict__[k] = None
|
|
|
|
else:
|
2011-03-28 10:11:19 +08:00
|
|
|
obj.__dict__[k] = copy.deepcopy(v, memo)
|
2009-06-06 21:35:33 +08:00
|
|
|
return obj
|
|
|
|
|
2008-04-28 22:14:41 +08:00
|
|
|
def __getstate__(self):
|
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Allows the QuerySet to be pickled.
|
2008-04-28 22:14:41 +08:00
|
|
|
"""
|
|
|
|
# Force the cache to be fully populated.
|
|
|
|
len(self)
|
|
|
|
|
|
|
|
obj_dict = self.__dict__.copy()
|
|
|
|
obj_dict['_iter'] = None
|
|
|
|
return obj_dict
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def __repr__(self):
|
2008-10-08 16:38:33 +08:00
|
|
|
data = list(self[:REPR_OUTPUT_SIZE + 1])
|
|
|
|
if len(data) > REPR_OUTPUT_SIZE:
|
|
|
|
data[-1] = "...(remaining elements truncated)..."
|
|
|
|
return repr(data)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __len__(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
|
|
|
# Since __len__ is called quite frequently (for example, as part of
|
|
|
|
# list(qs), we make some effort here to be as efficient as possible
|
2008-07-09 05:53:38 +08:00
|
|
|
# whilst not messing up any existing iterators against the 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
|
|
|
if self._result_cache is None:
|
|
|
|
if self._iter:
|
2008-04-28 22:14:41 +08:00
|
|
|
self._result_cache = list(self._iter)
|
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
|
|
|
else:
|
|
|
|
self._result_cache = list(self.iterator())
|
|
|
|
elif self._iter:
|
2011-02-12 13:23:41 +08:00
|
|
|
self._result_cache.extend(self._iter)
|
2011-10-06 07:14:52 +08:00
|
|
|
if self._prefetch_related_lookups and not self._prefetch_done:
|
|
|
|
self._prefetch_related_objects()
|
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 len(self._result_cache)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
2011-10-06 07:14:52 +08:00
|
|
|
if self._prefetch_related_lookups and not self._prefetch_done:
|
|
|
|
# We need all the results in order to be able to do the prefetch
|
|
|
|
# in one go. To minimize code duplication, we use the __len__
|
|
|
|
# code path which also forces this, and also does the prefetch
|
|
|
|
len(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 self._result_cache is None:
|
2013-03-20 17:41:53 +08:00
|
|
|
self._iter = self.iterator()
|
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
|
|
|
self._result_cache = []
|
|
|
|
if self._iter:
|
|
|
|
return self._result_iter()
|
|
|
|
# Python's list iterator is better than our version when we're just
|
|
|
|
# iterating over the cache.
|
|
|
|
return iter(self._result_cache)
|
|
|
|
|
|
|
|
def _result_iter(self):
|
|
|
|
pos = 0
|
|
|
|
while 1:
|
|
|
|
upper = len(self._result_cache)
|
|
|
|
while pos < upper:
|
|
|
|
yield self._result_cache[pos]
|
|
|
|
pos = pos + 1
|
|
|
|
if not self._iter:
|
|
|
|
raise StopIteration
|
|
|
|
if len(self._result_cache) <= pos:
|
|
|
|
self._fill_cache()
|
|
|
|
|
2012-08-08 20:52:21 +08:00
|
|
|
def __bool__(self):
|
2011-10-06 07:14:52 +08:00
|
|
|
if self._prefetch_related_lookups and not self._prefetch_done:
|
|
|
|
# We need all the results in order to be able to do the prefetch
|
|
|
|
# in one go. To minimize code duplication, we use the __len__
|
|
|
|
# code path which also forces this, and also does the prefetch
|
|
|
|
len(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 self._result_cache is not None:
|
|
|
|
return bool(self._result_cache)
|
|
|
|
try:
|
2012-05-11 02:14:04 +08:00
|
|
|
next(iter(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
|
|
|
except StopIteration:
|
|
|
|
return False
|
|
|
|
return True
|
2012-11-04 04:43:11 +08:00
|
|
|
|
|
|
|
def __nonzero__(self): # Python 2 compatibility
|
|
|
|
return type(self).__bool__(self)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-12-10 00:07:21 +08:00
|
|
|
def __contains__(self, val):
|
|
|
|
# The 'in' operator works without this method, due to __iter__. This
|
|
|
|
# implementation exists only to shortcut the creation of Model
|
|
|
|
# instances, by bailing out early if we find a matching element.
|
|
|
|
pos = 0
|
|
|
|
if self._result_cache is not None:
|
|
|
|
if val in self._result_cache:
|
|
|
|
return True
|
|
|
|
elif self._iter is None:
|
|
|
|
# iterator is exhausted, so we have our answer
|
|
|
|
return False
|
|
|
|
# remember not to check these again:
|
|
|
|
pos = len(self._result_cache)
|
|
|
|
else:
|
|
|
|
# We need to start filling the result cache out. The following
|
|
|
|
# ensures that self._iter is not None and self._result_cache is not
|
|
|
|
# None
|
|
|
|
it = iter(self)
|
|
|
|
|
|
|
|
# Carry on, one result at a time.
|
|
|
|
while True:
|
|
|
|
if len(self._result_cache) <= pos:
|
|
|
|
self._fill_cache(num=1)
|
|
|
|
if self._iter is None:
|
|
|
|
# we ran out of items
|
|
|
|
return False
|
|
|
|
if self._result_cache[pos] == val:
|
|
|
|
return True
|
|
|
|
pos += 1
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def __getitem__(self, k):
|
2008-07-09 05:53:38 +08:00
|
|
|
"""
|
|
|
|
Retrieves an item or slice from the set of results.
|
|
|
|
"""
|
2012-07-20 18:45:19 +08:00
|
|
|
if not isinstance(k, (slice,) + six.integer_types):
|
2007-03-22 11:00:12 +08:00
|
|
|
raise TypeError
|
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
|
|
|
assert ((not isinstance(k, slice) and (k >= 0))
|
|
|
|
or (isinstance(k, slice) and (k.start is None or k.start >= 0)
|
|
|
|
and (k.stop is None or k.stop >= 0))), \
|
|
|
|
"Negative indexing is not supported."
|
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
|
|
|
if self._result_cache is not None:
|
|
|
|
if self._iter is not None:
|
|
|
|
# The result cache has only been partially populated, so we may
|
|
|
|
# need to fill it out a bit more.
|
|
|
|
if isinstance(k, slice):
|
|
|
|
if k.stop is not None:
|
|
|
|
# Some people insist on passing in strings here.
|
|
|
|
bound = int(k.stop)
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
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
|
|
|
bound = None
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
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
|
|
|
bound = k + 1
|
|
|
|
if len(self._result_cache) < bound:
|
|
|
|
self._fill_cache(bound - len(self._result_cache))
|
|
|
|
return self._result_cache[k]
|
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
|
|
|
if isinstance(k, slice):
|
|
|
|
qs = self._clone()
|
|
|
|
if k.start is not None:
|
|
|
|
start = int(k.start)
|
2006-05-02 09:31:56 +08:00
|
|
|
else:
|
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
|
|
|
start = None
|
|
|
|
if k.stop is not None:
|
|
|
|
stop = int(k.stop)
|
|
|
|
else:
|
|
|
|
stop = None
|
|
|
|
qs.query.set_limits(start, stop)
|
|
|
|
return k.step and list(qs)[::k.step] or qs
|
2013-01-03 05:46:08 +08:00
|
|
|
|
|
|
|
qs = self._clone()
|
|
|
|
qs.query.set_limits(k, k + 1)
|
|
|
|
return list(qs)[0]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __and__(self, other):
|
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
|
|
|
self._merge_sanity_check(other)
|
2008-06-26 19:42:12 +08:00
|
|
|
if isinstance(other, EmptyQuerySet):
|
2012-10-24 05:04:37 +08:00
|
|
|
return other
|
|
|
|
if isinstance(self, EmptyQuerySet):
|
|
|
|
return 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
|
|
|
combined = self._clone()
|
2013-01-03 04:42:52 +08:00
|
|
|
combined._merge_known_related_objects(other)
|
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
|
|
|
combined.query.combine(other.query, sql.AND)
|
2006-05-02 09:31:56 +08:00
|
|
|
return combined
|
|
|
|
|
|
|
|
def __or__(self, other):
|
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
|
|
|
self._merge_sanity_check(other)
|
2012-10-24 05:04:37 +08:00
|
|
|
if isinstance(self, EmptyQuerySet):
|
|
|
|
return other
|
2008-06-26 19:42:12 +08:00
|
|
|
if isinstance(other, EmptyQuerySet):
|
2012-10-24 05:04:37 +08:00
|
|
|
return self
|
|
|
|
combined = self._clone()
|
2013-01-03 04:42:52 +08:00
|
|
|
combined._merge_known_related_objects(other)
|
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
|
|
|
combined.query.combine(other.query, sql.OR)
|
2006-05-02 09:31:56 +08:00
|
|
|
return combined
|
|
|
|
|
|
|
|
####################################
|
|
|
|
# METHODS THAT DO DATABASE QUERIES #
|
|
|
|
####################################
|
|
|
|
|
|
|
|
def iterator(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
|
|
|
"""
|
|
|
|
An iterator over the results from applying this QuerySet to the
|
|
|
|
database.
|
|
|
|
"""
|
2011-12-06 07:11:43 +08:00
|
|
|
fill_cache = False
|
|
|
|
if connections[self.db].features.supports_select_related:
|
|
|
|
fill_cache = self.query.select_related
|
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 isinstance(fill_cache, dict):
|
|
|
|
requested = fill_cache
|
|
|
|
else:
|
|
|
|
requested = None
|
|
|
|
max_depth = self.query.max_depth
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2012-08-08 22:33:15 +08:00
|
|
|
extra_select = list(self.query.extra_select)
|
|
|
|
aggregate_select = list(self.query.aggregate_select)
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2009-03-19 17:06:04 +08:00
|
|
|
only_load = self.query.get_loaded_field_names()
|
|
|
|
if not fill_cache:
|
|
|
|
fields = self.model._meta.fields
|
|
|
|
|
2009-06-06 14:14:05 +08:00
|
|
|
load_fields = []
|
|
|
|
# If only/defer clauses have been specified,
|
|
|
|
# build the list of fields that are to be loaded.
|
|
|
|
if only_load:
|
|
|
|
for field, model in self.model._meta.get_fields_with_model():
|
|
|
|
if model is None:
|
|
|
|
model = self.model
|
|
|
|
try:
|
|
|
|
if field.name in only_load[model]:
|
|
|
|
# Add a field that has been explicitly included
|
|
|
|
load_fields.append(field.name)
|
|
|
|
except KeyError:
|
|
|
|
# Model wasn't explicitly listed in the only_load table
|
|
|
|
# Therefore, we need to load all fields from this model
|
|
|
|
load_fields.append(field.name)
|
|
|
|
|
2011-07-07 09:12:45 +08:00
|
|
|
index_start = len(extra_select)
|
|
|
|
aggregate_start = index_start + len(load_fields or self.model._meta.fields)
|
|
|
|
|
2009-04-04 11:21:31 +08:00
|
|
|
skip = None
|
|
|
|
if load_fields and not fill_cache:
|
|
|
|
# Some fields have been deferred, so we have to initialise
|
|
|
|
# via keyword arguments.
|
|
|
|
skip = set()
|
|
|
|
init_list = []
|
|
|
|
for field in fields:
|
|
|
|
if field.name not in load_fields:
|
|
|
|
skip.add(field.attname)
|
|
|
|
else:
|
|
|
|
init_list.append(field.attname)
|
|
|
|
model_cls = deferred_class_factory(self.model, skip)
|
|
|
|
|
2013-01-03 04:42:52 +08:00
|
|
|
# Cache db and model outside the loop
|
2010-11-23 21:54:58 +08:00
|
|
|
db = self.db
|
|
|
|
model = self.model
|
|
|
|
compiler = self.query.get_compiler(using=db)
|
2011-10-06 06:56:09 +08:00
|
|
|
if fill_cache:
|
|
|
|
klass_info = get_klass_info(model, max_depth=max_depth,
|
|
|
|
requested=requested, only_load=only_load)
|
2009-12-22 23:18:51 +08:00
|
|
|
for row in compiler.results_iter():
|
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 fill_cache:
|
2011-10-06 06:56:09 +08:00
|
|
|
obj, _ = get_cached_row(row, index_start, db, klass_info,
|
|
|
|
offset=len(aggregate_select))
|
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
|
|
|
else:
|
2012-05-24 19:25:01 +08:00
|
|
|
# Omit aggregates in object creation.
|
|
|
|
row_data = row[index_start:aggregate_start]
|
2009-04-04 11:21:31 +08:00
|
|
|
if skip:
|
|
|
|
obj = model_cls(**dict(zip(init_list, row_data)))
|
2009-03-19 17:06:04 +08:00
|
|
|
else:
|
2012-05-24 19:25:01 +08:00
|
|
|
obj = model(*row_data)
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2010-03-07 15:13:55 +08:00
|
|
|
# Store the source database of the object
|
2010-11-23 21:54:58 +08:00
|
|
|
obj._state.db = db
|
2010-11-19 07:29:58 +08:00
|
|
|
# This object came from the database; it's not being added.
|
|
|
|
obj._state.adding = False
|
2010-03-07 15:13:55 +08:00
|
|
|
|
2010-11-23 21:54:58 +08:00
|
|
|
if extra_select:
|
|
|
|
for i, k in enumerate(extra_select):
|
|
|
|
setattr(obj, k, row[i])
|
2009-01-15 19:06:34 +08:00
|
|
|
|
|
|
|
# Add the aggregates to the model
|
2010-11-23 21:54:58 +08:00
|
|
|
if aggregate_select:
|
|
|
|
for i, aggregate in enumerate(aggregate_select):
|
2012-05-24 19:25:01 +08:00
|
|
|
setattr(obj, aggregate, row[i + aggregate_start])
|
|
|
|
|
2013-01-03 04:42:52 +08:00
|
|
|
# Add the known related objects to the model, if there are any
|
|
|
|
if self._known_related_objects:
|
|
|
|
for field, rel_objs in self._known_related_objects.items():
|
|
|
|
pk = getattr(obj, field.get_attname())
|
|
|
|
try:
|
|
|
|
rel_obj = rel_objs[pk]
|
|
|
|
except KeyError:
|
|
|
|
pass # may happen in qs1 | qs2 scenarios
|
|
|
|
else:
|
|
|
|
setattr(obj, field.name, rel_obj)
|
2009-01-15 19:06:34 +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
|
|
|
yield obj
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
def aggregate(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns a dictionary containing the calculations (aggregation)
|
|
|
|
over the current queryset
|
|
|
|
|
2009-12-20 10:46:58 +08:00
|
|
|
If args is present the expression is passed as a kwarg using
|
2009-01-15 19:06:34 +08:00
|
|
|
the Aggregate object's default alias.
|
|
|
|
"""
|
2011-12-23 04:42:40 +08:00
|
|
|
if self.query.distinct_fields:
|
|
|
|
raise NotImplementedError("aggregate() + distinct(fields) not implemented.")
|
2009-01-15 19:06:34 +08:00
|
|
|
for arg in args:
|
|
|
|
kwargs[arg.default_alias] = arg
|
|
|
|
|
2009-02-08 19:14:07 +08:00
|
|
|
query = self.query.clone()
|
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
for (alias, aggregate_expr) in kwargs.items():
|
2009-02-08 19:14:07 +08:00
|
|
|
query.add_aggregate(aggregate_expr, self.model, alias,
|
2009-01-15 19:06:34 +08:00
|
|
|
is_summary=True)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
return query.get_aggregation(using=self.db)
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def count(self):
|
2007-02-24 04:58:28 +08:00
|
|
|
"""
|
|
|
|
Performs a SELECT COUNT() and returns the number of records as an
|
|
|
|
integer.
|
2007-04-09 23:35:19 +08:00
|
|
|
|
2008-07-17 07:17:29 +08:00
|
|
|
If the QuerySet is already fully cached this simply returns the length
|
|
|
|
of the cached results set to avoid multiple SELECT COUNT(*) calls.
|
2007-02-24 04:58:28 +08:00
|
|
|
"""
|
2008-07-17 07:17:29 +08:00
|
|
|
if self._result_cache is not None and not self._iter:
|
2007-02-24 07:05:25 +08:00
|
|
|
return len(self._result_cache)
|
2007-04-09 23:35:19 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
return self.query.get_count(using=self.db)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def get(self, *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
|
|
|
"""
|
|
|
|
Performs the query and returns a single object matching the given
|
|
|
|
keyword arguments.
|
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
clone = self.filter(*args, **kwargs)
|
2009-12-19 23:02:46 +08:00
|
|
|
if self.query.can_filter():
|
|
|
|
clone = clone.order_by()
|
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
|
|
|
num = len(clone)
|
|
|
|
if num == 1:
|
|
|
|
return clone._result_cache[0]
|
|
|
|
if not num:
|
2012-04-20 19:09:32 +08:00
|
|
|
raise self.model.DoesNotExist(
|
|
|
|
"%s matching query does not exist. "
|
|
|
|
"Lookup parameters were %s" %
|
|
|
|
(self.model._meta.object_name, kwargs))
|
|
|
|
raise self.model.MultipleObjectsReturned(
|
|
|
|
"get() returned more than one %s -- it returned %s! "
|
|
|
|
"Lookup parameters were %s" %
|
|
|
|
(self.model._meta.object_name, num, kwargs))
|
2006-07-01 11:21:32 +08:00
|
|
|
|
2006-06-28 04:36:25 +08:00
|
|
|
def create(self, **kwargs):
|
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Creates a new object with the given kwargs, saving it to the database
|
2006-06-28 04:36:25 +08:00
|
|
|
and returning the created object.
|
|
|
|
"""
|
|
|
|
obj = self.model(**kwargs)
|
2010-01-22 22:30:06 +08:00
|
|
|
self._for_write = True
|
2009-12-22 23:18:51 +08:00
|
|
|
obj.save(force_insert=True, using=self.db)
|
2006-06-28 04:36:25 +08:00
|
|
|
return obj
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-04-29 09:22:05 +08:00
|
|
|
def bulk_create(self, objs, batch_size=None):
|
2011-09-10 03:22:28 +08:00
|
|
|
"""
|
|
|
|
Inserts each of the instances into the database. This does *not* call
|
|
|
|
save() on each of the instances, does not send any pre/post save
|
|
|
|
signals, and does not set the primary key attribute if it is an
|
|
|
|
autoincrement field.
|
|
|
|
"""
|
|
|
|
# So this case is fun. When you bulk insert you don't get the primary
|
|
|
|
# keys back (if it's an autoincrement), so you can't insert into the
|
|
|
|
# child tables which references this. There are two workarounds, 1)
|
|
|
|
# this could be implemented if you didn't have an autoincrement pk,
|
|
|
|
# and 2) you could do it by doing O(n) normal inserts into the parent
|
|
|
|
# tables to get the primary keys back, and then doing a single bulk
|
2012-04-29 09:22:05 +08:00
|
|
|
# insert into the childmost table. Some databases might allow doing
|
|
|
|
# this by using RETURNING clause for the insert query. We're punting
|
|
|
|
# on these for now because they are relatively rare cases.
|
|
|
|
assert batch_size is None or batch_size > 0
|
2011-09-10 03:22:28 +08:00
|
|
|
if self.model._meta.parents:
|
|
|
|
raise ValueError("Can't bulk create an inherited model")
|
|
|
|
if not objs:
|
2011-09-11 05:46:59 +08:00
|
|
|
return objs
|
2011-09-10 03:22:28 +08:00
|
|
|
self._for_write = True
|
|
|
|
connection = connections[self.db]
|
|
|
|
fields = self.model._meta.local_fields
|
2013-03-08 18:35:54 +08:00
|
|
|
with transaction.commit_on_success_unless_managed(using=self.db):
|
2011-12-10 07:16:56 +08:00
|
|
|
if (connection.features.can_combine_inserts_with_and_without_auto_increment_pk
|
|
|
|
and self.model._meta.has_auto_field):
|
2012-04-29 09:22:05 +08:00
|
|
|
self._batched_insert(objs, fields, batch_size)
|
2011-12-10 07:16:56 +08:00
|
|
|
else:
|
|
|
|
objs_with_pk, objs_without_pk = partition(lambda o: o.pk is None, objs)
|
|
|
|
if objs_with_pk:
|
2012-04-29 09:22:05 +08:00
|
|
|
self._batched_insert(objs_with_pk, fields, batch_size)
|
2011-12-10 07:16:56 +08:00
|
|
|
if objs_without_pk:
|
2012-04-29 09:22:05 +08:00
|
|
|
fields= [f for f in fields if not isinstance(f, AutoField)]
|
|
|
|
self._batched_insert(objs_without_pk, fields, batch_size)
|
2011-12-10 07:16:56 +08:00
|
|
|
|
2011-09-11 05:44:57 +08:00
|
|
|
return objs
|
2011-09-10 03:22:28 +08:00
|
|
|
|
2006-06-07 08:09:29 +08:00
|
|
|
def get_or_create(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Looks up an object with the given kwargs, creating one if necessary.
|
|
|
|
Returns a tuple of (object, created), where created is a boolean
|
|
|
|
specifying whether an object was created.
|
|
|
|
"""
|
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
|
|
|
assert kwargs, \
|
|
|
|
'get_or_create() must be passed at least one keyword argument'
|
2006-06-07 08:09:29 +08:00
|
|
|
defaults = kwargs.pop('defaults', {})
|
2011-01-08 21:27:30 +08:00
|
|
|
lookup = kwargs.copy()
|
|
|
|
for f in self.model._meta.fields:
|
2011-01-10 21:44:35 +08:00
|
|
|
if f.attname in lookup:
|
|
|
|
lookup[f.name] = lookup.pop(f.attname)
|
2006-06-07 08:09:29 +08:00
|
|
|
try:
|
2012-09-20 01:15:12 +08:00
|
|
|
self._for_write = True
|
2011-01-08 21:27:30 +08:00
|
|
|
return self.get(**lookup), False
|
2006-06-07 08:09:29 +08:00
|
|
|
except self.model.DoesNotExist:
|
2008-03-18 22:08:40 +08:00
|
|
|
try:
|
|
|
|
params = dict([(k, v) for k, v in kwargs.items() if '__' not in k])
|
|
|
|
params.update(defaults)
|
|
|
|
obj = self.model(**params)
|
2009-12-22 23:18:51 +08:00
|
|
|
sid = transaction.savepoint(using=self.db)
|
|
|
|
obj.save(force_insert=True, using=self.db)
|
|
|
|
transaction.savepoint_commit(sid, using=self.db)
|
2008-03-18 22:08:40 +08:00
|
|
|
return obj, True
|
2012-04-29 00:09:37 +08:00
|
|
|
except IntegrityError as e:
|
2009-12-22 23:18:51 +08:00
|
|
|
transaction.savepoint_rollback(sid, using=self.db)
|
2012-01-03 05:30:47 +08:00
|
|
|
exc_info = sys.exc_info()
|
2008-08-16 07:29:55 +08:00
|
|
|
try:
|
2011-01-08 21:27:30 +08:00
|
|
|
return self.get(**lookup), False
|
2008-08-16 07:29:55 +08:00
|
|
|
except self.model.DoesNotExist:
|
2012-01-03 05:30:47 +08:00
|
|
|
# Re-raise the IntegrityError with its original traceback.
|
2012-08-12 05:43:45 +08:00
|
|
|
six.reraise(*exc_info)
|
2006-06-07 08:09:29 +08:00
|
|
|
|
2013-01-12 16:37:19 +08:00
|
|
|
def _earliest_or_latest(self, field_name=None, direction="-"):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2013-01-12 16:37:19 +08:00
|
|
|
Returns the latest object, according to the model's
|
|
|
|
'get_latest_by' option or optional given field_name.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2013-01-12 16:37:19 +08:00
|
|
|
order_by = field_name or getattr(self.model._meta, 'get_latest_by')
|
|
|
|
assert bool(order_by), "earliest() and latest() require either a "\
|
|
|
|
"field_name parameter or 'get_latest_by' in the model"
|
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
|
|
|
assert self.query.can_filter(), \
|
2013-01-12 16:37:19 +08:00
|
|
|
"Cannot change a query once a slice has been taken."
|
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
|
|
|
obj = self._clone()
|
|
|
|
obj.query.set_limits(high=1)
|
2013-02-11 02:52:52 +08:00
|
|
|
obj.query.clear_ordering(force_empty=True)
|
2013-01-12 16:37:19 +08:00
|
|
|
obj.query.add_ordering('%s%s' % (direction, order_by))
|
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 obj.get()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-01-12 16:37:19 +08:00
|
|
|
def earliest(self, field_name=None):
|
|
|
|
return self._earliest_or_latest(field_name=field_name, direction="")
|
|
|
|
|
|
|
|
def latest(self, field_name=None):
|
|
|
|
return self._earliest_or_latest(field_name=field_name, direction="-")
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def in_bulk(self, id_list):
|
|
|
|
"""
|
|
|
|
Returns a dictionary mapping each of the given IDs to the object with
|
|
|
|
that ID.
|
|
|
|
"""
|
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
|
|
|
assert self.query.can_filter(), \
|
2006-05-02 09:31:56 +08:00
|
|
|
"Cannot use 'limit' or 'offset' with in_bulk"
|
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 id_list:
|
2006-05-02 09:31:56 +08:00
|
|
|
return {}
|
2012-08-25 07:08:16 +08:00
|
|
|
qs = self.filter(pk__in=id_list).order_by()
|
2012-02-29 03:34:04 +08:00
|
|
|
return dict([(obj._get_pk_val(), obj) for obj in qs])
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
"""
|
|
|
|
Deletes the records in the current 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
|
|
|
assert self.query.can_filter(), \
|
|
|
|
"Cannot use 'limit' or 'offset' with delete."
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
del_query = self._clone()
|
|
|
|
|
2010-01-22 22:30:06 +08:00
|
|
|
# The delete is actually 2 queries - one to find related objects,
|
|
|
|
# and one to delete. Make sure that the discovery of related
|
|
|
|
# objects is performed on the same database as the deletion.
|
|
|
|
del_query._for_write = 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
|
|
|
# Disable non-supported fields.
|
2011-04-21 04:42:07 +08:00
|
|
|
del_query.query.select_for_update = False
|
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
|
|
|
del_query.query.select_related = False
|
2013-02-11 01:49:28 +08:00
|
|
|
del_query.query.clear_ordering(force_empty=True)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
collector = Collector(using=del_query.db)
|
|
|
|
collector.collect(del_query)
|
|
|
|
collector.delete()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Clear the result cache, in case this QuerySet gets reused.
|
|
|
|
self._result_cache = None
|
|
|
|
delete.alters_data = True
|
|
|
|
|
2012-09-20 23:51:30 +08:00
|
|
|
def _raw_delete(self, using):
|
|
|
|
"""
|
|
|
|
Deletes objects found from the given queryset in single direct SQL
|
|
|
|
query. No signals are sent, and there is no protection for cascades.
|
|
|
|
"""
|
|
|
|
sql.DeleteQuery(self.model).delete_qs(self, using)
|
|
|
|
_raw_delete.alters_data = 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
|
|
|
def update(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Updates all elements in the current QuerySet, setting all the given
|
|
|
|
fields to the appropriate values.
|
|
|
|
"""
|
2008-06-10 00:17:54 +08:00
|
|
|
assert self.query.can_filter(), \
|
|
|
|
"Cannot update a query once a slice has been taken."
|
2010-01-22 22:30:06 +08:00
|
|
|
self._for_write = 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
|
|
|
query = self.query.clone(sql.UpdateQuery)
|
|
|
|
query.add_update_values(kwargs)
|
2013-03-08 18:35:54 +08:00
|
|
|
with transaction.commit_on_success_unless_managed(using=self.db):
|
2009-12-22 23:18:51 +08:00
|
|
|
rows = query.get_compiler(self.db).execute_sql(None)
|
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
|
|
|
self._result_cache = None
|
2008-08-10 01:19:23 +08:00
|
|
|
return rows
|
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
|
|
|
update.alters_data = True
|
|
|
|
|
|
|
|
def _update(self, values):
|
|
|
|
"""
|
|
|
|
A version of update that accepts field objects instead of field names.
|
|
|
|
Used primarily for model saving and not intended for use by general
|
|
|
|
code (it requires too much poking around at model internals to be
|
|
|
|
useful at that level).
|
|
|
|
"""
|
2008-06-10 00:17:54 +08:00
|
|
|
assert self.query.can_filter(), \
|
|
|
|
"Cannot update a query once a slice has been taken."
|
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
|
|
|
query = self.query.clone(sql.UpdateQuery)
|
|
|
|
query.add_update_fields(values)
|
|
|
|
self._result_cache = None
|
2009-12-22 23:18:51 +08:00
|
|
|
return query.get_compiler(self.db).execute_sql(None)
|
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
|
|
|
_update.alters_data = True
|
|
|
|
|
2009-10-24 08:28:39 +08:00
|
|
|
def exists(self):
|
|
|
|
if self._result_cache is None:
|
2009-12-22 23:18:51 +08:00
|
|
|
return self.query.has_results(using=self.db)
|
2009-10-24 08:28:39 +08:00
|
|
|
return bool(self._result_cache)
|
|
|
|
|
2011-10-06 07:14:52 +08:00
|
|
|
def _prefetch_related_objects(self):
|
|
|
|
# This method can only be called once the result cache has been filled.
|
|
|
|
prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
|
|
|
|
self._prefetch_done = True
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
##################################################
|
|
|
|
# PUBLIC METHODS THAT RETURN A QUERYSET SUBCLASS #
|
|
|
|
##################################################
|
|
|
|
|
|
|
|
def values(self, *fields):
|
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 self._clone(klass=ValuesQuerySet, setup=True, _fields=fields)
|
|
|
|
|
|
|
|
def values_list(self, *fields, **kwargs):
|
|
|
|
flat = kwargs.pop('flat', False)
|
|
|
|
if kwargs:
|
|
|
|
raise TypeError('Unexpected keyword arguments to values_list: %s'
|
2012-08-08 22:33:15 +08:00
|
|
|
% (list(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
|
|
|
if flat and len(fields) > 1:
|
|
|
|
raise TypeError("'flat' is not valid when values_list is called with more than one field.")
|
|
|
|
return self._clone(klass=ValuesListQuerySet, setup=True, flat=flat,
|
|
|
|
_fields=fields)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def dates(self, field_name, kind, order='ASC'):
|
|
|
|
"""
|
2013-02-10 23:15:49 +08:00
|
|
|
Returns a list of date objects representing all available dates for
|
2008-07-09 05:53:38 +08:00
|
|
|
the given field_name, scoped to 'kind'.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2013-02-10 23:15:49 +08:00
|
|
|
assert kind in ("year", "month", "day"), \
|
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
|
|
|
"'kind' must be one of 'year', 'month' or 'day'."
|
|
|
|
assert order in ('ASC', 'DESC'), \
|
|
|
|
"'order' must be either 'ASC' or 'DESC'."
|
2008-08-21 06:38:15 +08:00
|
|
|
return self._clone(klass=DateQuerySet, setup=True,
|
|
|
|
_field_name=field_name, _kind=kind, _order=order)
|
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
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
def datetimes(self, field_name, kind, order='ASC', tzinfo=None):
|
|
|
|
"""
|
|
|
|
Returns a list of datetime objects representing all available
|
|
|
|
datetimes for the given field_name, scoped to 'kind'.
|
|
|
|
"""
|
|
|
|
assert kind in ("year", "month", "day", "hour", "minute", "second"), \
|
|
|
|
"'kind' must be one of 'year', 'month', 'day', 'hour', 'minute' or 'second'."
|
|
|
|
assert order in ('ASC', 'DESC'), \
|
|
|
|
"'order' must be either 'ASC' or 'DESC'."
|
|
|
|
if settings.USE_TZ:
|
|
|
|
if tzinfo is None:
|
|
|
|
tzinfo = timezone.get_current_timezone()
|
|
|
|
else:
|
|
|
|
tzinfo = None
|
|
|
|
return self._clone(klass=DateTimeQuerySet, setup=True,
|
|
|
|
_field_name=field_name, _kind=kind, _order=order, _tzinfo=tzinfo)
|
|
|
|
|
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 none(self):
|
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Returns an empty 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
|
|
|
"""
|
2012-10-24 05:04:37 +08:00
|
|
|
clone = self._clone()
|
|
|
|
clone.query.set_empty()
|
|
|
|
return clone
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
##################################################################
|
|
|
|
# PUBLIC METHODS THAT ALTER ATTRIBUTES AND RETURN A NEW 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
|
|
|
def all(self):
|
|
|
|
"""
|
|
|
|
Returns a new QuerySet that is a copy of the current one. This allows a
|
|
|
|
QuerySet to proxy for a model manager in some cases.
|
|
|
|
"""
|
|
|
|
return self._clone()
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def filter(self, *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
|
|
|
"""
|
|
|
|
Returns a new QuerySet instance with the args ANDed to the existing
|
|
|
|
set.
|
|
|
|
"""
|
|
|
|
return self._filter_or_exclude(False, *args, **kwargs)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def exclude(self, *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
|
|
|
"""
|
|
|
|
Returns a new QuerySet instance with NOT (args) ANDed to the existing
|
|
|
|
set.
|
|
|
|
"""
|
|
|
|
return self._filter_or_exclude(True, *args, **kwargs)
|
|
|
|
|
|
|
|
def _filter_or_exclude(self, negate, *args, **kwargs):
|
|
|
|
if args or kwargs:
|
|
|
|
assert self.query.can_filter(), \
|
|
|
|
"Cannot filter a query once a slice has been taken."
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
clone = self._clone()
|
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 negate:
|
|
|
|
clone.query.add_q(~Q(*args, **kwargs))
|
|
|
|
else:
|
|
|
|
clone.query.add_q(Q(*args, **kwargs))
|
2006-05-02 09:31:56 +08:00
|
|
|
return clone
|
|
|
|
|
2006-05-06 08:26:24 +08:00
|
|
|
def complex_filter(self, filter_obj):
|
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
|
|
|
"""
|
|
|
|
Returns a new QuerySet instance with filter_obj added to the filters.
|
2008-07-09 05:53:38 +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
|
|
|
filter_obj can be a Q object (or anything with an add_to_query()
|
|
|
|
method) or a dictionary of keyword lookup arguments.
|
|
|
|
|
|
|
|
This exists to support framework features such as 'limit_choices_to',
|
|
|
|
and usually it will be more natural to use other methods.
|
|
|
|
"""
|
|
|
|
if isinstance(filter_obj, Q) or hasattr(filter_obj, 'add_to_query'):
|
2008-06-26 21:25:59 +08:00
|
|
|
clone = self._clone()
|
|
|
|
clone.query.add_q(filter_obj)
|
|
|
|
return clone
|
2006-05-06 08:26:24 +08:00
|
|
|
else:
|
2006-05-27 07:41:43 +08:00
|
|
|
return self._filter_or_exclude(None, **filter_obj)
|
2006-05-06 08:26:24 +08:00
|
|
|
|
2011-04-21 04:42:07 +08:00
|
|
|
def select_for_update(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns a new QuerySet instance that will select objects with a
|
|
|
|
FOR UPDATE lock.
|
|
|
|
"""
|
|
|
|
# Default to false for nowait
|
|
|
|
nowait = kwargs.pop('nowait', False)
|
|
|
|
obj = self._clone()
|
|
|
|
obj.query.select_for_update = True
|
|
|
|
obj.query.select_for_update_nowait = nowait
|
|
|
|
return obj
|
|
|
|
|
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 select_related(self, *fields, **kwargs):
|
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Returns a new QuerySet instance that will select related objects.
|
|
|
|
|
|
|
|
If fields are specified, they must be ForeignKey fields and only those
|
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
|
|
|
related objects are included in the selection.
|
|
|
|
"""
|
2012-11-02 23:49:29 +08:00
|
|
|
if 'depth' in kwargs:
|
|
|
|
warnings.warn('The "depth" keyword argument has been deprecated.\n'
|
2012-12-26 04:07:27 +08:00
|
|
|
'Use related field names instead.', DeprecationWarning, stacklevel=2)
|
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
|
|
|
depth = kwargs.pop('depth', 0)
|
|
|
|
if kwargs:
|
|
|
|
raise TypeError('Unexpected keyword arguments to select_related: %s'
|
2012-08-08 22:33:15 +08:00
|
|
|
% (list(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
|
|
|
obj = self._clone()
|
|
|
|
if fields:
|
|
|
|
if depth:
|
|
|
|
raise TypeError('Cannot pass both "depth" and fields to select_related()')
|
|
|
|
obj.query.add_select_related(fields)
|
|
|
|
else:
|
|
|
|
obj.query.select_related = True
|
|
|
|
if depth:
|
|
|
|
obj.query.max_depth = depth
|
|
|
|
return obj
|
|
|
|
|
2011-10-06 07:14:52 +08:00
|
|
|
def prefetch_related(self, *lookups):
|
|
|
|
"""
|
|
|
|
Returns a new QuerySet instance that will prefetch the specified
|
|
|
|
Many-To-One and Many-To-Many related objects when the QuerySet is
|
|
|
|
evaluated.
|
|
|
|
|
|
|
|
When prefetch_related() is called more than once, the list of lookups to
|
|
|
|
prefetch is appended to. If prefetch_related(None) is called, the
|
|
|
|
the list is cleared.
|
|
|
|
"""
|
|
|
|
clone = self._clone()
|
|
|
|
if lookups == (None,):
|
|
|
|
clone._prefetch_related_lookups = []
|
|
|
|
else:
|
|
|
|
clone._prefetch_related_lookups.extend(lookups)
|
|
|
|
return clone
|
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
def annotate(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Return a query set in which the returned objects have been annotated
|
|
|
|
with data aggregated from related fields.
|
|
|
|
"""
|
|
|
|
for arg in args:
|
2010-10-10 04:07:48 +08:00
|
|
|
if arg.default_alias in kwargs:
|
2011-01-16 16:03:25 +08:00
|
|
|
raise ValueError("The named annotation '%s' conflicts with the "
|
2010-10-10 04:07:48 +08:00
|
|
|
"default name for another annotation."
|
|
|
|
% arg.default_alias)
|
2009-01-15 19:06:34 +08:00
|
|
|
kwargs[arg.default_alias] = arg
|
|
|
|
|
2011-01-16 16:03:25 +08:00
|
|
|
names = getattr(self, '_fields', None)
|
|
|
|
if names is None:
|
|
|
|
names = set(self.model._meta.get_all_field_names())
|
2010-10-10 04:07:48 +08:00
|
|
|
for aggregate in kwargs:
|
|
|
|
if aggregate in names:
|
2011-01-16 16:03:25 +08:00
|
|
|
raise ValueError("The annotation '%s' conflicts with a field on "
|
2010-10-10 04:07:48 +08:00
|
|
|
"the model." % aggregate)
|
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
obj = self._clone()
|
|
|
|
|
2012-08-08 22:33:15 +08:00
|
|
|
obj._setup_aggregate_query(list(kwargs))
|
2009-01-15 19:06:34 +08:00
|
|
|
|
|
|
|
# Add the aggregates to the query
|
|
|
|
for (alias, aggregate_expr) in kwargs.items():
|
|
|
|
obj.query.add_aggregate(aggregate_expr, self.model, alias,
|
|
|
|
is_summary=False)
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def order_by(self, *field_names):
|
2008-07-09 05:53:38 +08:00
|
|
|
"""
|
|
|
|
Returns a new QuerySet instance with the ordering changed.
|
|
|
|
"""
|
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
|
|
|
assert self.query.can_filter(), \
|
2006-05-02 09:31:56 +08:00
|
|
|
"Cannot reorder a query once a slice has been taken."
|
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
|
|
|
obj = self._clone()
|
2013-02-11 02:52:52 +08:00
|
|
|
obj.query.clear_ordering(force_empty=False)
|
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
|
|
|
obj.query.add_ordering(*field_names)
|
|
|
|
return obj
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-12-23 04:42:40 +08:00
|
|
|
def distinct(self, *field_names):
|
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
|
|
|
"""
|
|
|
|
Returns a new QuerySet instance that will select only distinct results.
|
|
|
|
"""
|
2011-12-23 04:42:40 +08:00
|
|
|
assert self.query.can_filter(), \
|
|
|
|
"Cannot create distinct fields once a slice has been taken."
|
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
|
|
|
obj = self._clone()
|
2011-12-23 04:42:40 +08:00
|
|
|
obj.query.add_distinct_fields(*field_names)
|
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 obj
|
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 extra(self, select=None, where=None, params=None, tables=None,
|
2008-07-09 05:53:38 +08:00
|
|
|
order_by=None, select_params=None):
|
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
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Adds extra SQL fragments to the query.
|
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
|
|
|
"""
|
|
|
|
assert self.query.can_filter(), \
|
2006-05-02 09:31:56 +08:00
|
|
|
"Cannot change a query once a slice has been taken"
|
|
|
|
clone = self._clone()
|
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
|
|
|
clone.query.add_extra(select, select_params, where, params, tables, order_by)
|
|
|
|
return clone
|
|
|
|
|
|
|
|
def reverse(self):
|
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Reverses the ordering of the 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
|
|
|
"""
|
|
|
|
clone = self._clone()
|
|
|
|
clone.query.standard_ordering = not clone.query.standard_ordering
|
2006-05-02 09:31:56 +08:00
|
|
|
return clone
|
|
|
|
|
2009-03-19 17:06:04 +08:00
|
|
|
def defer(self, *fields):
|
|
|
|
"""
|
|
|
|
Defers the loading of data for certain fields until they are accessed.
|
|
|
|
The set of fields to defer is added to any existing set of deferred
|
|
|
|
fields. The only exception to this is if None is passed in as the only
|
|
|
|
parameter, in which case all deferrals are removed (None acts as a
|
|
|
|
reset option).
|
|
|
|
"""
|
|
|
|
clone = self._clone()
|
|
|
|
if fields == (None,):
|
|
|
|
clone.query.clear_deferred_loading()
|
|
|
|
else:
|
|
|
|
clone.query.add_deferred_loading(fields)
|
|
|
|
return clone
|
|
|
|
|
|
|
|
def only(self, *fields):
|
|
|
|
"""
|
|
|
|
Essentially, the opposite of defer. Only the fields passed into this
|
|
|
|
method and that are not already specified as deferred are loaded
|
|
|
|
immediately when the queryset is evaluated.
|
|
|
|
"""
|
2009-03-19 17:43:45 +08:00
|
|
|
if fields == (None,):
|
2009-03-19 17:06:04 +08:00
|
|
|
# Can only pass None to defer(), not only(), as the rest option.
|
|
|
|
# That won't stop people trying to do this, so let's be explicit.
|
|
|
|
raise TypeError("Cannot pass None as an argument to only().")
|
|
|
|
clone = self._clone()
|
|
|
|
clone.query.add_immediate_loading(fields)
|
|
|
|
return clone
|
2009-05-19 20:44:17 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def using(self, alias):
|
|
|
|
"""
|
2011-04-12 04:27:53 +08:00
|
|
|
Selects which database this QuerySet should excecute its query against.
|
2009-12-22 23:18:51 +08:00
|
|
|
"""
|
|
|
|
clone = self._clone()
|
|
|
|
clone._db = alias
|
|
|
|
return clone
|
|
|
|
|
2009-04-23 06:16:19 +08:00
|
|
|
###################################
|
|
|
|
# PUBLIC INTROSPECTION ATTRIBUTES #
|
|
|
|
###################################
|
2009-03-19 17:06:04 +08:00
|
|
|
|
2009-04-23 06:16:19 +08:00
|
|
|
def ordered(self):
|
|
|
|
"""
|
|
|
|
Returns True if the QuerySet is ordered -- i.e. has an order_by()
|
|
|
|
clause or a default ordering on the model.
|
|
|
|
"""
|
|
|
|
if self.query.extra_order_by or self.query.order_by:
|
|
|
|
return True
|
|
|
|
elif self.query.default_ordering and self.query.model._meta.ordering:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
ordered = property(ordered)
|
2009-05-19 20:44:17 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
@property
|
|
|
|
def db(self):
|
|
|
|
"Return the database that will be used if this query is executed now"
|
2010-01-22 22:30:06 +08:00
|
|
|
if self._for_write:
|
|
|
|
return self._db or router.db_for_write(self.model)
|
|
|
|
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
|
|
|
###################
|
|
|
|
# PRIVATE METHODS #
|
|
|
|
###################
|
2012-04-29 09:22:05 +08:00
|
|
|
def _batched_insert(self, objs, fields, batch_size):
|
|
|
|
"""
|
|
|
|
A little helper method for bulk_insert to insert the bulk one batch
|
|
|
|
at a time. Inserts recursively a batch from the front of the bulk and
|
|
|
|
then _batched_insert() the remaining objects again.
|
|
|
|
"""
|
|
|
|
if not objs:
|
|
|
|
return
|
|
|
|
ops = connections[self.db].ops
|
|
|
|
batch_size = (batch_size or max(ops.bulk_batch_size(fields, objs), 1))
|
|
|
|
for batch in [objs[i:i+batch_size]
|
|
|
|
for i in range(0, len(objs), batch_size)]:
|
|
|
|
self.model._base_manager._insert(batch, fields=fields,
|
|
|
|
using=self.db)
|
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 _clone(self, klass=None, setup=False, **kwargs):
|
2006-05-02 09:31:56 +08:00
|
|
|
if klass is None:
|
|
|
|
klass = self.__class__
|
2008-08-23 06:00:28 +08:00
|
|
|
query = self.query.clone()
|
|
|
|
if self._sticky_filter:
|
|
|
|
query.filter_is_sticky = True
|
2010-01-22 22:30:06 +08:00
|
|
|
c = klass(model=self.model, query=query, using=self._db)
|
|
|
|
c._for_write = self._for_write
|
2011-10-06 07:14:52 +08:00
|
|
|
c._prefetch_related_lookups = self._prefetch_related_lookups[:]
|
2013-01-03 04:42:52 +08:00
|
|
|
c._known_related_objects = self._known_related_objects
|
2006-05-02 09:31:56 +08:00
|
|
|
c.__dict__.update(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
|
|
|
if setup and hasattr(c, '_setup_query'):
|
|
|
|
c._setup_query()
|
2006-05-02 09:31:56 +08:00
|
|
|
return c
|
|
|
|
|
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 _fill_cache(self, num=None):
|
|
|
|
"""
|
|
|
|
Fills the result cache with 'num' more entries (or until the results
|
|
|
|
iterator is exhausted).
|
|
|
|
"""
|
|
|
|
if self._iter:
|
|
|
|
try:
|
|
|
|
for i in range(num or ITER_CHUNK_SIZE):
|
2012-05-11 02:14:04 +08:00
|
|
|
self._result_cache.append(next(self._iter))
|
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
|
|
|
except StopIteration:
|
|
|
|
self._iter = None
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-23 06:00:28 +08:00
|
|
|
def _next_is_sticky(self):
|
|
|
|
"""
|
|
|
|
Indicates that the next filter call and the one following that should
|
|
|
|
be treated as a single filter. This is only important when it comes to
|
|
|
|
determining when to reuse tables for many-to-many filters. Required so
|
|
|
|
that we can filter naturally on the results of related managers.
|
2008-08-24 02:34:32 +08:00
|
|
|
|
|
|
|
This doesn't return a clone of the current QuerySet (it returns
|
|
|
|
"self"). The method is only used internally and should be immediately
|
|
|
|
followed by a filter() that does create a clone.
|
2008-08-23 06:00:28 +08:00
|
|
|
"""
|
2008-08-24 02:34:32 +08:00
|
|
|
self._sticky_filter = True
|
|
|
|
return self
|
2008-08-23 06:00:28 +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 _merge_sanity_check(self, other):
|
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Checks that we are merging two comparable QuerySet classes. By default
|
2008-06-26 10:20:45 +08:00
|
|
|
this does nothing, but see the ValuesQuerySet for an example of where
|
|
|
|
it's useful.
|
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
|
|
|
"""
|
2008-06-26 10:20:45 +08:00
|
|
|
pass
|
2007-06-23 22:16:00 +08:00
|
|
|
|
2013-01-03 04:42:52 +08:00
|
|
|
def _merge_known_related_objects(self, other):
|
|
|
|
"""
|
|
|
|
Keep track of all known related objects from either QuerySet instance.
|
|
|
|
"""
|
|
|
|
for field, objects in other._known_related_objects.items():
|
|
|
|
self._known_related_objects.setdefault(field, {}).update(objects)
|
|
|
|
|
2009-02-23 22:47:59 +08:00
|
|
|
def _setup_aggregate_query(self, aggregates):
|
2009-01-15 19:06:34 +08:00
|
|
|
"""
|
|
|
|
Prepare the query for computing a result that contains aggregate annotations.
|
|
|
|
"""
|
|
|
|
opts = self.model._meta
|
2009-02-16 20:29:31 +08:00
|
|
|
if self.query.group_by is None:
|
2009-01-15 19:06:34 +08:00
|
|
|
field_names = [f.attname for f in opts.fields]
|
|
|
|
self.query.add_fields(field_names, False)
|
|
|
|
self.query.set_group_by()
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _prepare(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def _as_sql(self, connection):
|
2009-01-05 19:47:48 +08:00
|
|
|
"""
|
|
|
|
Returns the internal query's SQL and parameters (as a tuple).
|
|
|
|
"""
|
|
|
|
obj = self.values("pk")
|
2010-03-10 23:27:22 +08:00
|
|
|
if obj._db is None or connection == connections[obj._db]:
|
2009-12-22 23:18:51 +08:00
|
|
|
return obj.query.get_compiler(connection=connection).as_nested_sql()
|
|
|
|
raise ValueError("Can't do subqueries with queries on different DBs.")
|
2008-07-09 05:53:38 +08:00
|
|
|
|
2009-01-08 13:16:21 +08:00
|
|
|
# When used as part of a nested query, a queryset will never be an "always
|
|
|
|
# empty" result.
|
|
|
|
value_annotation = True
|
|
|
|
|
2012-10-24 05:04:37 +08:00
|
|
|
class InstanceCheckMeta(type):
|
|
|
|
def __instancecheck__(self, instance):
|
|
|
|
return instance.query.is_empty()
|
|
|
|
|
2013-01-08 12:41:59 +08:00
|
|
|
class EmptyQuerySet(six.with_metaclass(InstanceCheckMeta)):
|
2012-10-24 05:04:37 +08:00
|
|
|
"""
|
|
|
|
Marker class usable for checking if a queryset is empty by .none():
|
|
|
|
isinstance(qs.none(), EmptyQuerySet) -> True
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
raise TypeError("EmptyQuerySet can't be instantiated")
|
2011-10-06 07:14:52 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class ValuesQuerySet(QuerySet):
|
2007-02-10 13:38:38 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(ValuesQuerySet, self).__init__(*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
|
|
|
# select_related isn't supported in values(). (FIXME -#3358)
|
|
|
|
self.query.select_related = False
|
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
|
|
|
# QuerySet.clone() will also set up the _fields attribute with the
|
|
|
|
# names of the model fields to select.
|
2007-02-10 13:38:38 +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 iterator(self):
|
2009-02-16 20:28:37 +08:00
|
|
|
# Purge any extra columns that haven't been explicitly asked for
|
2012-08-08 22:33:15 +08:00
|
|
|
extra_names = list(self.query.extra_select)
|
2009-02-16 20:28:37 +08:00
|
|
|
field_names = self.field_names
|
2012-08-08 22:33:15 +08:00
|
|
|
aggregate_names = list(self.query.aggregate_select)
|
2009-02-16 20:28:37 +08:00
|
|
|
|
|
|
|
names = extra_names + field_names + aggregate_names
|
2009-02-16 20:29:31 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
for row in self.query.get_compiler(self.db).results_iter():
|
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
|
|
|
yield dict(zip(names, row))
|
2007-08-20 09:03:33 +08:00
|
|
|
|
2012-10-25 20:27:43 +08:00
|
|
|
def delete(self):
|
|
|
|
# values().delete() doesn't work currently - make sure it raises an
|
|
|
|
# user friendly error.
|
|
|
|
raise TypeError("Queries with .values() or .values_list() applied "
|
|
|
|
"can't be deleted")
|
|
|
|
|
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 _setup_query(self):
|
|
|
|
"""
|
|
|
|
Constructs the field_names list that the values query will be
|
|
|
|
retrieving.
|
2007-07-28 06:07:42 +08:00
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
Called by the _clone() method after initializing the rest of the
|
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
|
|
|
instance.
|
|
|
|
"""
|
2009-03-19 17:06:04 +08:00
|
|
|
self.query.clear_deferred_loading()
|
2009-01-08 13:49:03 +08:00
|
|
|
self.query.clear_select_fields()
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
if self._fields:
|
2009-02-16 20:28:37 +08:00
|
|
|
self.extra_names = []
|
|
|
|
self.aggregate_names = []
|
2009-04-30 23:40:09 +08:00
|
|
|
if not self.query.extra and not self.query.aggregates:
|
|
|
|
# Short cut - if there are no extra or aggregates, then
|
|
|
|
# the values() clause must be just field names.
|
2009-02-16 20:28:37 +08:00
|
|
|
self.field_names = list(self._fields)
|
2007-05-31 10:22:41 +08:00
|
|
|
else:
|
2009-02-16 20:28:37 +08:00
|
|
|
self.query.default_cols = False
|
|
|
|
self.field_names = []
|
2007-05-31 10:22:41 +08:00
|
|
|
for f in self._fields:
|
2009-04-30 23:40:09 +08:00
|
|
|
# we inspect the full extra_select list since we might
|
|
|
|
# be adding back an extra select item that we hadn't
|
|
|
|
# had selected previously.
|
2010-10-29 12:24:53 +08:00
|
|
|
if f in self.query.extra:
|
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
|
|
|
self.extra_names.append(f)
|
2010-10-29 12:24:53 +08:00
|
|
|
elif f in self.query.aggregate_select:
|
2009-01-15 19:06:34 +08:00
|
|
|
self.aggregate_names.append(f)
|
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
|
|
|
else:
|
2009-02-16 20:28:37 +08:00
|
|
|
self.field_names.append(f)
|
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
|
|
|
else:
|
|
|
|
# Default to all fields.
|
2009-02-16 20:28:37 +08:00
|
|
|
self.extra_names = None
|
|
|
|
self.field_names = [f.attname for f in self.model._meta.fields]
|
|
|
|
self.aggregate_names = None
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
self.query.select = []
|
2009-04-30 23:40:09 +08:00
|
|
|
if self.extra_names is not None:
|
|
|
|
self.query.set_extra_mask(self.extra_names)
|
2010-11-21 10:28:25 +08:00
|
|
|
self.query.add_fields(self.field_names, True)
|
2009-02-23 22:47:59 +08:00
|
|
|
if self.aggregate_names is not None:
|
|
|
|
self.query.set_aggregate_mask(self.aggregate_names)
|
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 _clone(self, klass=None, setup=False, **kwargs):
|
|
|
|
"""
|
|
|
|
Cloning a ValuesQuerySet preserves the current fields.
|
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
c = super(ValuesQuerySet, self)._clone(klass, **kwargs)
|
2009-01-08 13:49:03 +08:00
|
|
|
if not hasattr(c, '_fields'):
|
|
|
|
# Only clone self._fields if _fields wasn't passed into the cloning
|
|
|
|
# call directly.
|
|
|
|
c._fields = self._fields[:]
|
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
|
|
|
c.field_names = self.field_names
|
|
|
|
c.extra_names = self.extra_names
|
2009-01-15 19:06:34 +08:00
|
|
|
c.aggregate_names = self.aggregate_names
|
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 setup and hasattr(c, '_setup_query'):
|
|
|
|
c._setup_query()
|
2006-05-02 09:31:56 +08:00
|
|
|
return c
|
|
|
|
|
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 _merge_sanity_check(self, other):
|
|
|
|
super(ValuesQuerySet, self)._merge_sanity_check(other)
|
|
|
|
if (set(self.extra_names) != set(other.extra_names) or
|
2009-01-15 19:06:34 +08:00
|
|
|
set(self.field_names) != set(other.field_names) or
|
|
|
|
self.aggregate_names != other.aggregate_names):
|
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
|
|
|
raise TypeError("Merging '%s' classes must involve the same values in each case."
|
|
|
|
% self.__class__.__name__)
|
|
|
|
|
2009-02-23 22:47:59 +08:00
|
|
|
def _setup_aggregate_query(self, aggregates):
|
2009-01-15 19:06:34 +08:00
|
|
|
"""
|
|
|
|
Prepare the query for computing a result that contains aggregate annotations.
|
|
|
|
"""
|
|
|
|
self.query.set_group_by()
|
|
|
|
|
2009-02-23 22:47:59 +08:00
|
|
|
if self.aggregate_names is not None:
|
|
|
|
self.aggregate_names.extend(aggregates)
|
|
|
|
self.query.set_aggregate_mask(self.aggregate_names)
|
|
|
|
|
|
|
|
super(ValuesQuerySet, self)._setup_aggregate_query(aggregates)
|
2008-07-09 05:53:38 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def _as_sql(self, connection):
|
2009-01-16 18:59:43 +08:00
|
|
|
"""
|
2012-12-02 10:55:26 +08:00
|
|
|
For ValuesQuerySet (and subclasses like ValuesListQuerySet), they can
|
2009-01-16 18:59:43 +08:00
|
|
|
only be used as nested queries if they're already set up to select only
|
|
|
|
a single field (in which case, that is the field column that is
|
|
|
|
returned). This differs from QuerySet.as_sql(), where the column to
|
|
|
|
select is set up by Django.
|
|
|
|
"""
|
|
|
|
if ((self._fields and len(self._fields) > 1) or
|
|
|
|
(not self._fields and len(self.model._meta.fields) > 1)):
|
|
|
|
raise TypeError('Cannot use a multi-field %s as a filter value.'
|
|
|
|
% self.__class__.__name__)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
obj = self._clone()
|
2010-03-10 23:27:22 +08:00
|
|
|
if obj._db is None or connection == connections[obj._db]:
|
2009-12-22 23:18:51 +08:00
|
|
|
return obj.query.get_compiler(connection=connection).as_nested_sql()
|
|
|
|
raise ValueError("Can't do subqueries with queries on different DBs.")
|
|
|
|
|
|
|
|
def _prepare(self):
|
|
|
|
"""
|
|
|
|
Validates that we aren't trying to do a query like
|
|
|
|
value__in=qs.values('value1', 'value2'), which isn't valid.
|
|
|
|
"""
|
|
|
|
if ((self._fields and len(self._fields) > 1) or
|
|
|
|
(not self._fields and len(self.model._meta.fields) > 1)):
|
|
|
|
raise TypeError('Cannot use a multi-field %s as a filter value.'
|
|
|
|
% self.__class__.__name__)
|
|
|
|
return self
|
2009-01-16 18:59:43 +08:00
|
|
|
|
2011-10-06 07:14:52 +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
|
|
|
class ValuesListQuerySet(ValuesQuerySet):
|
|
|
|
def iterator(self):
|
|
|
|
if self.flat and len(self._fields) == 1:
|
2009-12-22 23:18:51 +08:00
|
|
|
for row in self.query.get_compiler(self.db).results_iter():
|
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
|
|
|
yield row[0]
|
2009-01-15 19:06:34 +08:00
|
|
|
elif not self.query.extra_select and not self.query.aggregate_select:
|
2009-12-22 23:18:51 +08:00
|
|
|
for row in self.query.get_compiler(self.db).results_iter():
|
2008-08-15 09:33:18 +08:00
|
|
|
yield tuple(row)
|
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
|
|
|
else:
|
2009-03-19 17:06:04 +08:00
|
|
|
# When extra(select=...) or an annotation is involved, the extra
|
|
|
|
# cols are always at the start of the row, and we need to reorder
|
|
|
|
# the fields to match the order in self._fields.
|
2012-08-08 22:33:15 +08:00
|
|
|
extra_names = list(self.query.extra_select)
|
2009-02-16 20:28:37 +08:00
|
|
|
field_names = self.field_names
|
2012-08-08 22:33:15 +08:00
|
|
|
aggregate_names = list(self.query.aggregate_select)
|
2009-02-23 22:47:59 +08:00
|
|
|
|
2009-02-16 20:28:37 +08:00
|
|
|
names = extra_names + field_names + aggregate_names
|
|
|
|
|
|
|
|
# If a field list has been specified, use it. Otherwise, use the
|
|
|
|
# full list of fields, including extras and aggregates.
|
|
|
|
if self._fields:
|
2012-08-14 20:22:08 +08:00
|
|
|
fields = list(self._fields) + [f for f in aggregate_names if f not in self._fields]
|
2009-02-16 20:28:37 +08:00
|
|
|
else:
|
|
|
|
fields = names
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
for row in self.query.get_compiler(self.db).results_iter():
|
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
|
|
|
data = dict(zip(names, row))
|
2009-02-16 20:28:37 +08:00
|
|
|
yield tuple([data[f] for f in fields])
|
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 _clone(self, *args, **kwargs):
|
|
|
|
clone = super(ValuesListQuerySet, self)._clone(*args, **kwargs)
|
2010-11-26 22:24:08 +08:00
|
|
|
if not hasattr(clone, "flat"):
|
|
|
|
# Only assign flat if the clone didn't already get it from kwargs
|
|
|
|
clone.flat = self.flat
|
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 clone
|
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class DateQuerySet(QuerySet):
|
|
|
|
def iterator(self):
|
2009-12-22 23:18:51 +08:00
|
|
|
return self.query.get_compiler(self.db).results_iter()
|
2007-08-20 09:03:33 +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 _setup_query(self):
|
|
|
|
"""
|
|
|
|
Sets up any special features of the query attribute.
|
2007-02-14 14:32:32 +08:00
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
Called by the _clone() method after initializing the rest of the
|
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
|
|
|
instance.
|
|
|
|
"""
|
2009-03-19 17:06:04 +08:00
|
|
|
self.query.clear_deferred_loading()
|
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
|
|
|
self.query = self.query.clone(klass=sql.DateQuery, setup=True)
|
|
|
|
self.query.select = []
|
2013-02-10 23:15:49 +08:00
|
|
|
self.query.add_select(self._field_name, self._kind, self._order)
|
2007-06-23 22:16:00 +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 _clone(self, klass=None, setup=False, **kwargs):
|
|
|
|
c = super(DateQuerySet, self)._clone(klass, False, **kwargs)
|
2008-08-21 06:38:15 +08:00
|
|
|
c._field_name = self._field_name
|
2006-05-02 09:31:56 +08:00
|
|
|
c._kind = self._kind
|
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 setup and hasattr(c, '_setup_query'):
|
|
|
|
c._setup_query()
|
2006-05-02 09:31:56 +08:00
|
|
|
return c
|
2007-02-14 14:32:32 +08:00
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
class DateTimeQuerySet(QuerySet):
|
|
|
|
def iterator(self):
|
|
|
|
return self.query.get_compiler(self.db).results_iter()
|
|
|
|
|
|
|
|
def _setup_query(self):
|
|
|
|
"""
|
|
|
|
Sets up any special features of the query attribute.
|
|
|
|
|
|
|
|
Called by the _clone() method after initializing the rest of the
|
|
|
|
instance.
|
|
|
|
"""
|
|
|
|
self.query.clear_deferred_loading()
|
|
|
|
self.query = self.query.clone(klass=sql.DateTimeQuery, setup=True, tzinfo=self._tzinfo)
|
|
|
|
self.query.select = []
|
|
|
|
self.query.add_select(self._field_name, self._kind, self._order)
|
|
|
|
|
|
|
|
def _clone(self, klass=None, setup=False, **kwargs):
|
|
|
|
c = super(DateTimeQuerySet, self)._clone(klass, False, **kwargs)
|
|
|
|
c._field_name = self._field_name
|
|
|
|
c._kind = self._kind
|
|
|
|
c._tzinfo = self._tzinfo
|
|
|
|
if setup and hasattr(c, '_setup_query'):
|
|
|
|
c._setup_query()
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
2011-10-06 06:56:09 +08:00
|
|
|
def get_klass_info(klass, max_depth=0, cur_depth=0, requested=None,
|
2012-11-10 02:21:46 +08:00
|
|
|
only_load=None, from_parent=None):
|
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
|
|
|
"""
|
2011-10-06 06:56:09 +08:00
|
|
|
Helper function that recursively returns an information for a klass, to be
|
|
|
|
used in get_cached_row. It exists just to compute this information only
|
|
|
|
once for entire queryset. Otherwise it would be computed for each row, which
|
|
|
|
leads to poor perfomance on large querysets.
|
2010-01-27 21:30:29 +08:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
* klass - the class to retrieve (and instantiate)
|
|
|
|
* max_depth - the maximum depth to which a select_related()
|
|
|
|
relationship should be explored.
|
|
|
|
* cur_depth - the current depth in the select_related() tree.
|
|
|
|
Used in recursive calls to determin if we should dig deeper.
|
|
|
|
* requested - A dictionary describing the select_related() tree
|
|
|
|
that is to be retrieved. keys are field names; values are
|
|
|
|
dictionaries describing the keys on that related object that
|
|
|
|
are themselves to be select_related().
|
|
|
|
* only_load - if the query has had only() or defer() applied,
|
|
|
|
this is the list of field names that will be returned. If None,
|
|
|
|
the full field list for `klass` can be assumed.
|
2012-11-10 02:21:46 +08:00
|
|
|
* from_parent - the parent model used to get to this model
|
|
|
|
|
|
|
|
Note that when travelling from parent to child, we will only load child
|
|
|
|
fields which aren't in the parent.
|
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 max_depth and requested is None and cur_depth > max_depth:
|
|
|
|
# We've recursed deeply enough; stop now.
|
2007-02-28 23:24:05 +08:00
|
|
|
return None
|
2007-04-09 23:35:19 +08:00
|
|
|
|
2010-05-01 00:32:48 +08:00
|
|
|
if only_load:
|
2012-03-13 06:33:18 +08:00
|
|
|
load_fields = only_load.get(klass) or set()
|
2010-05-01 00:32:48 +08:00
|
|
|
# When we create the object, we will also be creating populating
|
|
|
|
# all the parent classes, so traverse the parent classes looking
|
|
|
|
# for fields that must be included on load.
|
|
|
|
for parent in klass._meta.get_parent_list():
|
|
|
|
fields = only_load.get(parent)
|
|
|
|
if fields:
|
|
|
|
load_fields.update(fields)
|
|
|
|
else:
|
|
|
|
load_fields = None
|
2011-10-06 06:56:09 +08:00
|
|
|
|
2009-04-04 13:34:23 +08:00
|
|
|
if load_fields:
|
|
|
|
# Handle deferred fields.
|
|
|
|
skip = set()
|
|
|
|
init_list = []
|
2010-01-27 21:30:29 +08:00
|
|
|
# Build the list of fields that *haven't* been requested
|
2010-03-20 23:02:59 +08:00
|
|
|
for field, model in klass._meta.get_fields_with_model():
|
2009-04-04 13:34:23 +08:00
|
|
|
if field.name not in load_fields:
|
2012-06-26 23:08:42 +08:00
|
|
|
skip.add(field.attname)
|
2012-11-10 02:21:46 +08:00
|
|
|
elif from_parent and issubclass(from_parent, model.__class__):
|
|
|
|
# Avoid loading fields already loaded for parent model for
|
|
|
|
# child models.
|
2010-03-20 23:02:59 +08:00
|
|
|
continue
|
2009-03-19 17:06:04 +08:00
|
|
|
else:
|
2009-04-04 13:34:23 +08:00
|
|
|
init_list.append(field.attname)
|
2010-01-27 21:30:29 +08:00
|
|
|
# Retrieve all the requested fields
|
2009-04-04 13:34:23 +08:00
|
|
|
field_count = len(init_list)
|
2011-10-06 06:56:09 +08:00
|
|
|
if skip:
|
2009-04-04 13:34:23 +08:00
|
|
|
klass = deferred_class_factory(klass, skip)
|
2011-10-06 06:56:09 +08:00
|
|
|
field_names = init_list
|
2009-03-19 17:06:04 +08:00
|
|
|
else:
|
2011-10-06 06:56:09 +08:00
|
|
|
field_names = ()
|
2009-04-04 13:34:23 +08:00
|
|
|
else:
|
2010-01-27 21:30:29 +08:00
|
|
|
# Load all fields on klass
|
2011-10-06 06:56:09 +08:00
|
|
|
|
2012-11-10 02:21:46 +08:00
|
|
|
field_count = len(klass._meta.fields)
|
|
|
|
# Check if we need to skip some parent fields.
|
|
|
|
if from_parent and len(klass._meta.local_fields) != len(klass._meta.fields):
|
|
|
|
# Only load those fields which haven't been already loaded into
|
|
|
|
# 'from_parent'.
|
|
|
|
non_seen_models = [p for p in klass._meta.get_parent_list()
|
|
|
|
if not issubclass(from_parent, p)]
|
|
|
|
# Load local fields, too...
|
|
|
|
non_seen_models.append(klass)
|
|
|
|
field_names = [f.attname for f in klass._meta.fields
|
|
|
|
if f.model in non_seen_models]
|
|
|
|
field_count = len(field_names)
|
|
|
|
# Try to avoid populating field_names variable for perfomance reasons.
|
|
|
|
# If field_names variable is set, we use **kwargs based model init
|
|
|
|
# which is slower than normal init.
|
|
|
|
if field_count == len(klass._meta.fields):
|
2011-10-06 06:56:09 +08:00
|
|
|
field_names = ()
|
|
|
|
|
|
|
|
restricted = requested is not None
|
|
|
|
|
|
|
|
related_fields = []
|
|
|
|
for f in klass._meta.fields:
|
2012-06-26 23:08:42 +08:00
|
|
|
if select_related_descend(f, restricted, requested, load_fields):
|
2011-10-06 06:56:09 +08:00
|
|
|
if restricted:
|
|
|
|
next = requested[f.name]
|
|
|
|
else:
|
|
|
|
next = None
|
|
|
|
klass_info = get_klass_info(f.rel.to, max_depth=max_depth, cur_depth=cur_depth+1,
|
|
|
|
requested=next, only_load=only_load)
|
|
|
|
related_fields.append((f, klass_info))
|
|
|
|
|
|
|
|
reverse_related_fields = []
|
|
|
|
if restricted:
|
|
|
|
for o in klass._meta.get_all_related_objects():
|
2012-06-26 23:08:42 +08:00
|
|
|
if o.field.unique and select_related_descend(o.field, restricted, requested,
|
|
|
|
only_load.get(o.model), reverse=True):
|
2011-10-06 06:56:09 +08:00
|
|
|
next = requested[o.field.related_query_name()]
|
2012-11-10 02:21:46 +08:00
|
|
|
parent = klass if issubclass(o.model, klass) else None
|
2011-10-06 06:56:09 +08:00
|
|
|
klass_info = get_klass_info(o.model, max_depth=max_depth, cur_depth=cur_depth+1,
|
2012-11-10 02:21:46 +08:00
|
|
|
requested=next, only_load=only_load, from_parent=parent)
|
2011-10-06 06:56:09 +08:00
|
|
|
reverse_related_fields.append((o.field, klass_info))
|
2012-11-11 20:05:29 +08:00
|
|
|
if field_names:
|
|
|
|
pk_idx = field_names.index(klass._meta.pk.attname)
|
|
|
|
else:
|
|
|
|
pk_idx = klass._meta.pk_index()
|
2011-10-06 06:56:09 +08:00
|
|
|
|
2012-11-11 20:05:29 +08:00
|
|
|
return klass, field_names, field_count, related_fields, reverse_related_fields, pk_idx
|
2011-10-06 06:56:09 +08:00
|
|
|
|
|
|
|
|
2012-11-10 02:21:46 +08:00
|
|
|
def get_cached_row(row, index_start, using, klass_info, offset=0,
|
|
|
|
parent_data=()):
|
2011-10-06 06:56:09 +08:00
|
|
|
"""
|
|
|
|
Helper function that recursively returns an object with the specified
|
|
|
|
related attributes already populated.
|
|
|
|
|
|
|
|
This method may be called recursively to populate deep select_related()
|
|
|
|
clauses.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
* row - the row of data returned by the database cursor
|
|
|
|
* index_start - the index of the row at which data for this
|
|
|
|
object is known to start
|
|
|
|
* offset - the number of additional fields that are known to
|
|
|
|
exist in row for `klass`. This usually means the number of
|
|
|
|
annotated results on `klass`.
|
2012-11-10 02:21:46 +08:00
|
|
|
* using - the database alias on which the query is being executed.
|
2011-10-06 06:56:09 +08:00
|
|
|
* klass_info - result of the get_klass_info function
|
2012-11-10 02:21:46 +08:00
|
|
|
* parent_data - parent model data in format (field, value). Used
|
|
|
|
to populate the non-local fields of child models.
|
2011-10-06 06:56:09 +08:00
|
|
|
"""
|
|
|
|
if klass_info is None:
|
|
|
|
return None
|
2012-11-11 20:05:29 +08:00
|
|
|
klass, field_names, field_count, related_fields, reverse_related_fields, pk_idx = klass_info
|
2011-10-06 06:56:09 +08:00
|
|
|
|
2012-11-10 02:21:46 +08:00
|
|
|
|
2011-10-06 06:56:09 +08:00
|
|
|
fields = row[index_start : index_start + field_count]
|
2012-11-11 20:05:29 +08:00
|
|
|
# If the pk column is None (or the Oracle equivalent ''), then the related
|
2011-10-06 06:56:09 +08:00
|
|
|
# object must be non-existent - set the relation to None.
|
2012-11-11 20:05:29 +08:00
|
|
|
if fields[pk_idx] == None or fields[pk_idx] == '':
|
2011-10-06 06:56:09 +08:00
|
|
|
obj = None
|
2012-11-11 20:05:29 +08:00
|
|
|
elif field_names:
|
2012-11-16 02:17:57 +08:00
|
|
|
fields = list(fields)
|
|
|
|
for rel_field, value in parent_data:
|
|
|
|
field_names.append(rel_field.attname)
|
|
|
|
fields.append(value)
|
2012-11-11 20:05:29 +08:00
|
|
|
obj = klass(**dict(zip(field_names, fields)))
|
2011-10-06 06:56:09 +08:00
|
|
|
else:
|
2012-11-11 20:05:29 +08:00
|
|
|
obj = klass(*fields)
|
2010-03-07 15:13:55 +08:00
|
|
|
# If an object was retrieved, set the database state.
|
|
|
|
if obj:
|
|
|
|
obj._state.db = using
|
2010-11-19 07:29:58 +08:00
|
|
|
obj._state.adding = False
|
2010-03-07 15:13:55 +08:00
|
|
|
|
2011-10-06 06:56:09 +08:00
|
|
|
# Instantiate related fields
|
2009-04-04 13:34:23 +08:00
|
|
|
index_end = index_start + field_count + offset
|
2010-01-27 21:30:29 +08:00
|
|
|
# Iterate over each related object, populating any
|
|
|
|
# select_related() fields
|
2011-10-06 06:56:09 +08:00
|
|
|
for f, klass_info in related_fields:
|
2010-01-27 21:30:29 +08:00
|
|
|
# Recursively retrieve the data for the related object
|
2011-10-06 06:56:09 +08:00
|
|
|
cached_row = get_cached_row(row, index_end, using, klass_info)
|
2010-01-27 21:30:29 +08:00
|
|
|
# If the recursive descent found an object, populate the
|
|
|
|
# descriptor caches relevant to the object
|
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 cached_row:
|
|
|
|
rel_obj, index_end = cached_row
|
2008-08-27 04:44:20 +08:00
|
|
|
if obj is not None:
|
2010-01-27 21:30:29 +08:00
|
|
|
# If the base object exists, populate the
|
|
|
|
# descriptor cache
|
2008-08-27 04:44:20 +08:00
|
|
|
setattr(obj, f.get_cache_name(), rel_obj)
|
2010-02-24 03:24:27 +08:00
|
|
|
if f.unique and rel_obj is not None:
|
2010-01-27 21:30:29 +08:00
|
|
|
# If the field is unique, populate the
|
|
|
|
# reverse descriptor cache on the related object
|
|
|
|
setattr(rel_obj, f.related.get_cache_name(), obj)
|
|
|
|
|
|
|
|
# Now do the same, but for reverse related objects.
|
|
|
|
# Only handle the restricted case - i.e., don't do a depth
|
|
|
|
# descent into reverse relations unless explicitly requested
|
2011-10-06 06:56:09 +08:00
|
|
|
for f, klass_info in reverse_related_fields:
|
2012-11-10 02:21:46 +08:00
|
|
|
# Transfer data from this object to childs.
|
|
|
|
parent_data = []
|
|
|
|
for rel_field, rel_model in klass_info[0]._meta.get_fields_with_model():
|
|
|
|
if rel_model is not None and isinstance(obj, rel_model):
|
|
|
|
parent_data.append((rel_field, getattr(obj, rel_field.attname)))
|
2011-10-06 06:56:09 +08:00
|
|
|
# Recursively retrieve the data for the related object
|
2012-11-10 02:21:46 +08:00
|
|
|
cached_row = get_cached_row(row, index_end, using, klass_info,
|
|
|
|
parent_data=parent_data)
|
2011-10-06 06:56:09 +08:00
|
|
|
# If the recursive descent found an object, populate the
|
|
|
|
# descriptor caches relevant to the object
|
|
|
|
if cached_row:
|
|
|
|
rel_obj, index_end = cached_row
|
|
|
|
if obj is not None:
|
2012-11-10 02:21:46 +08:00
|
|
|
# populate the reverse descriptor cache
|
2011-10-06 06:56:09 +08:00
|
|
|
setattr(obj, f.related.get_cache_name(), rel_obj)
|
|
|
|
if rel_obj is not None:
|
|
|
|
# If the related object exists, populate
|
|
|
|
# the descriptor cache.
|
|
|
|
setattr(rel_obj, f.get_cache_name(), obj)
|
2012-11-10 02:21:46 +08:00
|
|
|
# Populate related object caches using parent data.
|
|
|
|
for rel_field, _ in parent_data:
|
|
|
|
if rel_field.rel:
|
2011-10-06 06:56:09 +08:00
|
|
|
setattr(rel_obj, rel_field.attname, getattr(obj, rel_field.attname))
|
2012-11-10 02:21:46 +08:00
|
|
|
try:
|
|
|
|
cached_obj = getattr(obj, rel_field.get_cache_name())
|
|
|
|
setattr(rel_obj, rel_field.get_cache_name(), cached_obj)
|
|
|
|
except AttributeError:
|
|
|
|
# Related object hasn't been cached yet
|
|
|
|
pass
|
2006-05-02 09:31:56 +08:00
|
|
|
return obj, index_end
|
|
|
|
|
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
|
|
|
|
2009-12-20 10:46:58 +08:00
|
|
|
class RawQuerySet(object):
|
|
|
|
"""
|
|
|
|
Provides an iterator which converts the results of raw SQL queries into
|
|
|
|
annotated model instances.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
def __init__(self, raw_query, model=None, query=None, params=None,
|
|
|
|
translations=None, using=None):
|
|
|
|
self.raw_query = raw_query
|
2009-12-20 10:46:58 +08:00
|
|
|
self.model = model
|
2009-12-22 23:18:51 +08:00
|
|
|
self._db = using
|
|
|
|
self.query = query or sql.RawQuery(sql=raw_query, using=self.db, params=params)
|
2009-12-20 10:46:58 +08:00
|
|
|
self.params = params or ()
|
|
|
|
self.translations = translations or {}
|
|
|
|
|
|
|
|
def __iter__(self):
|
2010-11-24 23:26:29 +08:00
|
|
|
# Mapping of attrnames to row column positions. Used for constructing
|
|
|
|
# the model using kwargs, needed when not all model's fields are present
|
|
|
|
# in the query.
|
|
|
|
model_init_field_names = {}
|
|
|
|
# A list of tuples of (column name, column position). Used for
|
|
|
|
# annotation fields.
|
|
|
|
annotation_fields = []
|
|
|
|
|
|
|
|
# Cache some things for performance reasons outside the loop.
|
|
|
|
db = self.db
|
2010-12-04 04:25:59 +08:00
|
|
|
compiler = connections[db].ops.compiler('SQLCompiler')(
|
|
|
|
self.query, connections[db], db
|
|
|
|
)
|
2010-11-24 23:26:29 +08:00
|
|
|
need_resolv_columns = hasattr(compiler, 'resolve_columns')
|
|
|
|
|
2010-12-04 04:25:59 +08:00
|
|
|
query = iter(self.query)
|
|
|
|
|
2010-11-24 23:26:29 +08:00
|
|
|
# Find out which columns are model's fields, and which ones should be
|
|
|
|
# annotated to the model.
|
|
|
|
for pos, column in enumerate(self.columns):
|
|
|
|
if column in self.model_fields:
|
|
|
|
model_init_field_names[self.model_fields[column].attname] = pos
|
|
|
|
else:
|
|
|
|
annotation_fields.append((column, pos))
|
|
|
|
|
|
|
|
# Find out which model's fields are not present in the query.
|
|
|
|
skip = set()
|
|
|
|
for field in self.model._meta.fields:
|
|
|
|
if field.attname not in model_init_field_names:
|
|
|
|
skip.add(field.attname)
|
|
|
|
if skip:
|
|
|
|
if self.model._meta.pk.attname in skip:
|
|
|
|
raise InvalidQuery('Raw query must include the primary key')
|
|
|
|
model_cls = deferred_class_factory(self.model, skip)
|
|
|
|
else:
|
|
|
|
model_cls = self.model
|
|
|
|
# All model's fields are present in the query. So, it is possible
|
|
|
|
# to use *args based model instantation. For each field of the model,
|
|
|
|
# record the query column position matching that field.
|
|
|
|
model_init_field_pos = []
|
|
|
|
for field in self.model._meta.fields:
|
|
|
|
model_init_field_pos.append(model_init_field_names[field.attname])
|
|
|
|
if need_resolv_columns:
|
|
|
|
fields = [self.model_fields.get(c, None) for c in self.columns]
|
|
|
|
# Begin looping through the query values.
|
2010-12-04 04:25:59 +08:00
|
|
|
for values in query:
|
2010-11-24 23:26:29 +08:00
|
|
|
if need_resolv_columns:
|
|
|
|
values = compiler.resolve_columns(values, fields)
|
|
|
|
# Associate fields to values
|
|
|
|
if skip:
|
|
|
|
model_init_kwargs = {}
|
2012-07-21 03:14:27 +08:00
|
|
|
for attname, pos in six.iteritems(model_init_field_names):
|
2010-11-24 23:26:29 +08:00
|
|
|
model_init_kwargs[attname] = values[pos]
|
|
|
|
instance = model_cls(**model_init_kwargs)
|
|
|
|
else:
|
|
|
|
model_init_args = [values[pos] for pos in model_init_field_pos]
|
|
|
|
instance = model_cls(*model_init_args)
|
|
|
|
if annotation_fields:
|
|
|
|
for column, pos in annotation_fields:
|
|
|
|
setattr(instance, column, values[pos])
|
|
|
|
|
|
|
|
instance._state.db = db
|
|
|
|
instance._state.adding = False
|
|
|
|
|
|
|
|
yield instance
|
2009-12-20 10:46:58 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
2011-04-23 02:17:26 +08:00
|
|
|
return "<RawQuerySet: %r>" % (self.raw_query % tuple(self.params))
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2010-02-23 13:22:12 +08:00
|
|
|
def __getitem__(self, k):
|
|
|
|
return list(self)[k]
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
@property
|
|
|
|
def db(self):
|
|
|
|
"Return the database that will be used if this query is executed now"
|
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
|
|
|
|
|
|
|
def using(self, alias):
|
|
|
|
"""
|
|
|
|
Selects which database this Raw QuerySet should excecute it's query against.
|
|
|
|
"""
|
|
|
|
return RawQuerySet(self.raw_query, model=self.model,
|
|
|
|
query=self.query.clone(using=alias),
|
|
|
|
params=self.params, translations=self.translations,
|
|
|
|
using=alias)
|
2009-12-20 10:46:58 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def columns(self):
|
|
|
|
"""
|
|
|
|
A list of model field names in the order they'll appear in the
|
|
|
|
query results.
|
|
|
|
"""
|
|
|
|
if not hasattr(self, '_columns'):
|
|
|
|
self._columns = self.query.get_columns()
|
|
|
|
|
|
|
|
# Adjust any column names which don't match field names
|
|
|
|
for (query_name, model_name) in self.translations.items():
|
|
|
|
try:
|
|
|
|
index = self._columns.index(query_name)
|
|
|
|
self._columns[index] = model_name
|
|
|
|
except ValueError:
|
|
|
|
# Ignore translations for non-existant column names
|
|
|
|
pass
|
|
|
|
|
|
|
|
return self._columns
|
|
|
|
|
|
|
|
@property
|
|
|
|
def model_fields(self):
|
|
|
|
"""
|
|
|
|
A dict mapping column names to model field names.
|
|
|
|
"""
|
|
|
|
if not hasattr(self, '_model_fields'):
|
2009-12-23 05:05:15 +08:00
|
|
|
converter = connections[self.db].introspection.table_name_converter
|
2009-12-20 10:46:58 +08:00
|
|
|
self._model_fields = {}
|
|
|
|
for field in self.model._meta.fields:
|
|
|
|
name, column = field.get_attname_column()
|
2010-04-02 00:48:16 +08:00
|
|
|
self._model_fields[converter(column)] = field
|
2009-12-20 10:46:58 +08:00
|
|
|
return self._model_fields
|
|
|
|
|
2011-10-06 07:14:52 +08:00
|
|
|
|
2011-09-10 03:22:28 +08:00
|
|
|
def insert_query(model, objs, fields, return_id=False, raw=False, using=None):
|
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
|
|
|
"""
|
|
|
|
Inserts a new record for the given model. This provides an interface to
|
|
|
|
the InsertQuery class and is how Model.save() is implemented. It is not
|
|
|
|
part of the public API.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
query = sql.InsertQuery(model)
|
2011-09-10 03:22:28 +08:00
|
|
|
query.insert_values(fields, objs, raw=raw)
|
2009-12-22 23:18:51 +08:00
|
|
|
return query.get_compiler(using=using).execute_sql(return_id)
|
2011-10-06 07:14:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
def prefetch_related_objects(result_cache, related_lookups):
|
|
|
|
"""
|
|
|
|
Helper function for prefetch_related functionality
|
|
|
|
|
|
|
|
Populates prefetched objects caches for a list of results
|
|
|
|
from a QuerySet
|
|
|
|
"""
|
|
|
|
if len(result_cache) == 0:
|
|
|
|
return # nothing to do
|
|
|
|
|
|
|
|
model = result_cache[0].__class__
|
|
|
|
|
|
|
|
# We need to be able to dynamically add to the list of prefetch_related
|
|
|
|
# lookups that we look up (see below). So we need some book keeping to
|
|
|
|
# ensure we don't do duplicate work.
|
|
|
|
done_lookups = set() # list of lookups like foo__bar__baz
|
|
|
|
done_queries = {} # dictionary of things like 'foo__bar': [results]
|
|
|
|
|
2011-10-08 00:06:02 +08:00
|
|
|
auto_lookups = [] # we add to this as we go through.
|
|
|
|
followed_descriptors = set() # recursion protection
|
|
|
|
|
2011-11-29 20:50:10 +08:00
|
|
|
all_lookups = itertools.chain(related_lookups, auto_lookups)
|
|
|
|
for lookup in all_lookups:
|
2011-10-06 07:14:52 +08:00
|
|
|
if lookup in done_lookups:
|
|
|
|
# We've done exactly this already, skip the whole thing
|
|
|
|
continue
|
|
|
|
done_lookups.add(lookup)
|
|
|
|
|
2013-01-29 23:45:40 +08:00
|
|
|
# Top level, the list of objects to decorate is the result cache
|
2011-10-06 07:14:52 +08:00
|
|
|
# from the primary QuerySet. It won't be for deeper levels.
|
|
|
|
obj_list = result_cache
|
|
|
|
|
|
|
|
attrs = lookup.split(LOOKUP_SEP)
|
|
|
|
for level, attr in enumerate(attrs):
|
|
|
|
# Prepare main instances
|
|
|
|
if len(obj_list) == 0:
|
|
|
|
break
|
|
|
|
|
|
|
|
good_objects = True
|
|
|
|
for obj in obj_list:
|
|
|
|
if not hasattr(obj, '_prefetched_objects_cache'):
|
|
|
|
try:
|
|
|
|
obj._prefetched_objects_cache = {}
|
|
|
|
except AttributeError:
|
|
|
|
# Must be in a QuerySet subclass that is not returning
|
|
|
|
# Model instances, either in Django or 3rd
|
|
|
|
# party. prefetch_related() doesn't make sense, so quit
|
|
|
|
# now.
|
|
|
|
good_objects = False
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# We already did this list
|
|
|
|
break
|
|
|
|
if not good_objects:
|
|
|
|
break
|
|
|
|
|
|
|
|
# Descend down tree
|
2011-10-08 00:05:53 +08:00
|
|
|
|
|
|
|
# We assume that objects retrieved are homogenous (which is the premise
|
|
|
|
# of prefetch_related), so what applies to first object applies to all.
|
|
|
|
first_obj = obj_list[0]
|
2011-10-08 00:06:02 +08:00
|
|
|
prefetcher, descriptor, attr_found, is_fetched = get_prefetcher(first_obj, attr)
|
2011-10-08 00:05:53 +08:00
|
|
|
|
|
|
|
if not attr_found:
|
2011-10-06 07:14:52 +08:00
|
|
|
raise AttributeError("Cannot find '%s' on %s object, '%s' is an invalid "
|
|
|
|
"parameter to prefetch_related()" %
|
2011-10-08 00:05:53 +08:00
|
|
|
(attr, first_obj.__class__.__name__, lookup))
|
2011-10-06 07:14:52 +08:00
|
|
|
|
2011-10-08 00:05:53 +08:00
|
|
|
if level == len(attrs) - 1 and prefetcher is None:
|
|
|
|
# Last one, this *must* resolve to something that supports
|
|
|
|
# prefetching, otherwise there is no point adding it and the
|
|
|
|
# developer asking for it has made a mistake.
|
|
|
|
raise ValueError("'%s' does not resolve to a item that supports "
|
|
|
|
"prefetching - this is an invalid parameter to "
|
|
|
|
"prefetch_related()." % lookup)
|
2011-10-06 07:14:52 +08:00
|
|
|
|
2011-10-08 00:05:53 +08:00
|
|
|
if prefetcher is not None and not is_fetched:
|
2011-10-06 07:14:52 +08:00
|
|
|
# Check we didn't do this already
|
|
|
|
current_lookup = LOOKUP_SEP.join(attrs[0:level+1])
|
|
|
|
if current_lookup in done_queries:
|
|
|
|
obj_list = done_queries[current_lookup]
|
|
|
|
else:
|
2011-10-08 00:05:53 +08:00
|
|
|
obj_list, additional_prl = prefetch_one_level(obj_list, prefetcher, attr)
|
2011-10-08 00:06:02 +08:00
|
|
|
# We need to ensure we don't keep adding lookups from the
|
|
|
|
# same relationships to stop infinite recursion. So, if we
|
|
|
|
# are already on an automatically added lookup, don't add
|
|
|
|
# the new lookups from relationships we've seen already.
|
|
|
|
if not (lookup in auto_lookups and
|
|
|
|
descriptor in followed_descriptors):
|
|
|
|
for f in additional_prl:
|
|
|
|
new_prl = LOOKUP_SEP.join([current_lookup, f])
|
|
|
|
auto_lookups.append(new_prl)
|
|
|
|
done_queries[current_lookup] = obj_list
|
|
|
|
followed_descriptors.add(descriptor)
|
2011-10-06 07:14:52 +08:00
|
|
|
else:
|
2011-10-08 00:05:53 +08:00
|
|
|
# Either a singly related object that has already been fetched
|
|
|
|
# (e.g. via select_related), or hopefully some other property
|
|
|
|
# that doesn't support prefetching but needs to be traversed.
|
|
|
|
|
2012-04-12 05:11:22 +08:00
|
|
|
# We replace the current list of parent objects with the list
|
|
|
|
# of related objects, filtering out empty or missing values so
|
|
|
|
# that we can continue with nullable or reverse relations.
|
|
|
|
new_obj_list = []
|
|
|
|
for obj in obj_list:
|
|
|
|
try:
|
|
|
|
new_obj = getattr(obj, attr)
|
|
|
|
except exceptions.ObjectDoesNotExist:
|
|
|
|
continue
|
|
|
|
if new_obj is None:
|
|
|
|
continue
|
|
|
|
new_obj_list.append(new_obj)
|
|
|
|
obj_list = new_obj_list
|
2011-10-06 07:14:52 +08:00
|
|
|
|
|
|
|
|
2011-10-08 00:05:53 +08:00
|
|
|
def get_prefetcher(instance, attr):
|
|
|
|
"""
|
|
|
|
For the attribute 'attr' on the given instance, finds
|
2013-03-08 22:15:23 +08:00
|
|
|
an object that has a get_prefetch_queryset().
|
2011-10-08 00:06:02 +08:00
|
|
|
Returns a 4 tuple containing:
|
2013-03-08 22:15:23 +08:00
|
|
|
(the object with get_prefetch_queryset (or None),
|
2011-10-08 00:06:02 +08:00
|
|
|
the descriptor object representing this relationship (or None),
|
2011-10-08 00:05:53 +08:00
|
|
|
a boolean that is False if the attribute was not found at all,
|
|
|
|
a boolean that is True if the attribute has already been fetched)
|
|
|
|
"""
|
|
|
|
prefetcher = None
|
|
|
|
attr_found = False
|
|
|
|
is_fetched = False
|
|
|
|
|
|
|
|
# For singly related objects, we have to avoid getting the attribute
|
|
|
|
# from the object, as this will trigger the query. So we first try
|
|
|
|
# on the class, in order to get the descriptor object.
|
|
|
|
rel_obj_descriptor = getattr(instance.__class__, attr, None)
|
|
|
|
if rel_obj_descriptor is None:
|
|
|
|
try:
|
|
|
|
rel_obj = getattr(instance, attr)
|
|
|
|
attr_found = True
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
attr_found = True
|
|
|
|
if rel_obj_descriptor:
|
|
|
|
# singly related object, descriptor object has the
|
2013-03-08 22:15:23 +08:00
|
|
|
# get_prefetch_queryset() method.
|
|
|
|
if hasattr(rel_obj_descriptor, 'get_prefetch_queryset'):
|
2011-10-08 00:05:53 +08:00
|
|
|
prefetcher = rel_obj_descriptor
|
|
|
|
if rel_obj_descriptor.is_cached(instance):
|
|
|
|
is_fetched = True
|
|
|
|
else:
|
|
|
|
# descriptor doesn't support prefetching, so we go ahead and get
|
|
|
|
# the attribute on the instance rather than the class to
|
|
|
|
# support many related managers
|
|
|
|
rel_obj = getattr(instance, attr)
|
2013-03-08 22:15:23 +08:00
|
|
|
if hasattr(rel_obj, 'get_prefetch_queryset'):
|
2011-10-08 00:05:53 +08:00
|
|
|
prefetcher = rel_obj
|
2011-10-08 00:06:02 +08:00
|
|
|
return prefetcher, rel_obj_descriptor, attr_found, is_fetched
|
2011-10-08 00:05:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
def prefetch_one_level(instances, prefetcher, attname):
|
2011-10-06 07:14:52 +08:00
|
|
|
"""
|
|
|
|
Helper function for prefetch_related_objects
|
|
|
|
|
2011-10-08 00:05:53 +08:00
|
|
|
Runs prefetches on all instances using the prefetcher object,
|
|
|
|
assigning results to relevant caches in instance.
|
2011-10-06 07:14:52 +08:00
|
|
|
|
|
|
|
The prefetched objects are returned, along with any additional
|
|
|
|
prefetches that must be done due to prefetch_related lookups
|
|
|
|
found from default managers.
|
|
|
|
"""
|
2013-03-08 22:15:23 +08:00
|
|
|
# prefetcher must have a method get_prefetch_queryset() which takes a list
|
2011-10-08 00:05:53 +08:00
|
|
|
# of instances, and returns a tuple:
|
|
|
|
|
|
|
|
# (queryset of instances of self.model that are related to passed in instances,
|
|
|
|
# callable that gets value to be matched for returned instances,
|
|
|
|
# callable that gets value to be matched for passed in instances,
|
|
|
|
# boolean that is True for singly related objects,
|
|
|
|
# cache name to assign to).
|
|
|
|
|
|
|
|
# The 'values to be matched' must be hashable as they will be used
|
|
|
|
# in a dictionary.
|
|
|
|
|
|
|
|
rel_qs, rel_obj_attr, instance_attr, single, cache_name =\
|
2013-03-08 22:15:23 +08:00
|
|
|
prefetcher.get_prefetch_queryset(instances)
|
2011-10-06 07:14:52 +08:00
|
|
|
# We have to handle the possibility that the default manager itself added
|
|
|
|
# prefetch_related lookups to the QuerySet we just got back. We don't want to
|
|
|
|
# trigger the prefetch_related functionality by evaluating the query.
|
|
|
|
# Rather, we need to merge in the prefetch_related lookups.
|
|
|
|
additional_prl = getattr(rel_qs, '_prefetch_related_lookups', [])
|
|
|
|
if additional_prl:
|
|
|
|
# Don't need to clone because the manager should have given us a fresh
|
|
|
|
# instance, so we access an internal instead of using public interface
|
|
|
|
# for performance reasons.
|
|
|
|
rel_qs._prefetch_related_lookups = []
|
|
|
|
|
|
|
|
all_related_objects = list(rel_qs)
|
|
|
|
|
|
|
|
rel_obj_cache = {}
|
|
|
|
for rel_obj in all_related_objects:
|
2011-10-08 00:05:53 +08:00
|
|
|
rel_attr_val = rel_obj_attr(rel_obj)
|
2012-05-24 19:25:01 +08:00
|
|
|
rel_obj_cache.setdefault(rel_attr_val, []).append(rel_obj)
|
2011-10-06 07:14:52 +08:00
|
|
|
|
|
|
|
for obj in instances:
|
2011-10-08 00:05:53 +08:00
|
|
|
instance_attr_val = instance_attr(obj)
|
|
|
|
vals = rel_obj_cache.get(instance_attr_val, [])
|
|
|
|
if single:
|
|
|
|
# Need to assign to single cache on instance
|
2012-04-12 05:11:22 +08:00
|
|
|
setattr(obj, cache_name, vals[0] if vals else None)
|
2011-10-08 00:05:53 +08:00
|
|
|
else:
|
|
|
|
# Multi, attribute represents a manager with an .all() method that
|
|
|
|
# returns a QuerySet
|
|
|
|
qs = getattr(obj, attname).all()
|
|
|
|
qs._result_cache = vals
|
|
|
|
# We don't want the individual qs doing prefetch_related now, since we
|
|
|
|
# have merged this into the current work.
|
|
|
|
qs._prefetch_done = True
|
|
|
|
obj._prefetched_objects_cache[cache_name] = qs
|
2011-10-06 07:14:52 +08:00
|
|
|
return all_related_objects, additional_prl
|