Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #9985 -- qs.values_list(...).values(...) was constructi…

…ng incorrect SQL.

Backport of r9717 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9718 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit cc76b89c5967e77fcbb4f0772526387066822497 1 parent fe9cd02
Malcolm Tredinnick authored January 08, 2009
6  django/db/models/query.py
@@ -667,6 +667,7 @@ def _setup_query(self):
667 667
         Called by the _clone() method after initializing the rest of the
668 668
         instance.
669 669
         """
  670
+        self.query.clear_select_fields()
670 671
         self.extra_names = []
671 672
         if self._fields:
672 673
             if not self.query.extra_select:
@@ -691,7 +692,10 @@ def _clone(self, klass=None, setup=False, **kwargs):
691 692
         Cloning a ValuesQuerySet preserves the current fields.
692 693
         """
693 694
         c = super(ValuesQuerySet, self)._clone(klass, **kwargs)
694  
-        c._fields = self._fields[:]
  695
+        if not hasattr(c, '_fields'):
  696
+            # Only clone self._fields if _fields wasn't passed into the cloning
  697
+            # call directly.
  698
+            c._fields = self._fields[:]
695 699
         c.field_names = self.field_names
696 700
         c.extra_names = self.extra_names
697 701
         if setup and hasattr(c, '_setup_query'):
9  django/db/models/sql/query.py
@@ -1535,6 +1535,15 @@ def can_filter(self):
1535 1535
         """
1536 1536
         return not (self.low_mark or self.high_mark)
1537 1537
 
  1538
+    def clear_select_fields(self):
  1539
+        """
  1540
+        Clears the list of fields to select (but not extra_select columns).
  1541
+        Some queryset types completely replace any existing list of select
  1542
+        columns.
  1543
+        """
  1544
+        self.select = []
  1545
+        self.select_fields = []
  1546
+
1538 1547
     def add_fields(self, field_names, allow_m2m=True):
1539 1548
         """
1540 1549
         Adds the given (model) fields to the select set. The field names are
4  tests/regressiontests/queries/models.py
@@ -1020,6 +1020,10 @@ class PointerB(models.Model):
1020 1020
 # optimise the inner query without losing results.
1021 1021
 >>> Annotation.objects.exclude(tag__children__name="t2")
1022 1022
 [<Annotation: a2>]
  1023
+
  1024
+Bug #9985 -- qs.values_list(...).values(...) combinations should work.
  1025
+>>> Note.objects.values_list("note", flat=True).values("id").order_by("id")
  1026
+[{'id': 1}, {'id': 2}, {'id': 3}]
1023 1027
 """}
1024 1028
 
1025 1029
 # In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__

0 notes on commit cc76b89

Please sign in to comment.
Something went wrong with that request. Please try again.