Ticket #14930: extra_order_by_values_list-with_test.diff

File extra_order_by_values_list-with_test.diff, 2.4 KB (added by Ludovic Delaveau, 12 years ago)

Added test to patch

  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 0210a79..2f0a4cf 100644
    a b class ValuesQuerySet(QuerySet):  
    10211021            self.aggregate_names = None
    10221022
    10231023        self.query.select = []
    1024         if self.extra_names is not None:
    1025             self.query.set_extra_mask(self.extra_names)
     1024        extra_mask_names = self.extra_names + list(self.query.extra_order_by or [])
     1025        if extra_mask_names is not None:
     1026            self.query.set_extra_mask(extra_mask_names)
    10261027        self.query.add_fields(self.field_names, True)
    10271028        if self.aggregate_names is not None:
    10281029            self.query.set_aggregate_mask(self.aggregate_names)
    class ValuesListQuerySet(ValuesQuerySet):  
    10971098    def iterator(self):
    10981099        if self.flat and len(self._fields) == 1:
    10991100            for row in self.query.get_compiler(self.db).results_iter():
    1100                 yield row[0]
     1101                yield row[len(self.query.extra_select_mask)]
    11011102        elif not self.query.extra_select and not self.query.aggregate_select:
    11021103            for row in self.query.get_compiler(self.db).results_iter():
    11031104                yield tuple(row)
  • tests/regressiontests/queries/tests.py

    diff --git a/tests/regressiontests/queries/tests.py b/tests/regressiontests/queries/tests.py
    index 71ac107..4fd6717 100644
    a b class EmptyQuerySetTests(TestCase):  
    17001700
    17011701
    17021702class ValuesQuerysetTests(BaseQuerysetTest):
    1703     def test_flat_values_lits(self):
     1703    def setUp(self):
    17041704        Number.objects.create(num=72)
     1705
     1706    def test_flat_values_lits(self):
    17051707        qs = Number.objects.values_list("num")
    17061708        qs = qs.values_list("num", flat=True)
    17071709        self.assertValueQuerysetEqual(
    17081710            qs, [72]
    17091711        )
    17101712
     1713    def test_extra_values(self):
     1714        qs = Number.objects.extra(select={'value_plus_one': 'num+1'}).order_by('value_plus_one')
     1715        qs = qs.values('num')
     1716        identity = lambda x:x
     1717        self.assertQuerysetEqual(qs, [{'num': 72}], identity)
     1718
     1719    def test_extra_values_list(self):
     1720        qs = Number.objects.extra(select={'value_plus_one': 'num+1'}).order_by('value_plus_one')
     1721        qs = qs.values_list('num')
     1722        identity = lambda x:x
     1723        self.assertQuerysetEqual(qs, [(72,)], identity)
     1724
    17111725
    17121726class WeirdQuerysetSlicingTests(BaseQuerysetTest):
    17131727    def setUp(self):
Back to Top