Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

…ueryset,

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
  • Loading branch information...
commit 911e65ada72d6faeecd156d285ac661058aa0f35 1 parent e016a4f
Malcolm Tredinnick malcolmt authored
Showing with 16 additions and 9 deletions.
  1. +12 −9 django/db/models/sql/query.py
  2. +4 −0 docs/db-api.txt
21 django/db/models/sql/query.py
View
@@ -88,6 +88,7 @@ def __init__(self, model, connection, where=WhereNode):
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 @@ def clone(self, klass=None, **kwargs):
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 @@ def get_ordering(self):
"""
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 @@ def add_ordering(self, *ordering):
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 @@ def add_ordering(self, *ordering):
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):
"""
4 docs/db-api.txt
View
@@ -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.
Please sign in to comment.
Something went wrong with that request. Please try again.