2008-07-13 09:22:31 +08:00
|
|
|
try:
|
|
|
|
set
|
|
|
|
except NameError:
|
|
|
|
from sets import Set as set # Python 2.3 fallback
|
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-03-18 22:08:40 +08:00
|
|
|
from django.db import connection, transaction, IntegrityError
|
2008-07-13 09:22:31 +08:00
|
|
|
from django.db.models.fields import DateField
|
2008-06-29 17:40:17 +08:00
|
|
|
from django.db.models.query_utils import Q, select_related_descend
|
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
|
|
|
from django.db.models import signals, sql
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.utils.datastructures import SortedDict
|
|
|
|
|
2008-07-13 07:25:22 +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
|
|
|
|
2008-07-13 09:17:23 +08:00
|
|
|
|
2008-06-22 04:57:05 +08:00
|
|
|
class CyclicDependency(Exception):
|
2008-07-13 09:17:23 +08:00
|
|
|
"""
|
|
|
|
An error when dealing with a collection of objects that have a cyclic
|
|
|
|
dependency, i.e. when deleting multiple objects.
|
|
|
|
"""
|
2008-06-22 04:57:05 +08:00
|
|
|
pass
|
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
|
2008-06-22 04:57:05 +08:00
|
|
|
class CollectedObjects(object):
|
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
A container that stores keys and lists of values along with remembering the
|
|
|
|
parent objects for all the keys.
|
2008-06-22 04:57:05 +08:00
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
This is used for the database object deletion routines so that we can
|
|
|
|
calculate the 'leaf' objects which should be deleted first.
|
2008-06-22 04:57:05 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.data = {}
|
|
|
|
self.children = {}
|
|
|
|
|
|
|
|
def add(self, model, pk, obj, parent_model, nullable=False):
|
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Adds an item to the container.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
* model - the class of the object being added.
|
|
|
|
* pk - the primary key.
|
|
|
|
* obj - the object itself.
|
|
|
|
* parent_model - the model of the parent object that this object was
|
|
|
|
reached through.
|
|
|
|
* nullable - should be True if this relation is nullable.
|
2008-06-22 04:57:05 +08:00
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
Returns True if the item already existed in the structure and
|
|
|
|
False otherwise.
|
2008-06-22 04:57:05 +08:00
|
|
|
"""
|
|
|
|
d = self.data.setdefault(model, SortedDict())
|
|
|
|
retval = pk in d
|
|
|
|
d[pk] = obj
|
2008-07-09 05:53:38 +08:00
|
|
|
# Nullable relationships can be ignored -- they are nulled out before
|
|
|
|
# deleting, and therefore do not affect the order in which objects
|
|
|
|
# have to be deleted.
|
2008-06-22 04:57:05 +08:00
|
|
|
if parent_model is not None and not nullable:
|
|
|
|
self.children.setdefault(parent_model, []).append(model)
|
|
|
|
return retval
|
|
|
|
|
|
|
|
def __contains__(self, key):
|
|
|
|
return self.data.__contains__(key)
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self.data[key]
|
|
|
|
|
|
|
|
def __nonzero__(self):
|
|
|
|
return bool(self.data)
|
|
|
|
|
|
|
|
def iteritems(self):
|
|
|
|
for k in self.ordered_keys():
|
|
|
|
yield k, self[k]
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return list(self.iteritems())
|
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
return self.ordered_keys()
|
|
|
|
|
|
|
|
def ordered_keys(self):
|
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Returns the models in the order that they should be dealt with (i.e.
|
|
|
|
models with no dependencies first).
|
2008-06-22 04:57:05 +08:00
|
|
|
"""
|
|
|
|
dealt_with = SortedDict()
|
|
|
|
# Start with items that have no children
|
|
|
|
models = self.data.keys()
|
|
|
|
while len(dealt_with) < len(models):
|
|
|
|
found = False
|
|
|
|
for model in models:
|
2008-09-02 03:35:03 +08:00
|
|
|
if model in dealt_with:
|
|
|
|
continue
|
2008-06-22 04:57:05 +08:00
|
|
|
children = self.children.setdefault(model, [])
|
|
|
|
if len([c for c in children if c not in dealt_with]) == 0:
|
|
|
|
dealt_with[model] = None
|
|
|
|
found = True
|
|
|
|
if not found:
|
2008-07-09 05:53:38 +08:00
|
|
|
raise CyclicDependency(
|
|
|
|
"There is a cyclic dependency of items to be processed.")
|
2008-06-26 10:20:45 +08:00
|
|
|
|
2008-06-22 04:57:05 +08:00
|
|
|
return dealt_with.keys()
|
|
|
|
|
|
|
|
def unordered_keys(self):
|
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Fallback for the case where is a cyclic dependency but we don't care.
|
2008-06-22 04:57:05 +08:00
|
|
|
"""
|
|
|
|
return self.data.keys()
|
|
|
|
|
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
|
|
|
class QuerySet(object):
|
2008-07-09 05:53:38 +08:00
|
|
|
"""
|
|
|
|
Represents a lazy database lookup for a set of 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
|
|
|
def __init__(self, model=None, query=None):
|
2006-05-02 09:31:56 +08:00
|
|
|
self.model = 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
|
|
|
self.query = query or sql.Query(self.model, connection)
|
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
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
########################
|
|
|
|
# PYTHON MAGIC METHODS #
|
|
|
|
########################
|
|
|
|
|
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:
|
|
|
|
self._result_cache.extend(list(self._iter))
|
|
|
|
return len(self._result_cache)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __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
|
|
|
if self._result_cache is None:
|
|
|
|
self._iter = self.iterator()
|
|
|
|
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()
|
|
|
|
|
|
|
|
def __nonzero__(self):
|
|
|
|
if self._result_cache is not None:
|
|
|
|
return bool(self._result_cache)
|
|
|
|
try:
|
|
|
|
iter(self).next()
|
|
|
|
except StopIteration:
|
|
|
|
return False
|
|
|
|
return True
|
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.
|
|
|
|
"""
|
2007-08-09 05:09:55 +08:00
|
|
|
if not isinstance(k, (slice, int, long)):
|
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
|
|
|
|
try:
|
|
|
|
qs = self._clone()
|
|
|
|
qs.query.set_limits(k, k + 1)
|
|
|
|
return list(qs)[0]
|
|
|
|
except self.model.DoesNotExist, e:
|
|
|
|
raise IndexError, e.args
|
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):
|
|
|
|
return other._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
|
|
|
combined = self._clone()
|
|
|
|
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)
|
|
|
|
combined = self._clone()
|
2008-06-26 19:42:12 +08:00
|
|
|
if isinstance(other, EmptyQuerySet):
|
|
|
|
return combined
|
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.
|
|
|
|
"""
|
|
|
|
fill_cache = self.query.select_related
|
|
|
|
if isinstance(fill_cache, dict):
|
|
|
|
requested = fill_cache
|
|
|
|
else:
|
|
|
|
requested = None
|
|
|
|
max_depth = self.query.max_depth
|
|
|
|
extra_select = self.query.extra_select.keys()
|
|
|
|
index_start = len(extra_select)
|
|
|
|
for row in self.query.results_iter():
|
|
|
|
if fill_cache:
|
|
|
|
obj, _ = get_cached_row(self.model, row, index_start,
|
|
|
|
max_depth, requested=requested)
|
|
|
|
else:
|
2008-04-29 09:19:42 +08:00
|
|
|
obj = self.model(*row[index_start:])
|
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
|
|
|
for i, k in enumerate(extra_select):
|
|
|
|
setattr(obj, k, row[i])
|
|
|
|
yield obj
|
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
|
|
|
|
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.query.get_count()
|
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)
|
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:
|
|
|
|
raise self.model.DoesNotExist("%s matching query does not exist."
|
|
|
|
% self.model._meta.object_name)
|
|
|
|
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)
|
2008-08-29 01:18:05 +08:00
|
|
|
obj.save(force_insert=True)
|
2006-06-28 04:36:25 +08:00
|
|
|
return obj
|
2006-05-02 09:31:56 +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', {})
|
|
|
|
try:
|
|
|
|
return self.get(**kwargs), False
|
|
|
|
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)
|
2008-08-12 13:59:43 +08:00
|
|
|
sid = transaction.savepoint()
|
2008-08-29 01:18:05 +08:00
|
|
|
obj.save(force_insert=True)
|
2008-08-12 13:59:43 +08:00
|
|
|
transaction.savepoint_commit(sid)
|
2008-03-18 22:08:40 +08:00
|
|
|
return obj, True
|
|
|
|
except IntegrityError, e:
|
2008-08-12 13:59:43 +08:00
|
|
|
transaction.savepoint_rollback(sid)
|
2008-08-16 07:29:55 +08:00
|
|
|
try:
|
|
|
|
return self.get(**kwargs), False
|
|
|
|
except self.model.DoesNotExist:
|
|
|
|
raise e
|
2006-06-07 08:09:29 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def latest(self, field_name=None):
|
|
|
|
"""
|
|
|
|
Returns the latest object, according to the model's 'get_latest_by'
|
|
|
|
option or optional given field_name.
|
|
|
|
"""
|
|
|
|
latest_by = field_name or self.model._meta.get_latest_by
|
|
|
|
assert bool(latest_by), "latest() requires 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(), \
|
2006-05-02 09:31:56 +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)
|
|
|
|
obj.query.add_ordering('-%s' % latest_by)
|
|
|
|
return obj.get()
|
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
|
|
|
assert isinstance(id_list, (tuple, list)), \
|
|
|
|
"in_bulk() must be provided with a list of IDs."
|
|
|
|
if not id_list:
|
2006-05-02 09:31:56 +08:00
|
|
|
return {}
|
|
|
|
qs = 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
|
|
|
qs.query.add_filter(('pk__in', id_list))
|
2006-05-02 09:31:56 +08:00
|
|
|
return dict([(obj._get_pk_val(), obj) for obj in qs.iterator()])
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
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.
|
|
|
|
del_query.query.select_related = False
|
|
|
|
del_query.query.clear_ordering()
|
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
|
|
|
# Delete objects in chunks to prevent the list of related objects from
|
|
|
|
# becoming too long.
|
|
|
|
while 1:
|
|
|
|
# Collect all the objects to be deleted in this chunk, and all the
|
|
|
|
# objects that are related to the objects that are to be deleted.
|
2008-06-22 04:57:05 +08:00
|
|
|
seen_objs = CollectedObjects()
|
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
|
|
|
for object in del_query[:CHUNK_SIZE]:
|
2006-05-02 09:31:56 +08:00
|
|
|
object._collect_sub_objects(seen_objs)
|
|
|
|
|
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 seen_objs:
|
|
|
|
break
|
|
|
|
delete_objects(seen_objs)
|
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
|
|
|
|
|
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."
|
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)
|
2008-08-10 01:19:23 +08:00
|
|
|
rows = query.execute_sql(None)
|
2008-04-28 08:58:21 +08:00
|
|
|
transaction.commit_unless_managed()
|
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
|
2008-08-10 01:19:23 +08:00
|
|
|
return query.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
|
|
|
|
|
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'
|
|
|
|
% (kwargs.keys(),))
|
|
|
|
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'):
|
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Returns a list of datetime objects representing all available dates for
|
|
|
|
the given field_name, scoped to 'kind'.
|
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
|
|
|
assert kind in ("month", "year", "day"), \
|
|
|
|
"'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
|
|
|
|
|
|
|
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
|
|
|
"""
|
|
|
|
return self._clone(klass=EmptyQuerySet)
|
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
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
depth = kwargs.pop('depth', 0)
|
|
|
|
if kwargs:
|
|
|
|
raise TypeError('Unexpected keyword arguments to select_related: %s'
|
|
|
|
% (kwargs.keys(),))
|
|
|
|
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
|
|
|
|
|
|
|
|
def dup_select_related(self, other):
|
|
|
|
"""
|
2008-07-09 05:53:38 +08:00
|
|
|
Copies the related selection status from the QuerySet 'other' to 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
|
|
|
"""
|
|
|
|
self.query.select_related = other.query.select_related
|
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()
|
|
|
|
obj.query.clear_ordering()
|
|
|
|
obj.query.add_ordering(*field_names)
|
|
|
|
return obj
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def distinct(self, true_or_false=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
|
|
|
"""
|
|
|
|
Returns a new QuerySet instance that will select only distinct results.
|
|
|
|
"""
|
|
|
|
obj = self._clone()
|
|
|
|
obj.query.distinct = true_or_false
|
|
|
|
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
|
|
|
|
|
|
|
|
###################
|
|
|
|
# PRIVATE METHODS #
|
|
|
|
###################
|
|
|
|
|
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
|
|
|
|
c = klass(model=self.model, query=query)
|
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):
|
|
|
|
self._result_cache.append(self._iter.next())
|
|
|
|
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
|
|
|
|
2009-01-05 19:47:48 +08:00
|
|
|
def as_sql(self):
|
|
|
|
"""
|
|
|
|
Returns the internal query's SQL and parameters (as a tuple).
|
|
|
|
|
|
|
|
This is a private (internal) method. The name is chosen to provide
|
|
|
|
uniformity with other interfaces (in particular, the Query class).
|
|
|
|
"""
|
|
|
|
obj = self.values("pk")
|
|
|
|
return obj.query.as_nested_sql()
|
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
|
|
|
|
|
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):
|
2008-06-26 10:20:45 +08:00
|
|
|
if (not self.extra_names and
|
2008-06-15 14:24:41 +08:00
|
|
|
len(self.field_names) != len(self.model._meta.fields)):
|
|
|
|
self.query.trim_extra_select(self.extra_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
|
|
|
names = self.query.extra_select.keys() + self.field_names
|
|
|
|
for row in self.query.results_iter():
|
|
|
|
yield dict(zip(names, row))
|
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):
|
|
|
|
"""
|
|
|
|
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-01-08 13:49:03 +08:00
|
|
|
self.query.clear_select_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
|
|
|
self.extra_names = []
|
2006-05-02 09:31:56 +08:00
|
|
|
if 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
|
|
|
if not self.query.extra_select:
|
|
|
|
field_names = list(self._fields)
|
2007-05-31 10:22:41 +08:00
|
|
|
else:
|
2007-07-28 06:07:42 +08:00
|
|
|
field_names = []
|
2007-05-31 10:22:41 +08:00
|
|
|
for f in 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
|
|
|
if self.query.extra_select.has_key(f):
|
|
|
|
self.extra_names.append(f)
|
|
|
|
else:
|
2007-07-28 06:07:42 +08:00
|
|
|
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.
|
|
|
|
field_names = [f.attname for f in self.model._meta.fields]
|
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
|
|
|
self.query.add_fields(field_names, False)
|
|
|
|
self.query.default_cols = False
|
|
|
|
self.field_names = field_names
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
set(self.field_names) != set(other.field_names)):
|
|
|
|
raise TypeError("Merging '%s' classes must involve the same values in each case."
|
|
|
|
% self.__class__.__name__)
|
|
|
|
|
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
|
|
|
class ValuesListQuerySet(ValuesQuerySet):
|
|
|
|
def iterator(self):
|
|
|
|
self.query.trim_extra_select(self.extra_names)
|
|
|
|
if self.flat and len(self._fields) == 1:
|
|
|
|
for row in self.query.results_iter():
|
|
|
|
yield row[0]
|
|
|
|
elif not self.query.extra_select:
|
|
|
|
for row in self.query.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:
|
|
|
|
# When extra(select=...) is involved, the extra cols come are
|
|
|
|
# always at the start of the row, so we need to reorder the fields
|
|
|
|
# to match the order in self._fields.
|
|
|
|
names = self.query.extra_select.keys() + self.field_names
|
|
|
|
for row in self.query.results_iter():
|
|
|
|
data = dict(zip(names, row))
|
|
|
|
yield tuple([data[f] for f in self._fields])
|
|
|
|
|
|
|
|
def _clone(self, *args, **kwargs):
|
|
|
|
clone = super(ValuesListQuerySet, self)._clone(*args, **kwargs)
|
|
|
|
clone.flat = self.flat
|
|
|
|
return clone
|
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class DateQuerySet(QuerySet):
|
|
|
|
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
|
|
|
return self.query.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.
|
|
|
|
"""
|
|
|
|
self.query = self.query.clone(klass=sql.DateQuery, setup=True)
|
|
|
|
self.query.select = []
|
2008-08-21 06:38:15 +08:00
|
|
|
field = self.model._meta.get_field(self._field_name, many_to_many=False)
|
|
|
|
assert isinstance(field, DateField), "%r isn't a DateField." \
|
2008-08-30 06:29:38 +08:00
|
|
|
% field.name
|
2008-08-21 06:38:15 +08:00
|
|
|
self.query.add_date_select(field, self._kind, self._order)
|
|
|
|
if field.null:
|
|
|
|
self.query.add_filter(('%s__isnull' % field.name, False))
|
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
|
|
|
|
2007-01-23 10:11:08 +08:00
|
|
|
class EmptyQuerySet(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 __init__(self, model=None, query=None):
|
|
|
|
super(EmptyQuerySet, self).__init__(model, query)
|
2007-01-23 10:11:08 +08:00
|
|
|
self._result_cache = []
|
2007-02-14 14:32:32 +08:00
|
|
|
|
2008-06-26 19:42:12 +08:00
|
|
|
def __and__(self, other):
|
|
|
|
return self._clone()
|
|
|
|
|
|
|
|
def __or__(self, other):
|
|
|
|
return other._clone()
|
|
|
|
|
2007-01-23 10:11:08 +08:00
|
|
|
def count(self):
|
|
|
|
return 0
|
2007-02-14 14:32:32 +08:00
|
|
|
|
2007-01-23 10:11:08 +08:00
|
|
|
def delete(self):
|
|
|
|
pass
|
|
|
|
|
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):
|
2007-01-23 10:11:08 +08:00
|
|
|
c = super(EmptyQuerySet, self)._clone(klass, **kwargs)
|
|
|
|
c._result_cache = []
|
|
|
|
return c
|
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 iterator(self):
|
|
|
|
# This slightly odd construction is because we need an empty generator
|
|
|
|
# (it raises StopIteration immediately).
|
|
|
|
yield iter([]).next()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-01-08 13:16:21 +08:00
|
|
|
# EmptyQuerySet is always an empty result in where-clauses (and similar
|
|
|
|
# situations).
|
|
|
|
value_annotation = False
|
|
|
|
|
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
|
|
|
def get_cached_row(klass, row, index_start, max_depth=0, cur_depth=0,
|
2008-07-09 05:53:38 +08:00
|
|
|
requested=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
|
|
|
"""
|
|
|
|
Helper function that recursively returns an object with the specified
|
|
|
|
related attributes already populated.
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
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
|
|
|
restricted = requested is not None
|
2006-05-02 09:31:56 +08:00
|
|
|
index_end = index_start + len(klass._meta.fields)
|
2008-07-27 09:18:23 +08:00
|
|
|
fields = row[index_start:index_end]
|
|
|
|
if not [x for x in fields if x is not None]:
|
|
|
|
# If we only have a list of Nones, there was not related object.
|
2008-08-27 04:44:20 +08:00
|
|
|
obj = None
|
|
|
|
else:
|
|
|
|
obj = klass(*fields)
|
2006-05-02 09:31:56 +08:00
|
|
|
for f in klass._meta.fields:
|
2008-06-29 17:40:17 +08:00
|
|
|
if not select_related_descend(f, restricted, requested):
|
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
|
|
|
continue
|
|
|
|
if restricted:
|
|
|
|
next = requested[f.name]
|
|
|
|
else:
|
|
|
|
next = None
|
|
|
|
cached_row = get_cached_row(f.rel.to, row, index_end, max_depth,
|
|
|
|
cur_depth+1, next)
|
|
|
|
if cached_row:
|
|
|
|
rel_obj, index_end = cached_row
|
2008-08-27 04:44:20 +08:00
|
|
|
if obj is not None:
|
|
|
|
setattr(obj, f.get_cache_name(), rel_obj)
|
2006-05-02 09:31:56 +08:00
|
|
|
return obj, index_end
|
|
|
|
|
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
|
|
|
def delete_objects(seen_objs):
|
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
|
|
|
Iterate through a list of seen classes, and remove any instances that are
|
|
|
|
referred to.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2008-06-22 04:57:05 +08:00
|
|
|
try:
|
|
|
|
ordered_classes = seen_objs.keys()
|
|
|
|
except CyclicDependency:
|
2008-07-09 05:53:38 +08:00
|
|
|
# If there is a cyclic dependency, we cannot in general delete the
|
|
|
|
# objects. However, if an appropriate transaction is set up, or if the
|
|
|
|
# database is lax enough, it will succeed. So for now, we go ahead and
|
|
|
|
# try anyway.
|
2008-06-22 04:57:05 +08:00
|
|
|
ordered_classes = seen_objs.unordered_keys()
|
|
|
|
|
|
|
|
obj_pairs = {}
|
2006-05-02 09:31:56 +08:00
|
|
|
for cls in ordered_classes:
|
2008-06-22 04:57:05 +08:00
|
|
|
items = seen_objs[cls].items()
|
|
|
|
items.sort()
|
|
|
|
obj_pairs[cls] = items
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
# Pre-notify all instances to be deleted.
|
2008-06-22 04:57:05 +08:00
|
|
|
for pk_val, instance in items:
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.pre_delete.send(sender=cls, instance=instance)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-06-22 04:57:05 +08:00
|
|
|
pk_list = [pk for pk,instance in items]
|
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 = sql.DeleteQuery(cls, connection)
|
|
|
|
del_query.delete_batch_related(pk_list)
|
|
|
|
|
|
|
|
update_query = sql.UpdateQuery(cls, connection)
|
2008-07-28 09:32:46 +08:00
|
|
|
for field, model in cls._meta.get_fields_with_model():
|
2008-07-27 12:18:52 +08:00
|
|
|
if (field.rel and field.null and field.rel.to in seen_objs and
|
|
|
|
filter(lambda f: f.column == field.column,
|
|
|
|
field.rel.to._meta.fields)):
|
2008-07-28 09:32:46 +08:00
|
|
|
if model:
|
|
|
|
sql.UpdateQuery(model, connection).clear_related(field,
|
|
|
|
pk_list)
|
|
|
|
else:
|
|
|
|
update_query.clear_related(field, pk_list)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-07-09 05:53:38 +08:00
|
|
|
# Now delete the actual data.
|
2006-05-02 09:31:56 +08:00
|
|
|
for cls in ordered_classes:
|
2008-06-22 04:57:05 +08:00
|
|
|
items = obj_pairs[cls]
|
|
|
|
items.reverse()
|
|
|
|
|
|
|
|
pk_list = [pk for pk,instance in items]
|
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 = sql.DeleteQuery(cls, connection)
|
|
|
|
del_query.delete_batch(pk_list)
|
|
|
|
|
|
|
|
# Last cleanup; set NULLs where there once was a reference to the
|
|
|
|
# object, NULL the primary key of the found objects, and perform
|
|
|
|
# post-notification.
|
2008-06-22 04:57:05 +08:00
|
|
|
for pk_val, instance in items:
|
2006-05-02 09:31:56 +08:00
|
|
|
for field in cls._meta.fields:
|
|
|
|
if field.rel and field.null and field.rel.to in seen_objs:
|
|
|
|
setattr(instance, field.attname, None)
|
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.post_delete.send(sender=cls, instance=instance)
|
2007-09-25 02:27:42 +08:00
|
|
|
setattr(instance, cls._meta.pk.attname, None)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
transaction.commit_unless_managed()
|
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
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
def insert_query(model, values, return_id=False, raw_values=False):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
query = sql.InsertQuery(model, connection)
|
|
|
|
query.insert_values(values, raw_values)
|
|
|
|
return query.execute_sql(return_id)
|