summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/db/models/query.py6
-rw-r--r--django/db/models/sql/query.py9
-rw-r--r--tests/regressiontests/queries/models.py4
3 files changed, 18 insertions, 1 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 5b9f504d2f..6c6a1991e1 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -667,6 +667,7 @@ class ValuesQuerySet(QuerySet):
Called by the _clone() method after initializing the rest of the
instance.
"""
+ self.query.clear_select_fields()
self.extra_names = []
if self._fields:
if not self.query.extra_select:
@@ -691,7 +692,10 @@ class ValuesQuerySet(QuerySet):
Cloning a ValuesQuerySet preserves the current fields.
"""
c = super(ValuesQuerySet, self)._clone(klass, **kwargs)
- c._fields = self._fields[:]
+ if not hasattr(c, '_fields'):
+ # Only clone self._fields if _fields wasn't passed into the cloning
+ # call directly.
+ c._fields = self._fields[:]
c.field_names = self.field_names
c.extra_names = self.extra_names
if setup and hasattr(c, '_setup_query'):
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index bc32ead930..1d4001bab0 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -1535,6 +1535,15 @@ class BaseQuery(object):
"""
return not (self.low_mark or self.high_mark)
+ def clear_select_fields(self):
+ """
+ Clears the list of fields to select (but not extra_select columns).
+ Some queryset types completely replace any existing list of select
+ columns.
+ """
+ self.select = []
+ self.select_fields = []
+
def add_fields(self, field_names, allow_m2m=True):
"""
Adds the given (model) fields to the select set. The field names are
diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py
index 868200f60c..50d81479a5 100644
--- a/tests/regressiontests/queries/models.py
+++ b/tests/regressiontests/queries/models.py
@@ -1020,6 +1020,10 @@ cases).
# optimise the inner query without losing results.
>>> Annotation.objects.exclude(tag__children__name="t2")
[<Annotation: a2>]
+
+Bug #9985 -- qs.values_list(...).values(...) combinations should work.
+>>> Note.objects.values_list("note", flat=True).values("id").order_by("id")
+[{'id': 1}, {'id': 2}, {'id': 3}]
"""}
# In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__