1
0
mirror of https://github.com/django/django.git synced 2025-07-05 18:29:11 +00:00

queryset-refactor: Added a way to clear all default ordering from a queryset,

by calling order_by() with no parameters.


git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7042 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-01-28 14:27:16 +00:00
parent e016a4f987
commit 911e65ada7
2 changed files with 16 additions and 9 deletions

View File

@ -88,6 +88,7 @@ class Query(object):
self.rev_join_map = {} # Reverse of join_map.
self.quote_cache = {}
self.default_cols = True
self.default_ordering = True
# SQL-related attributes
self.select = []
@ -155,6 +156,7 @@ class Query(object):
obj.rev_join_map = copy.deepcopy(self.rev_join_map)
obj.quote_cache = {}
obj.default_cols = self.default_cols
obj.default_ordering = self.default_ordering
obj.select = self.select[:]
obj.tables = self.tables[:]
obj.where = copy.deepcopy(self.where)
@ -459,12 +461,9 @@ class Query(object):
"""
if self.extra_order_by:
ordering = self.extra_order_by
elif self.order_by is None:
elif not self.default_ordering:
ordering = []
else:
# Note that self.order_by can be empty in two ways: [] ("use the
# default"), which is handled here, and None ("no ordering"), which
# is handled in the previous test.
ordering = self.order_by or self.model._meta.ordering
qn = self.quote_name_unless_alias
distinct = self.distinct
@ -983,6 +982,8 @@ class Query(object):
clause. These items are either field names (not column names) --
possibly with a direction prefix ('-' or '?') -- or ordinals,
corresponding to column positions in the 'select' list.
If 'ordering' is empty, all ordering is cleared from the query.
"""
errors = []
for item in ordering:
@ -990,18 +991,20 @@ class Query(object):
errors.append(item)
if errors:
raise TypeError('Invalid order_by arguments: %s' % errors)
self.order_by.extend(ordering)
if ordering:
self.order_by.extend(ordering)
else:
self.default_ordering = False
def clear_ordering(self, force_empty=False):
"""
Removes any ordering settings. If 'force_empty' is True, there will be
no ordering in the resulting query (not even the model's default).
"""
if force_empty:
self.order_by = None
else:
self.order_by = []
self.order_by = []
self.extra_order_by = []
if force_empty:
self.default_ordering = False
def add_count_column(self):
"""

View File

@ -533,6 +533,10 @@ primary key if there is no ``Meta.ordering`` specified. For example::
...since the ``Blog`` model has no default ordering specified.
**New in Django development version:** If you don't want any ordering to be
applied to a query, not even the default ordering, call ``order_by()`` with no
parameters.
**New in Django development version:** The syntax for ordering across related
models has changed. See the `Django 0.96 documentation`_ for the old behaviour.