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 #10256 -- Corrected the interaction of extra(select=) w…

…ith values() and values_list() where an explicit list of columns is requested.

Merge of r9837 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9884 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c2807db68371dd84edb8244e748cb14a8a30c6df 1 parent 8366ef4
Russell Keith-Magee authored February 22, 2009
39  django/db/models/query.py
@@ -652,10 +652,11 @@ def __init__(self, *args, **kwargs):
652 652
         # names of the model fields to select.
653 653
 
654 654
     def iterator(self):
655  
-        if (not self.extra_names and
656  
-            len(self.field_names) != len(self.model._meta.fields)):
  655
+        # Purge any extra columns that haven't been explicitly asked for
  656
+        if self.extra_names is not None:
657 657
             self.query.trim_extra_select(self.extra_names)
658 658
         names = self.query.extra_select.keys() + self.field_names
  659
+
659 660
         for row in self.query.results_iter():
660 661
             yield dict(zip(names, row))
661 662
 
@@ -668,24 +669,25 @@ def _setup_query(self):
668 669
         instance.
669 670
         """
670 671
         self.query.clear_select_fields()
671  
-        self.extra_names = []
  672
+
672 673
         if self._fields:
  674
+            self.extra_names = []
673 675
             if not self.query.extra_select:
674  
-                field_names = list(self._fields)
  676
+                self.field_names = list(self._fields)
675 677
             else:
676  
-                field_names = []
  678
+                self.query.default_cols = False
  679
+                self.field_names = []
677 680
                 for f in self._fields:
678 681
                     if self.query.extra_select.has_key(f):
679 682
                         self.extra_names.append(f)
680 683
                     else:
681  
-                        field_names.append(f)
  684
+                        self.field_names.append(f)
682 685
         else:
683 686
             # Default to all fields.
684  
-            field_names = [f.attname for f in self.model._meta.fields]
  687
+            self.extra_names = None
  688
+            self.field_names = [f.attname for f in self.model._meta.fields]
685 689
 
686  
-        self.query.add_fields(field_names, False)
687  
-        self.query.default_cols = False
688  
-        self.field_names = field_names
  690
+        self.query.add_fields(self.field_names, False)
689 691
 
690 692
     def _clone(self, klass=None, setup=False, **kwargs):
691 693
         """
@@ -712,7 +714,8 @@ def _merge_sanity_check(self, other):
712 714
 
713 715
 class ValuesListQuerySet(ValuesQuerySet):
714 716
     def iterator(self):
715  
-        self.query.trim_extra_select(self.extra_names)
  717
+        if self.extra_names is not None:
  718
+            self.query.trim_extra_select(self.extra_names)
716 719
         if self.flat and len(self._fields) == 1:
717 720
             for row in self.query.results_iter():
718 721
                 yield row[0]
@@ -720,13 +723,21 @@ def iterator(self):
720 723
             for row in self.query.results_iter():
721 724
                 yield tuple(row)
722 725
         else:
723  
-            # When extra(select=...) is involved, the extra cols come are
724  
-            # always at the start of the row, so we need to reorder the fields
  726
+            # When extra(select=...) is involved, the extra cols are
  727
+            # always at the start of the row, and we need to reorder the fields
725 728
             # to match the order in self._fields.
726 729
             names = self.query.extra_select.keys() + self.field_names
  730
+
  731
+            # If a field list has been specified, use it. Otherwise, use the
  732
+            # full list of fields, including extras.
  733
+            if self._fields:
  734
+                fields = self._fields
  735
+            else:
  736
+                fields = names
  737
+
727 738
             for row in self.query.results_iter():
728 739
                 data = dict(zip(names, row))
729  
-                yield tuple([data[f] for f in self._fields])
  740
+                yield tuple([data[f] for f in fields])
730 741
 
731 742
     def _clone(self, *args, **kwargs):
732 743
         clone = super(ValuesListQuerySet, self)._clone(*args, **kwargs)
