From 559aca7d78cd6f0c24260ba9197579b2c9c53bd2 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Wed, 8 Oct 2008 09:45:27 +0000 Subject: [PATCH] A queryset that has had ordering removed (order_by()) can have ordering added again later (order_by('foo')). Or, at least, it can now. Thanks to Ilya Novoselov for diagnosing the problem here. git-svn-id: http://code.djangoproject.com/svn/django/trunk@9206 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/sql/query.py | 2 +- tests/regressiontests/queries/models.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 9ac0d0c960..01fbd3d002 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -608,7 +608,7 @@ class Query(object): if self.extra_order_by: ordering = self.extra_order_by elif not self.default_ordering: - ordering = [] + ordering = self.order_by else: ordering = self.order_by or self.model._meta.ordering qn = self.quote_name_unless_alias diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py index 12822c6f7a..fc21cef3cc 100644 --- a/tests/regressiontests/queries/models.py +++ b/tests/regressiontests/queries/models.py @@ -973,6 +973,12 @@ Make sure bump_prefix() (an internal Query method) doesn't (re-)break. >>> query.bump_prefix() >>> print query.as_sql()[0] SELECT U0."id" FROM "queries_tag" U0 + +Calling order_by() with no parameters removes any existing ordering on the +model. But it should still be possible to add new ordering after that. +>>> qs = Author.objects.order_by().order_by('name') +>>> 'ORDER BY' in qs.query.as_sql()[0] +True """} # In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__