4
from sets import Set as set # Python 2.3 fallback
6
from django.db import connection, transaction, IntegrityError
7
from django.db.models.fields import DateField
8
from django.db.models.query_utils import Q, select_related_descend
9
from django.db.models import signals, sql
10
from django.utils.datastructures import SortedDict
13
# Used to control how many objects are worked with at once in some cases (e.g.
14
# when deleting objects).
16
ITER_CHUNK_SIZE = CHUNK_SIZE
18
# Pull into this namespace for backwards compatibility.
19
EmptyResultSet = sql.EmptyResultSet
22
class CyclicDependency(Exception):
24
An error when dealing with a collection of objects that have a cyclic
25
dependency, i.e. when deleting multiple objects.
30
class CollectedObjects(object):
32
A container that stores keys and lists of values along with remembering the
33
parent objects for all the keys.
35
This is used for the database object deletion routines so that we can
36
calculate the 'leaf' objects which should be deleted first.
43
def add(self, model, pk, obj, parent_model, nullable=False):
45
Adds an item to the container.
48
* model - the class of the object being added.
49
* pk - the primary key.
50
* obj - the object itself.
51
* parent_model - the model of the parent object that this object was
53
* nullable - should be True if this relation is nullable.
55
Returns True if the item already existed in the structure and
58
d = self.data.setdefault(model, SortedDict())
61
# Nullable relationships can be ignored -- they are nulled out before
62
# deleting, and therefore do not affect the order in which objects
64
if parent_model is not None and not nullable:
65
self.children.setdefault(parent_model, []).append(model)
68
def __contains__(self, key):
69
return self.data.__contains__(key)
71
def __getitem__(self, key):
74
def __nonzero__(self):
75
return bool(self.data)
78
for k in self.ordered_keys():
82
return list(self.iteritems())
85
return self.ordered_keys()
87
def ordered_keys(self):
89
Returns the models in the order that they should be dealt with (i.e.
90
models with no dependencies first).
92
dealt_with = SortedDict()
93
# Start with items that have no children
94
models = self.data.keys()
95
while len(dealt_with) < len(models):
98
if model in dealt_with:
100
children = self.children.setdefault(model, [])
101
if len([c for c in children if c not in dealt_with]) == 0:
102
dealt_with[model] = None
105
raise CyclicDependency(
106
"There is a cyclic dependency of items to be processed.")
108
return dealt_with.keys()
110
def unordered_keys(self):
112
Fallback for the case where is a cyclic dependency but we don't care.
114
return self.data.keys()
117
class QuerySet(object):
119
Represents a lazy database lookup for a set of objects.
121
def __init__(self, model=None, query=None):
123
self.query = query or sql.Query(self.model, connection)
124
self._result_cache = None
126
self._sticky_filter = False
128
########################
129
# PYTHON MAGIC METHODS #
130
########################
132
def __getstate__(self):
134
Allows the QuerySet to be pickled.
136
# Force the cache to be fully populated.
139
obj_dict = self.__dict__.copy()
140
obj_dict['_iter'] = None
144
return repr(list(self))
147
# Since __len__ is called quite frequently (for example, as part of
148
# list(qs), we make some effort here to be as efficient as possible
149
# whilst not messing up any existing iterators against the QuerySet.
150
if self._result_cache is None:
152
self._result_cache = list(self._iter)
154
self._result_cache = list(self.iterator())
156
self._result_cache.extend(list(self._iter))
157
return len(self._result_cache)
160
if self._result_cache is None:
161
self._iter = self.iterator()
162
self._result_cache = []
164
return self._result_iter()
165
# Python's list iterator is better than our version when we're just
166
# iterating over the cache.
167
return iter(self._result_cache)
169
def _result_iter(self):
172
upper = len(self._result_cache)
174
yield self._result_cache[pos]
178
if len(self._result_cache) <= pos:
181
def __nonzero__(self):
182
if self._result_cache is not None:
183
return bool(self._result_cache)
186
except StopIteration:
190
def __getitem__(self, k):
192
Retrieves an item or slice from the set of results.
194
if not isinstance(k, (slice, int, long)):
196
assert ((not isinstance(k, slice) and (k >= 0))
197
or (isinstance(k, slice) and (k.start is None or k.start >= 0)
198
and (k.stop is None or k.stop >= 0))), \
199
"Negative indexing is not supported."
201
if self._result_cache is not None:
202
if self._iter is not None:
203
# The result cache has only been partially populated, so we may
204
# need to fill it out a bit more.
205
if isinstance(k, slice):
206
if k.stop is not None:
207
# Some people insist on passing in strings here.
213
if len(self._result_cache) < bound:
214
self._fill_cache(bound - len(self._result_cache))
215
return self._result_cache[k]
217
if isinstance(k, slice):
219
if k.start is not None:
223
if k.stop is not None:
227
qs.query.set_limits(start, stop)
228
return k.step and list(qs)[::k.step] or qs
231
qs.query.set_limits(k, k + 1)
233
except self.model.DoesNotExist, e:
234
raise IndexError, e.args
236
def __and__(self, other):
237
self._merge_sanity_check(other)
238
if isinstance(other, EmptyQuerySet):
239
return other._clone()
240
combined = self._clone()
241
combined.query.combine(other.query, sql.AND)
244
def __or__(self, other):
245
self._merge_sanity_check(other)
246
combined = self._clone()
247
if isinstance(other, EmptyQuerySet):
249
combined.query.combine(other.query, sql.OR)
252
####################################
253
# METHODS THAT DO DATABASE QUERIES #
254
####################################
258
An iterator over the results from applying this QuerySet to the
261
fill_cache = self.query.select_related
262
if isinstance(fill_cache, dict):
263
requested = fill_cache
266
max_depth = self.query.max_depth
267
extra_select = self.query.extra_select.keys()
268
index_start = len(extra_select)
269
for row in self.query.results_iter():
271
obj, _ = get_cached_row(self.model, row, index_start,
272
max_depth, requested=requested)
274
obj = self.model(*row[index_start:])
275
for i, k in enumerate(extra_select):
276
setattr(obj, k, row[i])
281
Performs a SELECT COUNT() and returns the number of records as an
284
If the QuerySet is already fully cached this simply returns the length
285
of the cached results set to avoid multiple SELECT COUNT(*) calls.
287
if self._result_cache is not None and not self._iter:
288
return len(self._result_cache)
290
return self.query.get_count()
292
def get(self, *args, **kwargs):
294
Performs the query and returns a single object matching the given
297
clone = self.filter(*args, **kwargs)
300
return clone._result_cache[0]
302
raise self.model.DoesNotExist("%s matching query does not exist."
303
% self.model._meta.object_name)
304
raise self.model.MultipleObjectsReturned("get() returned more than one %s -- it returned %s! Lookup parameters were %s"
305
% (self.model._meta.object_name, num, kwargs))
307
def create(self, **kwargs):
309
Creates a new object with the given kwargs, saving it to the database
310
and returning the created object.
312
obj = self.model(**kwargs)
313
obj.save(force_insert=True)
316
def get_or_create(self, **kwargs):
318
Looks up an object with the given kwargs, creating one if necessary.
319
Returns a tuple of (object, created), where created is a boolean
320
specifying whether an object was created.
323
'get_or_create() must be passed at least one keyword argument'
324
defaults = kwargs.pop('defaults', {})
326
return self.get(**kwargs), False
327
except self.model.DoesNotExist:
329
params = dict([(k, v) for k, v in kwargs.items() if '__' not in k])
330
params.update(defaults)
331
obj = self.model(**params)
332
sid = transaction.savepoint()
333
obj.save(force_insert=True)
334
transaction.savepoint_commit(sid)
336
except IntegrityError, e:
337
transaction.savepoint_rollback(sid)
339
return self.get(**kwargs), False
340
except self.model.DoesNotExist:
343
def latest(self, field_name=None):
345
Returns the latest object, according to the model's 'get_latest_by'
346
option or optional given field_name.
348
latest_by = field_name or self.model._meta.get_latest_by
349
assert bool(latest_by), "latest() requires either a field_name parameter or 'get_latest_by' in the model"
350
assert self.query.can_filter(), \
351
"Cannot change a query once a slice has been taken."
353
obj.query.set_limits(high=1)
354
obj.query.add_ordering('-%s' % latest_by)
357
def in_bulk(self, id_list):
359
Returns a dictionary mapping each of the given IDs to the object with
362
assert self.query.can_filter(), \
363
"Cannot use 'limit' or 'offset' with in_bulk"
364
assert isinstance(id_list, (tuple, list)), \
365
"in_bulk() must be provided with a list of IDs."
369
qs.query.add_filter(('pk__in', id_list))
370
return dict([(obj._get_pk_val(), obj) for obj in qs.iterator()])
374
Deletes the records in the current QuerySet.
376
assert self.query.can_filter(), \
377
"Cannot use 'limit' or 'offset' with delete."
379
del_query = self._clone()
381
# Disable non-supported fields.
382
del_query.query.select_related = False
383
del_query.query.clear_ordering()
385
# Delete objects in chunks to prevent the list of related objects from
388
# Collect all the objects to be deleted in this chunk, and all the
389
# objects that are related to the objects that are to be deleted.
390
seen_objs = CollectedObjects()
391
for object in del_query[:CHUNK_SIZE]:
392
object._collect_sub_objects(seen_objs)
396
delete_objects(seen_objs)
398
# Clear the result cache, in case this QuerySet gets reused.
399
self._result_cache = None
400
delete.alters_data = True
402
def update(self, **kwargs):
404
Updates all elements in the current QuerySet, setting all the given
405
fields to the appropriate values.
407
assert self.query.can_filter(), \
408
"Cannot update a query once a slice has been taken."
409
query = self.query.clone(sql.UpdateQuery)
410
query.add_update_values(kwargs)
411
rows = query.execute_sql(None)
412
transaction.commit_unless_managed()
413
self._result_cache = None
415
update.alters_data = True
417
def _update(self, values):
419
A version of update that accepts field objects instead of field names.
420
Used primarily for model saving and not intended for use by general
421
code (it requires too much poking around at model internals to be
422
useful at that level).
424
assert self.query.can_filter(), \
425
"Cannot update a query once a slice has been taken."
426
query = self.query.clone(sql.UpdateQuery)
427
query.add_update_fields(values)
428
self._result_cache = None
429
return query.execute_sql(None)
430
_update.alters_data = True
432
##################################################
433
# PUBLIC METHODS THAT RETURN A QUERYSET SUBCLASS #
434
##################################################
436
def values(self, *fields):
437
return self._clone(klass=ValuesQuerySet, setup=True, _fields=fields)
439
def values_list(self, *fields, **kwargs):
440
flat = kwargs.pop('flat', False)
442
raise TypeError('Unexpected keyword arguments to values_list: %s'
444
if flat and len(fields) > 1:
445
raise TypeError("'flat' is not valid when values_list is called with more than one field.")
446
return self._clone(klass=ValuesListQuerySet, setup=True, flat=flat,
449
def dates(self, field_name, kind, order='ASC'):
451
Returns a list of datetime objects representing all available dates for
452
the given field_name, scoped to 'kind'.
454
assert kind in ("month", "year", "day"), \
455
"'kind' must be one of 'year', 'month' or 'day'."
456
assert order in ('ASC', 'DESC'), \
457
"'order' must be either 'ASC' or 'DESC'."
458
return self._clone(klass=DateQuerySet, setup=True,
459
_field_name=field_name, _kind=kind, _order=order)
463
Returns an empty QuerySet.
465
return self._clone(klass=EmptyQuerySet)
467
##################################################################
468
# PUBLIC METHODS THAT ALTER ATTRIBUTES AND RETURN A NEW QUERYSET #
469
##################################################################
473
Returns a new QuerySet that is a copy of the current one. This allows a
474
QuerySet to proxy for a model manager in some cases.
478
def filter(self, *args, **kwargs):
480
Returns a new QuerySet instance with the args ANDed to the existing
483
return self._filter_or_exclude(False, *args, **kwargs)
485
def exclude(self, *args, **kwargs):
487
Returns a new QuerySet instance with NOT (args) ANDed to the existing
490
return self._filter_or_exclude(True, *args, **kwargs)
492
def _filter_or_exclude(self, negate, *args, **kwargs):
494
assert self.query.can_filter(), \
495
"Cannot filter a query once a slice has been taken."
497
clone = self._clone()
499
clone.query.add_q(~Q(*args, **kwargs))
501
clone.query.add_q(Q(*args, **kwargs))
504
def complex_filter(self, filter_obj):
506
Returns a new QuerySet instance with filter_obj added to the filters.
508
filter_obj can be a Q object (or anything with an add_to_query()
509
method) or a dictionary of keyword lookup arguments.
511
This exists to support framework features such as 'limit_choices_to',
512
and usually it will be more natural to use other methods.
514
if isinstance(filter_obj, Q) or hasattr(filter_obj, 'add_to_query'):
515
clone = self._clone()
516
clone.query.add_q(filter_obj)
519
return self._filter_or_exclude(None, **filter_obj)
521
def select_related(self, *fields, **kwargs):
523
Returns a new QuerySet instance that will select related objects.
525
If fields are specified, they must be ForeignKey fields and only those
526
related objects are included in the selection.
528
depth = kwargs.pop('depth', 0)
530
raise TypeError('Unexpected keyword arguments to select_related: %s'
535
raise TypeError('Cannot pass both "depth" and fields to select_related()')
536
obj.query.add_select_related(fields)
538
obj.query.select_related = True
540
obj.query.max_depth = depth
543
def dup_select_related(self, other):
545
Copies the related selection status from the QuerySet 'other' to the
548
self.query.select_related = other.query.select_related
550
def order_by(self, *field_names):
552
Returns a new QuerySet instance with the ordering changed.
554
assert self.query.can_filter(), \
555
"Cannot reorder a query once a slice has been taken."
557
obj.query.clear_ordering()
558
obj.query.add_ordering(*field_names)
561
def distinct(self, true_or_false=True):
563
Returns a new QuerySet instance that will select only distinct results.
566
obj.query.distinct = true_or_false
569
def extra(self, select=None, where=None, params=None, tables=None,
570
order_by=None, select_params=None):
572
Adds extra SQL fragments to the query.
574
assert self.query.can_filter(), \
575
"Cannot change a query once a slice has been taken"
576
clone = self._clone()
577
clone.query.add_extra(select, select_params, where, params, tables, order_by)
582
Reverses the ordering of the QuerySet.
584
clone = self._clone()
585
clone.query.standard_ordering = not clone.query.standard_ordering
592
def _clone(self, klass=None, setup=False, **kwargs):
594
klass = self.__class__
595
query = self.query.clone()
596
if self._sticky_filter:
597
query.filter_is_sticky = True
598
c = klass(model=self.model, query=query)
599
c.__dict__.update(kwargs)
600
if setup and hasattr(c, '_setup_query'):
604
def _fill_cache(self, num=None):
606
Fills the result cache with 'num' more entries (or until the results
607
iterator is exhausted).
611
for i in range(num or ITER_CHUNK_SIZE):
612
self._result_cache.append(self._iter.next())
613
except StopIteration:
616
def _next_is_sticky(self):
618
Indicates that the next filter call and the one following that should
619
be treated as a single filter. This is only important when it comes to
620
determining when to reuse tables for many-to-many filters. Required so
621
that we can filter naturally on the results of related managers.
623
This doesn't return a clone of the current QuerySet (it returns
624
"self"). The method is only used internally and should be immediately
625
followed by a filter() that does create a clone.
627
self._sticky_filter = True
630
def _merge_sanity_check(self, other):
632
Checks that we are merging two comparable QuerySet classes. By default
633
this does nothing, but see the ValuesQuerySet for an example of where
639
class ValuesQuerySet(QuerySet):
640
def __init__(self, *args, **kwargs):
641
super(ValuesQuerySet, self).__init__(*args, **kwargs)
642
# select_related isn't supported in values(). (FIXME -#3358)
643
self.query.select_related = False
645
# QuerySet.clone() will also set up the _fields attribute with the
646
# names of the model fields to select.
649
if (not self.extra_names and
650
len(self.field_names) != len(self.model._meta.fields)):
651
self.query.trim_extra_select(self.extra_names)
652
names = self.query.extra_select.keys() + self.field_names
653
for row in self.query.results_iter():
654
yield dict(zip(names, row))
656
def _setup_query(self):
658
Constructs the field_names list that the values query will be
661
Called by the _clone() method after initializing the rest of the
664
self.extra_names = []
666
if not self.query.extra_select:
667
field_names = list(self._fields)
670
for f in self._fields:
671
if self.query.extra_select.has_key(f):
672
self.extra_names.append(f)
674
field_names.append(f)
676
# Default to all fields.
677
field_names = [f.attname for f in self.model._meta.fields]
679
self.query.add_fields(field_names, False)
680
self.query.default_cols = False
681
self.field_names = field_names
683
def _clone(self, klass=None, setup=False, **kwargs):
685
Cloning a ValuesQuerySet preserves the current fields.
687
c = super(ValuesQuerySet, self)._clone(klass, **kwargs)
688
c._fields = self._fields[:]
689
c.field_names = self.field_names
690
c.extra_names = self.extra_names
691
if setup and hasattr(c, '_setup_query'):
695
def _merge_sanity_check(self, other):
696
super(ValuesQuerySet, self)._merge_sanity_check(other)
697
if (set(self.extra_names) != set(other.extra_names) or
698
set(self.field_names) != set(other.field_names)):
699
raise TypeError("Merging '%s' classes must involve the same values in each case."
700
% self.__class__.__name__)
703
class ValuesListQuerySet(ValuesQuerySet):
705
self.query.trim_extra_select(self.extra_names)
706
if self.flat and len(self._fields) == 1:
707
for row in self.query.results_iter():
709
elif not self.query.extra_select:
710
for row in self.query.results_iter():
713
# When extra(select=...) is involved, the extra cols come are
714
# always at the start of the row, so we need to reorder the fields
715
# to match the order in self._fields.
716
names = self.query.extra_select.keys() + self.field_names
717
for row in self.query.results_iter():
718
data = dict(zip(names, row))
719
yield tuple([data[f] for f in self._fields])
721
def _clone(self, *args, **kwargs):
722
clone = super(ValuesListQuerySet, self)._clone(*args, **kwargs)
723
clone.flat = self.flat
727
class DateQuerySet(QuerySet):
729
return self.query.results_iter()
731
def _setup_query(self):
733
Sets up any special features of the query attribute.
735
Called by the _clone() method after initializing the rest of the
738
self.query = self.query.clone(klass=sql.DateQuery, setup=True)
739
self.query.select = []
740
field = self.model._meta.get_field(self._field_name, many_to_many=False)
741
assert isinstance(field, DateField), "%r isn't a DateField." \
743
self.query.add_date_select(field, self._kind, self._order)
745
self.query.add_filter(('%s__isnull' % field.name, False))
747
def _clone(self, klass=None, setup=False, **kwargs):
748
c = super(DateQuerySet, self)._clone(klass, False, **kwargs)
749
c._field_name = self._field_name
751
if setup and hasattr(c, '_setup_query'):
756
class EmptyQuerySet(QuerySet):
757
def __init__(self, model=None, query=None):
758
super(EmptyQuerySet, self).__init__(model, query)
759
self._result_cache = []
761
def __and__(self, other):
764
def __or__(self, other):
765
return other._clone()
773
def _clone(self, klass=None, setup=False, **kwargs):
774
c = super(EmptyQuerySet, self)._clone(klass, **kwargs)
779
# This slightly odd construction is because we need an empty generator
780
# (it raises StopIteration immediately).
781
yield iter([]).next()
784
def get_cached_row(klass, row, index_start, max_depth=0, cur_depth=0,
787
Helper function that recursively returns an object with the specified
788
related attributes already populated.
790
if max_depth and requested is None and cur_depth > max_depth:
791
# We've recursed deeply enough; stop now.
794
restricted = requested is not None
795
index_end = index_start + len(klass._meta.fields)
796
fields = row[index_start:index_end]
797
if not [x for x in fields if x is not None]:
798
# If we only have a list of Nones, there was not related object.
802
for f in klass._meta.fields:
803
if not select_related_descend(f, restricted, requested):
806
next = requested[f.name]
809
cached_row = get_cached_row(f.rel.to, row, index_end, max_depth,
812
rel_obj, index_end = cached_row
814
setattr(obj, f.get_cache_name(), rel_obj)
815
return obj, index_end
818
def delete_objects(seen_objs):
820
Iterate through a list of seen classes, and remove any instances that are
824
ordered_classes = seen_objs.keys()
825
except CyclicDependency:
826
# If there is a cyclic dependency, we cannot in general delete the
827
# objects. However, if an appropriate transaction is set up, or if the
828
# database is lax enough, it will succeed. So for now, we go ahead and
830
ordered_classes = seen_objs.unordered_keys()
833
for cls in ordered_classes:
834
items = seen_objs[cls].items()
836
obj_pairs[cls] = items
838
# Pre-notify all instances to be deleted.
839
for pk_val, instance in items:
840
signals.pre_delete.send(sender=cls, instance=instance)
842
pk_list = [pk for pk,instance in items]
843
del_query = sql.DeleteQuery(cls, connection)
844
del_query.delete_batch_related(pk_list)
846
update_query = sql.UpdateQuery(cls, connection)
847
for field, model in cls._meta.get_fields_with_model():
848
if (field.rel and field.null and field.rel.to in seen_objs and
849
filter(lambda f: f.column == field.column,
850
field.rel.to._meta.fields)):
852
sql.UpdateQuery(model, connection).clear_related(field,
855
update_query.clear_related(field, pk_list)
857
# Now delete the actual data.
858
for cls in ordered_classes:
859
items = obj_pairs[cls]
862
pk_list = [pk for pk,instance in items]
863
del_query = sql.DeleteQuery(cls, connection)
864
del_query.delete_batch(pk_list)
866
# Last cleanup; set NULLs where there once was a reference to the
867
# object, NULL the primary key of the found objects, and perform
869
for pk_val, instance in items:
870
for field in cls._meta.fields:
871
if field.rel and field.null and field.rel.to in seen_objs:
872
setattr(instance, field.attname, None)
874
signals.post_delete.send(sender=cls, instance=instance)
875
setattr(instance, cls._meta.pk.attname, None)
877
transaction.commit_unless_managed()
880
def insert_query(model, values, return_id=False, raw_values=False):
882
Inserts a new record for the given model. This provides an interface to
883
the InsertQuery class and is how Model.save() is implemented. It is not
884
part of the public API.
886
query = sql.InsertQuery(model, connection)
887
query.insert_values(values, raw_values)
888
return query.execute_sql(return_id)