76  tests/regressiontests/extra_regress/models.py
@@ -30,6 +30,11 @@ class Order(models.Model):
30 30
     created_by = models.ForeignKey(User)
31 31
     text = models.TextField()
32 32
 
  33
+class TestObject(models.Model):
  34
+    first = models.CharField(max_length=20)
  35
+    second = models.CharField(max_length=20)
  36
+    third = models.CharField(max_length=20)
  37
+
33 38
 __test__ = {"API_TESTS": """
34 39
 # Regression tests for #7314 and #7372
35 40
 
@@ -115,4 +120,75 @@ class Order(models.Model):
115 120
 # cause incorrect SQL to be produced otherwise.
116 121
 >>> RevisionableModel.objects.extra(select={"the_answer": 'id'}).dates('when', 'month')
117 122
 [datetime.datetime(2008, 9, 1, 0, 0)]
  123
+
  124
+# Regression test for #10256... If there is a values() clause, Extra columns are
  125
+# only returned if they are explicitly mentioned.
  126
+>>> TestObject(first='first', second='second', third='third').save()
  127
+
  128
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values()
  129
+[{'bar': u'second', 'third': u'third', 'second': u'second', 'whiz': u'third', 'foo': u'first', 'id': 1, 'first': u'first'}]
  130
+
  131
+# Extra clauses after an empty values clause are still included
  132
+>>> TestObject.objects.values().extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third'))))
  133
+[{'bar': u'second', 'third': u'third', 'second': u'second', 'whiz': u'third', 'foo': u'first', 'id': 1, 'first': u'first'}]
  134
+
  135
+# Extra columns are ignored if not mentioned in the values() clause
  136
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values('first', 'second')
  137
+[{'second': u'second', 'first': u'first'}]
  138
+
  139
+# Extra columns after a non-empty values() clause are ignored
  140
+>>> TestObject.objects.values('first', 'second').extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third'))))
  141
+[{'second': u'second', 'first': u'first'}]
  142
+
  143
+# Extra columns can be partially returned
  144
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values('first', 'second', 'foo')
  145
+[{'second': u'second', 'foo': u'first', 'first': u'first'}]
  146
+
  147
+# Also works if only extra columns are included
  148
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values('foo', 'whiz')
  149
+[{'foo': u'first', 'whiz': u'third'}]
  150
+
  151
+# Values list works the same way
  152
+# All columns are returned for an empty values_list()
  153
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list()
  154
+[(u'first', u'second', u'third', 1, u'first', u'second', u'third')]
  155
+
  156
+# Extra columns after an empty values_list() are still included
  157
+>>> TestObject.objects.values_list().extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third'))))
  158
+[(u'first', u'second', u'third', 1, u'first', u'second', u'third')]
  159
+
  160
+# Extra columns ignored completely if not mentioned in values_list()
  161
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('first', 'second')
  162
+[(u'first', u'second')]
  163
+
  164
+# Extra columns after a non-empty values_list() clause are ignored completely
  165
+>>> TestObject.objects.values_list('first', 'second').extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third'))))
  166
+[(u'first', u'second')]
  167
+
  168
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('second', flat=True)
  169
+[u'second']
  170
+
  171
+# Only the extra columns specified in the values_list() are returned
  172
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('first', 'second', 'whiz')
  173
+[(u'first', u'second', u'third')]
  174
+
  175
+# ...also works if only extra columns are included
  176
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('foo','whiz')
  177
+[(u'first', u'third')]
  178
+
  179
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('whiz', flat=True)
  180
+[u'third']
  181
+
  182
+# ... and values are returned in the order they are specified
  183
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('whiz','foo')
  184
+[(u'third', u'first')]
  185
+
  186
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('first','id')
  187
+[(u'first', 1)]
  188
+
  189
+>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('whiz', 'first', 'bar', 'id')
  190
+[(u'third', u'first', u'second', 1)]
  191
+
118 192
 """}
  193
+
  194
+

0 notes on commit c2807db

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