Code

Ticket #14930: extra_order_by_values_list-with_test.diff

File extra_order_by_values_list-with_test.diff, 2.4 KB (added by vicould, 22 months ago)

Added test to patch

Line 
1diff --git a/django/db/models/query.py b/django/db/models/query.py
2index 0210a79..2f0a4cf 100644
3--- a/django/db/models/query.py
4+++ b/django/db/models/query.py
5@@ -1021,8 +1021,9 @@ class ValuesQuerySet(QuerySet):
6             self.aggregate_names = None
7 
8         self.query.select = []
9-        if self.extra_names is not None:
10-            self.query.set_extra_mask(self.extra_names)
11+        extra_mask_names = self.extra_names + list(self.query.extra_order_by or [])
12+        if extra_mask_names is not None:
13+            self.query.set_extra_mask(extra_mask_names)
14         self.query.add_fields(self.field_names, True)
15         if self.aggregate_names is not None:
16             self.query.set_aggregate_mask(self.aggregate_names)
17@@ -1097,7 +1098,7 @@ class ValuesListQuerySet(ValuesQuerySet):
18     def iterator(self):
19         if self.flat and len(self._fields) == 1:
20             for row in self.query.get_compiler(self.db).results_iter():
21-                yield row[0]
22+                yield row[len(self.query.extra_select_mask)]
23         elif not self.query.extra_select and not self.query.aggregate_select:
24             for row in self.query.get_compiler(self.db).results_iter():
25                 yield tuple(row)
26diff --git a/tests/regressiontests/queries/tests.py b/tests/regressiontests/queries/tests.py
27index 71ac107..4fd6717 100644
28--- a/tests/regressiontests/queries/tests.py
29+++ b/tests/regressiontests/queries/tests.py
30@@ -1700,14 +1700,28 @@ class EmptyQuerySetTests(TestCase):
31 
32 
33 class ValuesQuerysetTests(BaseQuerysetTest):
34-    def test_flat_values_lits(self):
35+    def setUp(self):
36         Number.objects.create(num=72)
37+
38+    def test_flat_values_lits(self):
39         qs = Number.objects.values_list("num")
40         qs = qs.values_list("num", flat=True)
41         self.assertValueQuerysetEqual(
42             qs, [72]
43         )
44 
45+    def test_extra_values(self):
46+        qs = Number.objects.extra(select={'value_plus_one': 'num+1'}).order_by('value_plus_one')
47+        qs = qs.values('num')
48+        identity = lambda x:x
49+        self.assertQuerysetEqual(qs, [{'num': 72}], identity)
50+
51+    def test_extra_values_list(self):
52+        qs = Number.objects.extra(select={'value_plus_one': 'num+1'}).order_by('value_plus_one')
53+        qs = qs.values_list('num')
54+        identity = lambda x:x
55+        self.assertQuerysetEqual(qs, [(72,)], identity)
56+
57 
58 class WeirdQuerysetSlicingTests(BaseQuerysetTest):
59     def setUp(self):