2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2010-09-28 21:42:09 +08:00
|
|
|
|
2011-10-14 02:51:33 +08:00
|
|
|
import datetime
|
2015-01-28 20:35:27 +08:00
|
|
|
from collections import OrderedDict
|
2010-09-28 21:42:09 +08:00
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
2011-10-14 02:51:33 +08:00
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import Order, RevisionableModel, TestObject
|
2010-09-28 21:42:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ExtraRegressTests(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.u = User.objects.create_user(
|
2013-10-20 07:33:10 +08:00
|
|
|
username="fred",
|
|
|
|
password="secret",
|
|
|
|
email="fred@example.com"
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_regression_7314_7372(self):
|
|
|
|
"""
|
|
|
|
Regression tests for #7314 and #7372
|
|
|
|
"""
|
|
|
|
rm = RevisionableModel.objects.create(
|
|
|
|
title='First Revision',
|
|
|
|
when=datetime.datetime(2008, 9, 28, 10, 30, 0)
|
|
|
|
)
|
|
|
|
self.assertEqual(rm.pk, rm.base.pk)
|
|
|
|
|
|
|
|
rm2 = rm.new_revision()
|
|
|
|
rm2.title = "Second Revision"
|
|
|
|
rm.when = datetime.datetime(2008, 9, 28, 14, 25, 0)
|
|
|
|
rm2.save()
|
|
|
|
|
|
|
|
self.assertEqual(rm2.title, 'Second Revision')
|
|
|
|
self.assertEqual(rm2.base.title, 'First Revision')
|
|
|
|
|
|
|
|
self.assertNotEqual(rm2.pk, rm.pk)
|
|
|
|
self.assertEqual(rm2.base.pk, rm.pk)
|
|
|
|
|
|
|
|
# Queryset to match most recent revision:
|
|
|
|
qs = RevisionableModel.objects.extra(
|
2013-10-20 07:33:10 +08:00
|
|
|
where=["%(table)s.id IN (SELECT MAX(rev.id) FROM %(table)s rev GROUP BY rev.base_id)" % {
|
|
|
|
'table': RevisionableModel._meta.db_table,
|
|
|
|
}]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
qs, [('Second Revision', 'First Revision')],
|
2010-09-28 21:42:09 +08:00
|
|
|
transform=lambda r: (r.title, r.base.title)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Queryset to search for string in title:
|
|
|
|
qs2 = RevisionableModel.objects.filter(title__contains="Revision")
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
qs2, [
|
2010-09-28 21:42:09 +08:00
|
|
|
('First Revision', 'First Revision'),
|
|
|
|
('Second Revision', 'First Revision'),
|
|
|
|
],
|
2012-12-13 19:33:11 +08:00
|
|
|
transform=lambda r: (r.title, r.base.title),
|
|
|
|
ordered=False
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Following queryset should return the most recent revision:
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
qs & qs2,
|
2010-09-28 21:42:09 +08:00
|
|
|
[('Second Revision', 'First Revision')],
|
2012-12-13 19:33:11 +08:00
|
|
|
transform=lambda r: (r.title, r.base.title),
|
|
|
|
ordered=False
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_extra_stay_tied(self):
|
|
|
|
# Extra select parameters should stay tied to their corresponding
|
|
|
|
# select portions. Applies when portions are updated or otherwise
|
|
|
|
# moved around.
|
|
|
|
qs = User.objects.extra(
|
2013-11-03 03:37:48 +08:00
|
|
|
select=OrderedDict((("alpha", "%s"), ("beta", "2"), ("gamma", "%s"))),
|
2013-10-20 07:33:10 +08:00
|
|
|
select_params=(1, 3)
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
qs = qs.extra(select={"beta": 4})
|
|
|
|
qs = qs.extra(select={"alpha": "%s"}, select_params=[5])
|
|
|
|
self.assertEqual(
|
|
|
|
list(qs.filter(id=self.u.id).values('alpha', 'beta', 'gamma')),
|
|
|
|
[{'alpha': 5, 'beta': 4, 'gamma': 3}]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_regression_7957(self):
|
|
|
|
"""
|
|
|
|
Regression test for #7957: Combining extra() calls should leave the
|
|
|
|
corresponding parameters associated with the right extra() bit. I.e.
|
|
|
|
internal dictionary must remain sorted.
|
|
|
|
"""
|
|
|
|
self.assertEqual(
|
2013-12-09 01:20:06 +08:00
|
|
|
(User.objects
|
|
|
|
.extra(select={"alpha": "%s"}, select_params=(1,))
|
|
|
|
.extra(select={"beta": "%s"}, select_params=(2,))[0].alpha),
|
|
|
|
1
|
|
|
|
)
|
2010-09-28 21:42:09 +08:00
|
|
|
|
|
|
|
self.assertEqual(
|
2013-12-09 01:20:06 +08:00
|
|
|
(User.objects
|
|
|
|
.extra(select={"beta": "%s"}, select_params=(1,))
|
|
|
|
.extra(select={"alpha": "%s"}, select_params=(2,))[0].alpha),
|
|
|
|
2
|
|
|
|
)
|
2010-09-28 21:42:09 +08:00
|
|
|
|
|
|
|
def test_regression_7961(self):
|
|
|
|
"""
|
|
|
|
Regression test for #7961: When not using a portion of an
|
|
|
|
extra(...) in a query, remove any corresponding parameters from the
|
|
|
|
query as well.
|
|
|
|
"""
|
|
|
|
self.assertEqual(
|
2016-04-08 10:04:45 +08:00
|
|
|
list(User.objects.extra(select={"alpha": "%s"}, select_params=(-6,))
|
|
|
|
.filter(id=self.u.id).values_list('id', flat=True)),
|
2010-09-28 21:42:09 +08:00
|
|
|
[self.u.id]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_regression_8063(self):
|
|
|
|
"""
|
|
|
|
Regression test for #8063: limiting a query shouldn't discard any
|
|
|
|
extra() bits.
|
|
|
|
"""
|
|
|
|
qs = User.objects.all().extra(where=['id=%s'], params=[self.u.id])
|
|
|
|
self.assertQuerysetEqual(qs, ['<User: fred>'])
|
|
|
|
self.assertQuerysetEqual(qs[:1], ['<User: fred>'])
|
|
|
|
|
|
|
|
def test_regression_8039(self):
|
|
|
|
"""
|
|
|
|
Regression test for #8039: Ordering sometimes removed relevant tables
|
|
|
|
from extra(). This test is the critical case: ordering uses a table,
|
2014-03-02 22:25:53 +08:00
|
|
|
but then removes the reference because of an optimization. The table
|
2010-09-28 21:42:09 +08:00
|
|
|
should still be present because of the extra() call.
|
|
|
|
"""
|
|
|
|
self.assertQuerysetEqual(
|
2013-12-09 01:20:06 +08:00
|
|
|
(Order.objects
|
|
|
|
.extra(where=["username=%s"], params=["fred"], tables=["auth_user"])
|
|
|
|
.order_by('created_by')),
|
2013-10-20 07:33:10 +08:00
|
|
|
[]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_regression_8819(self):
|
|
|
|
"""
|
|
|
|
Regression test for #8819: Fields in the extra(select=...) list
|
|
|
|
should be available to extra(order_by=...).
|
|
|
|
"""
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
User.objects.filter(pk=self.u.id).extra(select={'extra_field': 1}).distinct(),
|
|
|
|
['<User: fred>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
User.objects.filter(pk=self.u.id).extra(select={'extra_field': 1}, order_by=['extra_field']),
|
|
|
|
['<User: fred>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
User.objects.filter(pk=self.u.id).extra(select={'extra_field': 1}, order_by=['extra_field']).distinct(),
|
|
|
|
['<User: fred>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_dates_query(self):
|
|
|
|
"""
|
|
|
|
When calling the dates() method on a queryset with extra selection
|
|
|
|
columns, we can (and should) ignore those columns. They don't change
|
|
|
|
the result and cause incorrect SQL to be produced otherwise.
|
|
|
|
"""
|
2013-10-19 20:31:38 +08:00
|
|
|
RevisionableModel.objects.create(
|
2010-09-28 21:42:09 +08:00
|
|
|
title='First Revision',
|
|
|
|
when=datetime.datetime(2008, 9, 28, 10, 30, 0)
|
|
|
|
)
|
|
|
|
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(
|
2013-02-10 23:15:49 +08:00
|
|
|
RevisionableModel.objects.extra(select={"the_answer": 'id'}).datetimes('when', 'month'),
|
|
|
|
[datetime.datetime(2008, 9, 1, 0, 0)],
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_values_with_extra(self):
|
|
|
|
"""
|
|
|
|
Regression test for #10256... If there is a values() clause, Extra
|
|
|
|
columns are only returned if they are explicitly mentioned.
|
|
|
|
"""
|
|
|
|
obj = TestObject(first='first', second='second', third='third')
|
|
|
|
obj.save()
|
|
|
|
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
.values()
|
|
|
|
),
|
|
|
|
[{
|
|
|
|
'bar': 'second', 'third': 'third', 'second': 'second', 'whiz': 'third', 'foo': 'first',
|
|
|
|
'id': obj.pk, 'first': 'first'
|
|
|
|
}]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Extra clauses after an empty values clause are still included
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.values()
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
),
|
|
|
|
[{
|
|
|
|
'bar': 'second', 'third': 'third', 'second': 'second', 'whiz': 'third', 'foo': 'first',
|
|
|
|
'id': obj.pk, 'first': 'first'
|
|
|
|
}]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Extra columns are ignored if not mentioned in the values() clause
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
.values('first', 'second')
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
[{'second': 'second', 'first': 'first'}]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Extra columns after a non-empty values() clause are ignored
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.values('first', 'second')
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
[{'second': 'second', 'first': 'first'}]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Extra columns can be partially returned
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
.values('first', 'second', 'foo')
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
[{'second': 'second', 'foo': 'first', 'first': 'first'}]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Also works if only extra columns are included
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
.values('foo', 'whiz')
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
[{'foo': 'first', 'whiz': 'third'}]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Values list works the same way
|
|
|
|
# All columns are returned for an empty values_list()
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
.values_list()
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
[('first', 'second', 'third', obj.pk, 'first', 'second', 'third')]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Extra columns after an empty values_list() are still included
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.values_list()
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
[('first', 'second', 'third', obj.pk, 'first', 'second', 'third')]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Extra columns ignored completely if not mentioned in values_list()
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
.values_list('first', 'second')
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
[('first', 'second')]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Extra columns after a non-empty values_list() clause are ignored completely
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.values_list('first', 'second')
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
[('first', 'second')]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
.values_list('second', flat=True)
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
['second']
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Only the extra columns specified in the values_list() are returned
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
.values_list('first', 'second', 'whiz')
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
[('first', 'second', 'third')]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# ...also works if only extra columns are included
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
.values_list('foo', 'whiz')
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
[('first', 'third')]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
.values_list('whiz', flat=True)
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
['third']
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# ... and values are returned in the order they are specified
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
.values_list('whiz', 'foo')
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
[('third', 'first')]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
.values_list('first', 'id')
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
[('first', obj.pk)]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
list(
|
|
|
|
TestObject.objects
|
|
|
|
.extra(select=OrderedDict((('foo', 'first'), ('bar', 'second'), ('whiz', 'third'))))
|
|
|
|
.values_list('whiz', 'first', 'bar', 'id')
|
|
|
|
),
|
2012-06-08 00:08:47 +08:00
|
|
|
[('third', 'first', 'second', obj.pk)]
|
2010-09-28 21:42:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_regression_10847(self):
|
|
|
|
"""
|
|
|
|
Regression for #10847: the list of extra columns can always be
|
|
|
|
accurately evaluated. Using an inner query ensures that as_sql() is
|
|
|
|
producing correct output without requiring full evaluation and
|
|
|
|
execution of the inner query.
|
|
|
|
"""
|
|
|
|
obj = TestObject(first='first', second='second', third='third')
|
|
|
|
obj.save()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
list(TestObject.objects.extra(select={'extra': 1}).values('pk')),
|
|
|
|
[{'pk': obj.pk}]
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
TestObject.objects.filter(
|
2013-10-20 07:33:10 +08:00
|
|
|
pk__in=TestObject.objects.extra(select={'extra': 1}).values('pk')
|
2010-09-28 21:42:09 +08:00
|
|
|
),
|
|
|
|
['<TestObject: TestObject: first,second,third>']
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
list(TestObject.objects.values('pk').extra(select={'extra': 1})),
|
|
|
|
[{'pk': obj.pk}]
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
TestObject.objects.filter(
|
2013-10-18 06:27:45 +08:00
|
|
|
pk__in=TestObject.objects.values('pk').extra(select={'extra': 1})
|
2010-09-28 21:42:09 +08:00
|
|
|
),
|
|
|
|
['<TestObject: TestObject: first,second,third>']
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
TestObject.objects.filter(pk=obj.pk) | TestObject.objects.extra(where=["id > %s"], params=[obj.pk]),
|
2010-09-28 21:42:09 +08:00
|
|
|
['<TestObject: TestObject: first,second,third>']
|
|
|
|
)
|
2012-04-09 08:43:08 +08:00
|
|
|
|
|
|
|
def test_regression_17877(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Extra WHERE clauses get correctly ANDed, even when they
|
2012-04-09 08:43:08 +08:00
|
|
|
contain OR operations.
|
|
|
|
"""
|
|
|
|
# Test Case 1: should appear in queryset.
|
|
|
|
t = TestObject(first='a', second='a', third='a')
|
|
|
|
t.save()
|
|
|
|
# Test Case 2: should appear in queryset.
|
|
|
|
t = TestObject(first='b', second='a', third='a')
|
|
|
|
t.save()
|
|
|
|
# Test Case 3: should not appear in queryset, bug case.
|
|
|
|
t = TestObject(first='a', second='a', third='b')
|
|
|
|
t.save()
|
|
|
|
# Test Case 4: should not appear in queryset.
|
|
|
|
t = TestObject(first='b', second='a', third='b')
|
|
|
|
t.save()
|
|
|
|
# Test Case 5: should not appear in queryset.
|
|
|
|
t = TestObject(first='b', second='b', third='a')
|
|
|
|
t.save()
|
|
|
|
# Test Case 6: should not appear in queryset, bug case.
|
|
|
|
t = TestObject(first='a', second='b', third='b')
|
|
|
|
t.save()
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
TestObject.objects.extra(
|
|
|
|
where=["first = 'a' OR second = 'a'", "third = 'a'"],
|
|
|
|
),
|
2012-12-13 19:33:11 +08:00
|
|
|
['<TestObject: TestObject: a,a,a>', '<TestObject: TestObject: b,a,a>'],
|
|
|
|
ordered=False
|
2012-04-09 08:43:08 +08:00
|
|
|
)
|
2013-10-11 05:12:10 +08:00
|
|
|
|
|
|
|
def test_extra_values_distinct_ordering(self):
|
|
|
|
t1 = TestObject.objects.create(first='a', second='a', third='a')
|
|
|
|
t2 = TestObject.objects.create(first='a', second='b', third='b')
|
|
|
|
qs = TestObject.objects.extra(
|
|
|
|
select={'second_extra': 'second'}
|
|
|
|
).values_list('id', flat=True).distinct()
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs.order_by('second_extra'), [t1.pk, t2.pk])
|
|
|
|
self.assertSequenceEqual(qs.order_by('-second_extra'), [t2.pk, t1.pk])
|
2013-10-11 05:12:10 +08:00
|
|
|
# Note: the extra ordering must appear in select clause, so we get two
|
|
|
|
# non-distinct results here (this is on purpose, see #7070).
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs.order_by('-second_extra').values_list('first', flat=True), ['a', 'a'])
|