Added support for modifying the effect of `DISTINCT` clauses so they

only consider some fields (PostgreSQL only).

For this, the ``distinct()`` QuerySet method now accepts an optional
list of model fields names and generates ``DISTINCT ON`` clauses on
these cases. Thanks Jeffrey Gelens and Anssi Kääriäinen for their work.

Fixes #6422.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17244 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Ramiro Morales 2011-12-22 20:42:40 +00:00
parent 03eb2907d5
commit 287565779d
16 changed files with 374 additions and 43 deletions

View file

@ -234,18 +234,22 @@ class Queries1Tests(BaseQuerysetTest):
['<Item: four>', '<Item: one>']
)
# FIXME: This is difficult to fix and very much an edge case, so punt for
# now. This is related to the order_by() tests for ticket #2253, but the
# old bug exhibited itself here (q2 was pulling too many tables into the
# combined query with the new ordering, but only because we have evaluated
# q2 already).
@unittest.expectedFailure
def test_order_by_tables(self):
q1 = Item.objects.order_by('name')
q2 = Item.objects.filter(id=self.i1.id)
list(q2)
self.assertEqual(len((q1 & q2).order_by('name').query.tables), 1)
def test_order_by_join_unref(self):
"""
This test is related to the above one, testing that there aren't
old JOINs in the query.
"""
qs = Celebrity.objects.order_by('greatest_fan__fan_of')
self.assertIn('OUTER JOIN', str(qs.query))
qs = qs.order_by('id')
self.assertNotIn('OUTER JOIN', str(qs.query))
def test_tickets_4088_4306(self):
self.assertQuerysetEqual(
Report.objects.filter(creator=1001),
@ -1728,7 +1732,7 @@ class ToFieldTests(TestCase):
class ConditionalTests(BaseQuerysetTest):
"""Tests whose execution depend on dfferent environment conditions like
"""Tests whose execution depend on different environment conditions like
Python version or DB backend features"""
def setUp(self):
@ -1739,6 +1743,7 @@ class ConditionalTests(BaseQuerysetTest):
t4 = Tag.objects.create(name='t4', parent=t3)
t5 = Tag.objects.create(name='t5', parent=t3)
# In Python 2.6 beta releases, exceptions raised in __len__ are swallowed
# (Python issue 1242657), so these cases return an empty list, rather than
# raising an exception. Not a lot we can do about that, unfortunately, due to
@ -1810,6 +1815,7 @@ class ConditionalTests(BaseQuerysetTest):
2500
)
class UnionTests(unittest.TestCase):
"""
Tests for the union of two querysets. Bug #12252.