Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #9985 -- qs.values_list(...).values(...) was constructing incor…

…rect SQL.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9717 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c5bdfab9ae17953645812dc1ffc7f0b50354b7c5 1 parent d068ad0
@malcolmt malcolmt authored
View
6 django/db/models/query.py
@@ -680,6 +680,7 @@ def _setup_query(self):
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:
@@ -704,7 +705,10 @@ def _clone(self, klass=None, setup=False, **kwargs):
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'):
View
9 django/db/models/sql/query.py
@@ -1538,6 +1538,15 @@ def can_filter(self):
"""
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
View
6 tests/regressiontests/queries/models.py
@@ -1022,6 +1022,12 @@ class PointerB(models.Model):
>>> print Annotation.objects.filter(notes__in=Note.objects.filter(note="xyzzy")).query
SELECT ...
+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}]
+>>> Annotation.objects.filter(notes__in=Note.objects.filter(note="n1").values_list('note').values('id'))
+[<Annotation: a1>]
+
"""}
# In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__
Please sign in to comment.
Something went wrong with that request. Please try again.