Ticket #25081: 25081.patch

File 25081.patch, 1.0 KB (added by Peter De Wachter, 9 years ago)

Patch with unit test

  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 1484d9d..96ecdbc 100644
    a b class QuerySet(object):  
    376376        keyword arguments.
    377377        """
    378378        clone = self.filter(*args, **kwargs)
    379         if self.query.can_filter():
     379        if self.query.can_filter() and not self.query.distinct_fields:
    380380            clone = clone.order_by()
    381381        num = len(clone)
    382382        if num == 1:
  • tests/distinct_on_fields/tests.py

    diff --git a/tests/distinct_on_fields/tests.py b/tests/distinct_on_fields/tests.py
    index 68290d7..e42fb9a 100644
    a b class DistinctOnTests(TestCase):  
    129129            qs, [self.p1_o2, self.p2_o1, self.p3_o1],
    130130            lambda x: x
    131131        )
     132
     133    def test_distinct_on_with_get(self):
     134        """get() respects order_by when distinct on is used."""
     135        s = Staff.objects.distinct('name').order_by('name', '-organisation').get(name='p1')
     136        self.assertEqual(s.organisation, 'o2')
Back to Top