2010-10-19 12:07:22 +08:00
|
|
|
import datetime
|
|
|
|
import pickle
|
2019-04-05 12:37:58 +08:00
|
|
|
import sys
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2015-01-28 20:35:27 +08:00
|
|
|
from operator import attrgetter
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2016-05-26 03:00:20 +08:00
|
|
|
from django.core.exceptions import EmptyResultSet, FieldError
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, connection
|
2016-03-28 23:19:25 +08:00
|
|
|
from django.db.models import Count, F, Q
|
2014-11-17 16:26:10 +08:00
|
|
|
from django.db.models.sql.constants import LOUTER
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.models.sql.where import NothingNode, WhereNode
|
2018-11-27 03:05:02 +08:00
|
|
|
from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
|
2014-08-17 22:21:48 +08:00
|
|
|
from django.test.utils import CaptureQueriesContext
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2013-06-10 23:22:30 +08:00
|
|
|
from .models import (
|
2016-03-28 23:19:25 +08:00
|
|
|
FK1, Annotation, Article, Author, BaseA, Book, CategoryItem,
|
2015-01-28 20:35:27 +08:00
|
|
|
CategoryRelationship, Celebrity, Channel, Chapter, Child, ChildObjectA,
|
2018-01-21 03:48:01 +08:00
|
|
|
Classroom, CommonMixedCaseForeignKeys, Company, Cover, CustomPk,
|
2019-06-24 23:48:10 +08:00
|
|
|
CustomPkTag, DateTimePK, Detail, DumbCategory, Eaten, Employment,
|
|
|
|
ExtraInfo, Fan, Food, Identifier, Individual, Item, Job,
|
|
|
|
JobResponsibilities, Join, LeafA, LeafB, LoopX, LoopZ, ManagedModel,
|
|
|
|
Member, MixedCaseDbColumnCategoryItem, MixedCaseFieldCategoryItem, ModelA,
|
|
|
|
ModelB, ModelC, ModelD, MyObject, NamedCategory, Node, Note, NullableName,
|
|
|
|
Number, ObjectA, ObjectB, ObjectC, OneToOneCategory, Order, OrderItem,
|
|
|
|
Page, Paragraph, Person, Plaything, PointerA, Program, ProxyCategory,
|
|
|
|
ProxyObjectA, ProxyObjectB, Ranking, Related, RelatedIndividual,
|
|
|
|
RelatedObject, Report, ReportComment, ReservedName, Responsibility, School,
|
|
|
|
SharedConnection, SimpleCategory, SingleObject, SpecialCategory, Staff,
|
|
|
|
StaffUser, Student, Tag, Task, Teacher, Ticket21203Child,
|
|
|
|
Ticket21203Parent, Ticket23605A, Ticket23605B, Ticket23605C, TvChef, Valid,
|
|
|
|
X,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2016-09-10 17:36:27 +08:00
|
|
|
class Queries1Tests(TestCase):
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2010-10-19 12:07:22 +08:00
|
|
|
generic = NamedCategory.objects.create(name="Generic")
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.t1 = Tag.objects.create(name='t1', category=generic)
|
|
|
|
cls.t2 = Tag.objects.create(name='t2', parent=cls.t1, category=generic)
|
|
|
|
cls.t3 = Tag.objects.create(name='t3', parent=cls.t1)
|
|
|
|
t4 = Tag.objects.create(name='t4', parent=cls.t3)
|
|
|
|
cls.t5 = Tag.objects.create(name='t5', parent=cls.t3)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.n1 = Note.objects.create(note='n1', misc='foo', id=1)
|
2019-03-30 03:04:26 +08:00
|
|
|
cls.n2 = Note.objects.create(note='n2', misc='bar', id=2)
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.n3 = Note.objects.create(note='n3', misc='foo', id=3)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2014-10-19 05:30:58 +08:00
|
|
|
ann1 = Annotation.objects.create(name='a1', tag=cls.t1)
|
|
|
|
ann1.notes.add(cls.n1)
|
2010-10-19 12:07:22 +08:00
|
|
|
ann2 = Annotation.objects.create(name='a2', tag=t4)
|
2019-03-30 03:04:26 +08:00
|
|
|
ann2.notes.add(cls.n2, cls.n3)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
# Create these out of order so that sorting by 'id' will be different to sorting
|
|
|
|
# by 'info'. Helps detect some problems later.
|
2019-03-30 03:04:26 +08:00
|
|
|
cls.e2 = ExtraInfo.objects.create(info='e2', note=cls.n2, value=41)
|
2014-10-19 05:30:58 +08:00
|
|
|
e1 = ExtraInfo.objects.create(info='e1', note=cls.n1, value=42)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.a1 = Author.objects.create(name='a1', num=1001, extra=e1)
|
|
|
|
cls.a2 = Author.objects.create(name='a2', num=2002, extra=e1)
|
|
|
|
a3 = Author.objects.create(name='a3', num=3003, extra=cls.e2)
|
|
|
|
cls.a4 = Author.objects.create(name='a4', num=4004, extra=cls.e2)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.time1 = datetime.datetime(2007, 12, 19, 22, 25, 0)
|
|
|
|
cls.time2 = datetime.datetime(2007, 12, 19, 21, 0, 0)
|
2010-10-19 12:07:22 +08:00
|
|
|
time3 = datetime.datetime(2007, 12, 20, 22, 25, 0)
|
|
|
|
time4 = datetime.datetime(2007, 12, 20, 21, 0, 0)
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.i1 = Item.objects.create(name='one', created=cls.time1, modified=cls.time1, creator=cls.a1, note=cls.n3)
|
2015-10-09 05:17:10 +08:00
|
|
|
cls.i1.tags.set([cls.t1, cls.t2])
|
2019-03-30 03:04:26 +08:00
|
|
|
cls.i2 = Item.objects.create(name='two', created=cls.time2, creator=cls.a2, note=cls.n2)
|
2015-10-09 05:17:10 +08:00
|
|
|
cls.i2.tags.set([cls.t1, cls.t3])
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.i3 = Item.objects.create(name='three', created=time3, creator=cls.a2, note=cls.n3)
|
|
|
|
i4 = Item.objects.create(name='four', created=time4, creator=cls.a4, note=cls.n3)
|
2015-10-09 05:17:10 +08:00
|
|
|
i4.tags.set([t4])
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.r1 = Report.objects.create(name='r1', creator=cls.a1)
|
2010-10-19 12:07:22 +08:00
|
|
|
Report.objects.create(name='r2', creator=a3)
|
|
|
|
Report.objects.create(name='r3')
|
|
|
|
|
|
|
|
# Ordering by 'rank' gives us rank2, rank1, rank3. Ordering by the Meta.ordering
|
|
|
|
# will be rank3, rank2, rank1.
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.rank1 = Ranking.objects.create(rank=2, author=cls.a2)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
Cover.objects.create(title="first", item=i4)
|
2014-10-19 05:30:58 +08:00
|
|
|
Cover.objects.create(title="second", item=cls.i2)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2013-08-13 19:11:52 +08:00
|
|
|
def test_subquery_condition(self):
|
|
|
|
qs1 = Tag.objects.filter(pk__lte=0)
|
|
|
|
qs2 = Tag.objects.filter(parent__in=qs1)
|
|
|
|
qs3 = Tag.objects.filter(parent__in=qs2)
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual(qs3.query.subq_aliases, {'T', 'U', 'V'})
|
2013-08-13 20:30:02 +08:00
|
|
|
self.assertIn('v0', str(qs3.query).lower())
|
2013-08-13 19:11:52 +08:00
|
|
|
qs4 = qs3.filter(parent__in=qs1)
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual(qs4.query.subq_aliases, {'T', 'U', 'V'})
|
2013-08-13 19:11:52 +08:00
|
|
|
# It is possible to reuse U for the second subquery, no need to use W.
|
2013-08-13 20:30:02 +08:00
|
|
|
self.assertNotIn('w0', str(qs4.query).lower())
|
2018-07-09 23:13:40 +08:00
|
|
|
# So, 'U0."id"' is referenced in SELECT and WHERE twice.
|
|
|
|
self.assertEqual(str(qs4.query).lower().count('u0.'), 4)
|
2013-08-13 19:11:52 +08:00
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
def test_ticket1050(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(tags__isnull=True),
|
|
|
|
['<Item: three>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(tags__id__isnull=True),
|
|
|
|
['<Item: three>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket1801(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.filter(item=self.i2),
|
|
|
|
['<Author: a2>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.filter(item=self.i3),
|
|
|
|
['<Author: a2>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.filter(item=self.i2) & Author.objects.filter(item=self.i3),
|
|
|
|
['<Author: a2>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket2306(self):
|
|
|
|
# Checking that no join types are "left outer" joins.
|
|
|
|
query = Item.objects.filter(tags=self.t2).query
|
2014-11-17 16:26:10 +08:00
|
|
|
self.assertNotIn(LOUTER, [x.join_type for x in query.alias_map.values()])
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(Q(tags=self.t1)).order_by('name'),
|
|
|
|
['<Item: one>', '<Item: two>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(Q(tags=self.t1)).filter(Q(tags=self.t2)),
|
|
|
|
['<Item: one>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Item.objects.filter(Q(tags=self.t1)).filter(Q(creator__name='fred') | Q(tags=self.t2)),
|
2010-10-19 12:07:22 +08:00
|
|
|
['<Item: one>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# Each filter call is processed "at once" against a single table, so this is
|
|
|
|
# different from the previous example as it tries to find tags that are two
|
|
|
|
# things at once (rather than two tags).
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(Q(tags=self.t1) & Q(tags=self.t2)),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Item.objects.filter(Q(tags=self.t1), Q(creator__name='fred') | Q(tags=self.t2)),
|
2010-10-19 12:07:22 +08:00
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
|
|
|
qs = Author.objects.filter(ranking__rank=2, ranking__id=self.rank1.id)
|
|
|
|
self.assertQuerysetEqual(list(qs), ['<Author: a2>'])
|
|
|
|
self.assertEqual(2, qs.query.count_active_tables(), 2)
|
|
|
|
qs = Author.objects.filter(ranking__rank=2).filter(ranking__id=self.rank1.id)
|
|
|
|
self.assertEqual(qs.query.count_active_tables(), 3)
|
|
|
|
|
|
|
|
def test_ticket4464(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(tags=self.t1).filter(tags=self.t2),
|
|
|
|
['<Item: one>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(tags__in=[self.t1, self.t2]).distinct().order_by('name'),
|
|
|
|
['<Item: one>', '<Item: two>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(tags__in=[self.t1, self.t2]).filter(tags=self.t3),
|
|
|
|
['<Item: two>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# Make sure .distinct() works with slicing (this was broken in Oracle).
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(tags__in=[self.t1, self.t2]).order_by('name')[:3],
|
|
|
|
['<Item: one>', '<Item: one>', '<Item: two>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(tags__in=[self.t1, self.t2]).distinct().order_by('name')[:3],
|
|
|
|
['<Item: one>', '<Item: two>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_tickets_2080_3592(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.filter(item__name='one') | Author.objects.filter(name='a3'),
|
|
|
|
['<Author: a1>', '<Author: a3>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.filter(Q(item__name='one') | Q(name='a3')),
|
|
|
|
['<Author: a1>', '<Author: a3>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.filter(Q(name='a3') | Q(item__name='one')),
|
|
|
|
['<Author: a1>', '<Author: a3>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.filter(Q(item__name='three') | Q(report__name='r3')),
|
|
|
|
['<Author: a2>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket6074(self):
|
|
|
|
# Merging two empty result sets shouldn't leave a queryset with no constraints
|
|
|
|
# (which would match everything).
|
|
|
|
self.assertQuerysetEqual(Author.objects.filter(Q(id__in=[])), [])
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Author.objects.filter(Q(id__in=[]) | Q(id__in=[])),
|
2010-10-19 12:07:22 +08:00
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_tickets_1878_2939(self):
|
|
|
|
self.assertEqual(Item.objects.values('creator').distinct().count(), 3)
|
|
|
|
|
|
|
|
# Create something with a duplicate 'name' so that we can test multi-column
|
|
|
|
# cases (which require some tricky SQL transformations under the covers).
|
|
|
|
xx = Item(name='four', created=self.time1, creator=self.a2, note=self.n1)
|
|
|
|
xx.save()
|
|
|
|
self.assertEqual(
|
|
|
|
Item.objects.exclude(name='two').values('creator', 'name').distinct().count(),
|
|
|
|
4
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
(
|
|
|
|
Item.objects
|
|
|
|
.exclude(name='two')
|
|
|
|
.extra(select={'foo': '%s'}, select_params=(1,))
|
|
|
|
.values('creator', 'name', 'foo')
|
|
|
|
.distinct()
|
|
|
|
.count()
|
|
|
|
),
|
2010-10-19 12:07:22 +08:00
|
|
|
4
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
(
|
|
|
|
Item.objects
|
|
|
|
.exclude(name='two')
|
|
|
|
.extra(select={'foo': '%s'}, select_params=(1,))
|
|
|
|
.values('creator', 'name')
|
|
|
|
.distinct()
|
|
|
|
.count()
|
|
|
|
),
|
2010-10-19 12:07:22 +08:00
|
|
|
4
|
|
|
|
)
|
|
|
|
xx.delete()
|
|
|
|
|
|
|
|
def test_ticket7323(self):
|
|
|
|
self.assertEqual(Item.objects.values('creator', 'name').count(), 4)
|
|
|
|
|
|
|
|
def test_ticket2253(self):
|
|
|
|
q1 = Item.objects.order_by('name')
|
|
|
|
q2 = Item.objects.filter(id=self.i1.id)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q1,
|
|
|
|
['<Item: four>', '<Item: one>', '<Item: three>', '<Item: two>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(q2, ['<Item: one>'])
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
(q1 | q2).order_by('name'),
|
|
|
|
['<Item: four>', '<Item: one>', '<Item: three>', '<Item: two>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual((q1 & q2).order_by('name'), ['<Item: one>'])
|
|
|
|
|
|
|
|
q1 = Item.objects.filter(tags=self.t1)
|
|
|
|
q2 = Item.objects.filter(note=self.n3, tags=self.t2)
|
|
|
|
q3 = Item.objects.filter(creator=self.a4)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
((q1 & q2) | q3).order_by('name'),
|
|
|
|
['<Item: four>', '<Item: one>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_order_by_tables(self):
|
|
|
|
q1 = Item.objects.order_by('name')
|
|
|
|
q2 = Item.objects.filter(id=self.i1.id)
|
|
|
|
list(q2)
|
2012-08-25 21:33:07 +08:00
|
|
|
combined_query = (q1 & q2).order_by('name').query
|
|
|
|
self.assertEqual(len([
|
2017-06-28 14:23:37 +08:00
|
|
|
t for t in combined_query.alias_map if combined_query.alias_refcount[t]
|
2012-08-25 21:33:07 +08:00
|
|
|
]), 1)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2011-12-23 04:42:40 +08:00
|
|
|
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))
|
|
|
|
|
2015-07-10 02:37:17 +08:00
|
|
|
def test_get_clears_ordering(self):
|
|
|
|
"""
|
|
|
|
get() should clear ordering for optimization purposes.
|
|
|
|
"""
|
|
|
|
with CaptureQueriesContext(connection) as captured_queries:
|
|
|
|
Author.objects.order_by('name').get(pk=self.a1.pk)
|
|
|
|
self.assertNotIn('order by', captured_queries[0]['sql'].lower())
|
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
def test_tickets_4088_4306(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Report.objects.filter(creator=1001),
|
|
|
|
['<Report: r1>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Report.objects.filter(creator__num=1001),
|
|
|
|
['<Report: r1>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(Report.objects.filter(creator__id=1001), [])
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Report.objects.filter(creator__id=self.a1.id),
|
|
|
|
['<Report: r1>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Report.objects.filter(creator__name='a1'),
|
|
|
|
['<Report: r1>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket4510(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.filter(report__name='r1'),
|
|
|
|
['<Author: a1>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket7378(self):
|
|
|
|
self.assertQuerysetEqual(self.a1.report_set.all(), ['<Report: r1>'])
|
|
|
|
|
|
|
|
def test_tickets_5324_6704(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(tags__name='t4'),
|
|
|
|
['<Item: four>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.exclude(tags__name='t4').order_by('name').distinct(),
|
|
|
|
['<Item: one>', '<Item: three>', '<Item: two>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.exclude(tags__name='t4').order_by('name').distinct().reverse(),
|
|
|
|
['<Item: two>', '<Item: three>', '<Item: one>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.exclude(item__name='one').distinct().order_by('name'),
|
|
|
|
['<Author: a2>', '<Author: a3>', '<Author: a4>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# Excluding across a m2m relation when there is more than one related
|
|
|
|
# object associated was problematic.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.exclude(tags__name='t1').order_by('name'),
|
|
|
|
['<Item: four>', '<Item: three>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.exclude(tags__name='t1').exclude(tags__name='t4'),
|
|
|
|
['<Item: three>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# Excluding from a relation that cannot be NULL should not use outer joins.
|
|
|
|
query = Item.objects.exclude(creator__in=[self.a1, self.a2]).query
|
2014-11-17 16:26:10 +08:00
|
|
|
self.assertNotIn(LOUTER, [x.join_type for x in query.alias_map.values()])
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
# Similarly, when one of the joins cannot possibly, ever, involve NULL
|
|
|
|
# values (Author -> ExtraInfo, in the following), it should never be
|
|
|
|
# promoted to a left outer join. So the following query should only
|
|
|
|
# involve one "left outer" join (Author -> Item is 0-to-many).
|
2013-10-21 20:52:21 +08:00
|
|
|
qs = Author.objects.filter(id=self.a1.id).filter(Q(extra__note=self.n1) | Q(item__note=self.n3))
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
len([
|
|
|
|
x for x in qs.query.alias_map.values()
|
|
|
|
if x.join_type == LOUTER and qs.query.alias_refcount[x.table_alias]
|
|
|
|
]),
|
2010-10-19 12:07:22 +08:00
|
|
|
1
|
|
|
|
)
|
|
|
|
|
|
|
|
# The previous changes shouldn't affect nullable foreign key joins.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.filter(parent__isnull=True).order_by('name'),
|
|
|
|
['<Tag: t1>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.exclude(parent__isnull=True).order_by('name'),
|
|
|
|
['<Tag: t2>', '<Tag: t3>', '<Tag: t4>', '<Tag: t5>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.exclude(Q(parent__name='t1') | Q(parent__isnull=True)).order_by('name'),
|
|
|
|
['<Tag: t4>', '<Tag: t5>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.exclude(Q(parent__isnull=True) | Q(parent__name='t1')).order_by('name'),
|
|
|
|
['<Tag: t4>', '<Tag: t5>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.exclude(Q(parent__parent__isnull=True)).order_by('name'),
|
|
|
|
['<Tag: t4>', '<Tag: t5>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.filter(~Q(parent__parent__isnull=True)).order_by('name'),
|
|
|
|
['<Tag: t4>', '<Tag: t5>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket2091(self):
|
|
|
|
t = Tag.objects.get(name='t4')
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(tags__in=[t]),
|
|
|
|
['<Item: four>']
|
|
|
|
)
|
|
|
|
|
2014-11-15 21:45:00 +08:00
|
|
|
def test_avoid_infinite_loop_on_too_many_subqueries(self):
|
|
|
|
x = Tag.objects.filter(pk=1)
|
2019-04-05 12:37:58 +08:00
|
|
|
local_recursion_limit = sys.getrecursionlimit() // 16
|
2014-11-15 21:45:00 +08:00
|
|
|
msg = 'Maximum recursion depth exceeded: too many subqueries.'
|
2019-04-05 12:40:54 +08:00
|
|
|
with self.assertRaisesMessage(RecursionError, msg):
|
2019-04-05 12:37:58 +08:00
|
|
|
for i in range(local_recursion_limit + 2):
|
2014-11-15 21:45:00 +08:00
|
|
|
x = Tag.objects.filter(pk__in=x)
|
|
|
|
|
|
|
|
def test_reasonable_number_of_subq_aliases(self):
|
|
|
|
x = Tag.objects.filter(pk=1)
|
2014-12-31 22:19:04 +08:00
|
|
|
for _ in range(20):
|
2014-11-15 21:45:00 +08:00
|
|
|
x = Tag.objects.filter(pk__in=x)
|
|
|
|
self.assertEqual(
|
|
|
|
x.query.subq_aliases, {
|
|
|
|
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD',
|
|
|
|
'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
def test_heterogeneous_qs_combination(self):
|
|
|
|
# Combining querysets built on different models should behave in a well-defined
|
|
|
|
# fashion. We raise an error.
|
2016-01-04 16:50:08 +08:00
|
|
|
with self.assertRaisesMessage(AssertionError, 'Cannot combine queries on two different base models.'):
|
|
|
|
Author.objects.all() & Tag.objects.all()
|
|
|
|
with self.assertRaisesMessage(AssertionError, 'Cannot combine queries on two different base models.'):
|
|
|
|
Author.objects.all() | Tag.objects.all()
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_ticket3141(self):
|
|
|
|
self.assertEqual(Author.objects.extra(select={'foo': '1'}).count(), 4)
|
|
|
|
self.assertEqual(
|
|
|
|
Author.objects.extra(select={'foo': '%s'}, select_params=(1,)).count(),
|
|
|
|
4
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket2400(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.filter(item__isnull=True),
|
|
|
|
['<Author: a3>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.filter(item__isnull=True),
|
|
|
|
['<Tag: t5>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket2496(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.extra(tables=['queries_author']).select_related().order_by('name')[:1],
|
|
|
|
['<Item: four>']
|
|
|
|
)
|
|
|
|
|
2015-04-14 06:57:11 +08:00
|
|
|
def test_error_raised_on_filter_with_dictionary(self):
|
|
|
|
with self.assertRaisesMessage(FieldError, 'Cannot parse keyword query as dict'):
|
|
|
|
Note.objects.filter({'note': 'n1', 'misc': 'foo'})
|
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
def test_tickets_2076_7256(self):
|
|
|
|
# Ordering on related tables should be possible, even if the table is
|
|
|
|
# not otherwise involved.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.order_by('note__note', 'name'),
|
|
|
|
['<Item: two>', '<Item: four>', '<Item: one>', '<Item: three>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# Ordering on a related field should use the remote model's default
|
|
|
|
# ordering as a final step.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.order_by('extra', '-name'),
|
|
|
|
['<Author: a2>', '<Author: a1>', '<Author: a4>', '<Author: a3>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# Using remote model default ordering can span multiple models (in this
|
|
|
|
# case, Cover is ordered by Item's default, which uses Note's default).
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Cover.objects.all(),
|
|
|
|
['<Cover: first>', '<Cover: second>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# If the remote model does not have a default ordering, we order by its 'id'
|
|
|
|
# field.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.order_by('creator', 'name'),
|
|
|
|
['<Item: one>', '<Item: three>', '<Item: two>', '<Item: four>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# Ordering by a many-valued attribute (e.g. a many-to-many or reverse
|
|
|
|
# ForeignKey) is legal, but the results might not make sense. That
|
|
|
|
# isn't Django's problem. Garbage in, garbage out.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(tags__isnull=False).order_by('tags', 'id'),
|
|
|
|
['<Item: one>', '<Item: two>', '<Item: one>', '<Item: two>', '<Item: four>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# If we replace the default ordering, Django adjusts the required
|
|
|
|
# tables automatically. Item normally requires a join with Note to do
|
|
|
|
# the default ordering, but that isn't needed here.
|
|
|
|
qs = Item.objects.order_by('name')
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
qs,
|
|
|
|
['<Item: four>', '<Item: one>', '<Item: three>', '<Item: two>']
|
|
|
|
)
|
2017-06-28 14:23:37 +08:00
|
|
|
self.assertEqual(len(qs.query.alias_map), 1)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_tickets_2874_3002(self):
|
|
|
|
qs = Item.objects.select_related().order_by('note__note', 'name')
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
qs,
|
|
|
|
['<Item: two>', '<Item: four>', '<Item: one>', '<Item: three>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# This is also a good select_related() test because there are multiple
|
|
|
|
# Note entries in the SQL. The two Note items should be different.
|
2018-07-09 23:13:40 +08:00
|
|
|
self.assertEqual(repr(qs[0].note), '<Note: n2>')
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertEqual(repr(qs[0].creator.extra.note), '<Note: n1>')
|
|
|
|
|
|
|
|
def test_ticket3037(self):
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Item.objects.filter(Q(creator__name='a3', name='two') | Q(creator__name='a4', name='four')),
|
2010-10-19 12:07:22 +08:00
|
|
|
['<Item: four>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_tickets_5321_7070(self):
|
|
|
|
# Ordering columns must be included in the output columns. Note that
|
|
|
|
# this means results that might otherwise be distinct are not (if there
|
|
|
|
# are multiple values in the ordering cols), as in this example. This
|
|
|
|
# isn't a bug; it's a warning to be careful with the selection of
|
|
|
|
# ordering columns.
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(
|
2010-10-19 12:07:22 +08:00
|
|
|
Note.objects.values('misc').distinct().order_by('note', '-misc'),
|
2012-06-08 00:08:47 +08:00
|
|
|
[{'misc': 'foo'}, {'misc': 'bar'}, {'misc': 'foo'}]
|
2010-10-19 12:07:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket4358(self):
|
|
|
|
# If you don't pass any fields to values(), relation fields are
|
|
|
|
# returned as "foo_id" keys, not "foo". For consistency, you should be
|
|
|
|
# able to pass "foo_id" in the fields list and have it work, too. We
|
|
|
|
# actually allow both "foo" and "foo_id".
|
|
|
|
# The *_id version is returned by default.
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('note_id', ExtraInfo.objects.values()[0])
|
2010-10-19 12:07:22 +08:00
|
|
|
# You can also pass it in explicitly.
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(ExtraInfo.objects.values('note_id'), [{'note_id': 1}, {'note_id': 2}])
|
2010-10-19 12:07:22 +08:00
|
|
|
# ...or use the field name.
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(ExtraInfo.objects.values('note'), [{'note': 1}, {'note': 2}])
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_ticket6154(self):
|
|
|
|
# Multiple filter statements are joined using "AND" all the time.
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Author.objects.filter(id=self.a1.id).filter(Q(extra__note=self.n1) | Q(item__note=self.n3)),
|
2010-10-19 12:07:22 +08:00
|
|
|
['<Author: a1>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Author.objects.filter(Q(extra__note=self.n1) | Q(item__note=self.n3)).filter(id=self.a1.id),
|
2010-10-19 12:07:22 +08:00
|
|
|
['<Author: a1>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket6981(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.select_related('parent').order_by('name'),
|
|
|
|
['<Tag: t1>', '<Tag: t2>', '<Tag: t3>', '<Tag: t4>', '<Tag: t5>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket9926(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.select_related("parent", "category").order_by('name'),
|
|
|
|
['<Tag: t1>', '<Tag: t2>', '<Tag: t3>', '<Tag: t4>', '<Tag: t5>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.select_related('parent', "parent__category").order_by('name'),
|
|
|
|
['<Tag: t1>', '<Tag: t2>', '<Tag: t3>', '<Tag: t4>', '<Tag: t5>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_tickets_6180_6203(self):
|
|
|
|
# Dates with limits and/or counts
|
|
|
|
self.assertEqual(Item.objects.count(), 4)
|
2013-02-10 23:15:49 +08:00
|
|
|
self.assertEqual(Item.objects.datetimes('created', 'month').count(), 1)
|
|
|
|
self.assertEqual(Item.objects.datetimes('created', 'day').count(), 2)
|
|
|
|
self.assertEqual(len(Item.objects.datetimes('created', 'day')), 2)
|
|
|
|
self.assertEqual(Item.objects.datetimes('created', 'day')[0], datetime.datetime(2007, 12, 19, 0, 0))
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_tickets_7087_12242(self):
|
|
|
|
# Dates with extra select columns
|
|
|
|
self.assertQuerysetEqual(
|
2013-02-10 23:15:49 +08:00
|
|
|
Item.objects.datetimes('created', 'day').extra(select={'a': 1}),
|
2010-10-19 12:07:22 +08:00
|
|
|
['datetime.datetime(2007, 12, 19, 0, 0)', 'datetime.datetime(2007, 12, 20, 0, 0)']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
2013-02-10 23:15:49 +08:00
|
|
|
Item.objects.extra(select={'a': 1}).datetimes('created', 'day'),
|
2010-10-19 12:07:22 +08:00
|
|
|
['datetime.datetime(2007, 12, 19, 0, 0)', 'datetime.datetime(2007, 12, 20, 0, 0)']
|
|
|
|
)
|
|
|
|
|
2013-10-23 18:09:29 +08:00
|
|
|
name = "one"
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertQuerysetEqual(
|
2013-02-10 23:15:49 +08:00
|
|
|
Item.objects.datetimes('created', 'day').extra(where=['name=%s'], params=[name]),
|
2010-10-19 12:07:22 +08:00
|
|
|
['datetime.datetime(2007, 12, 19, 0, 0)']
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
2013-02-10 23:15:49 +08:00
|
|
|
Item.objects.extra(where=['name=%s'], params=[name]).datetimes('created', 'day'),
|
2010-10-19 12:07:22 +08:00
|
|
|
['datetime.datetime(2007, 12, 19, 0, 0)']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket7155(self):
|
|
|
|
# Nullable dates
|
|
|
|
self.assertQuerysetEqual(
|
2013-02-10 23:15:49 +08:00
|
|
|
Item.objects.datetimes('modified', 'day'),
|
2010-10-19 12:07:22 +08:00
|
|
|
['datetime.datetime(2007, 12, 19, 0, 0)']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket7098(self):
|
|
|
|
# Make sure semi-deprecated ordering by related models syntax still
|
|
|
|
# works.
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(
|
2010-10-19 12:07:22 +08:00
|
|
|
Item.objects.values('note__note').order_by('queries_note.note', 'id'),
|
2012-06-08 00:08:47 +08:00
|
|
|
[{'note__note': 'n2'}, {'note__note': 'n3'}, {'note__note': 'n3'}, {'note__note': 'n3'}]
|
2010-10-19 12:07:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket7096(self):
|
|
|
|
# Make sure exclude() with multiple conditions continues to work.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.filter(parent=self.t1, name='t3').order_by('name'),
|
|
|
|
['<Tag: t3>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.exclude(parent=self.t1, name='t3').order_by('name'),
|
|
|
|
['<Tag: t1>', '<Tag: t2>', '<Tag: t4>', '<Tag: t5>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.exclude(tags__name='t1', name='one').order_by('name').distinct(),
|
|
|
|
['<Item: four>', '<Item: three>', '<Item: two>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(name__in=['three', 'four']).exclude(tags__name='t1').order_by('name'),
|
|
|
|
['<Item: four>', '<Item: three>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# More twisted cases, involving nested negations.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.exclude(~Q(tags__name='t1', name='one')),
|
|
|
|
['<Item: one>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(~Q(tags__name='t1', name='one'), name='two'),
|
|
|
|
['<Item: two>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.exclude(~Q(tags__name='t1', name='one'), name='two'),
|
|
|
|
['<Item: four>', '<Item: one>', '<Item: three>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_tickets_7204_7506(self):
|
|
|
|
# Make sure querysets with related fields can be pickled. If this
|
|
|
|
# doesn't crash, it's a Good Thing.
|
|
|
|
pickle.dumps(Item.objects.all())
|
|
|
|
|
|
|
|
def test_ticket7813(self):
|
|
|
|
# We should also be able to pickle things that use select_related().
|
|
|
|
# The only tricky thing here is to ensure that we do the related
|
|
|
|
# selections properly after unpickling.
|
|
|
|
qs = Item.objects.select_related()
|
|
|
|
query = qs.query.get_compiler(qs.db).as_sql()[0]
|
|
|
|
query2 = pickle.loads(pickle.dumps(qs.query))
|
|
|
|
self.assertEqual(
|
|
|
|
query2.get_compiler(qs.db).as_sql()[0],
|
|
|
|
query
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_deferred_load_qs_pickling(self):
|
|
|
|
# Check pickling of deferred-loading querysets
|
|
|
|
qs = Item.objects.defer('name', 'creator')
|
|
|
|
q2 = pickle.loads(pickle.dumps(qs))
|
|
|
|
self.assertEqual(list(qs), list(q2))
|
|
|
|
q3 = pickle.loads(pickle.dumps(qs, pickle.HIGHEST_PROTOCOL))
|
|
|
|
self.assertEqual(list(qs), list(q3))
|
|
|
|
|
|
|
|
def test_ticket7277(self):
|
|
|
|
self.assertQuerysetEqual(
|
2015-09-12 07:33:12 +08:00
|
|
|
self.n1.annotation_set.filter(
|
|
|
|
Q(tag=self.t5) | Q(tag__children=self.t5) | Q(tag__children__children=self.t5)
|
|
|
|
),
|
2010-10-19 12:07:22 +08:00
|
|
|
['<Annotation: a1>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_tickets_7448_7707(self):
|
|
|
|
# Complex objects should be converted to strings before being used in
|
|
|
|
# lookups.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(created__in=[self.time1, self.time2]),
|
|
|
|
['<Item: one>', '<Item: two>']
|
|
|
|
)
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
def test_ticket7235(self):
|
|
|
|
# An EmptyQuerySet should not raise exceptions if it is filtered.
|
2012-10-24 05:04:37 +08:00
|
|
|
Eaten.objects.create(meal='m')
|
|
|
|
q = Eaten.objects.none()
|
|
|
|
with self.assertNumQueries(0):
|
|
|
|
self.assertQuerysetEqual(q.all(), [])
|
|
|
|
self.assertQuerysetEqual(q.filter(meal='m'), [])
|
|
|
|
self.assertQuerysetEqual(q.exclude(meal='m'), [])
|
|
|
|
self.assertQuerysetEqual(q.complex_filter({'pk': 1}), [])
|
|
|
|
self.assertQuerysetEqual(q.select_related('food'), [])
|
|
|
|
self.assertQuerysetEqual(q.annotate(Count('food')), [])
|
|
|
|
self.assertQuerysetEqual(q.order_by('meal', 'food'), [])
|
|
|
|
self.assertQuerysetEqual(q.distinct(), [])
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q.extra(select={'foo': "1"}),
|
|
|
|
[]
|
|
|
|
)
|
2014-05-17 20:59:57 +08:00
|
|
|
self.assertQuerysetEqual(q.reverse(), [])
|
2012-10-24 05:04:37 +08:00
|
|
|
q.query.low_mark = 1
|
2016-01-04 16:50:08 +08:00
|
|
|
with self.assertRaisesMessage(AssertionError, 'Cannot change a query once a slice has been taken'):
|
|
|
|
q.extra(select={'foo': "1"})
|
2012-10-24 05:04:37 +08:00
|
|
|
self.assertQuerysetEqual(q.defer('meal'), [])
|
|
|
|
self.assertQuerysetEqual(q.only('meal'), [])
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_ticket7791(self):
|
|
|
|
# There were "issues" when ordering and distinct-ing on fields related
|
|
|
|
# via ForeignKeys.
|
|
|
|
self.assertEqual(
|
|
|
|
len(Note.objects.order_by('extrainfo__info').distinct()),
|
|
|
|
3
|
|
|
|
)
|
|
|
|
|
2015-09-11 23:14:35 +08:00
|
|
|
# Pickling of QuerySets using datetimes() should work.
|
2013-02-10 23:15:49 +08:00
|
|
|
qs = Item.objects.datetimes('created', 'month')
|
2013-10-19 20:31:38 +08:00
|
|
|
pickle.loads(pickle.dumps(qs))
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_ticket9997(self):
|
|
|
|
# If a ValuesList or Values queryset is passed as an inner query, we
|
|
|
|
# make sure it's only requesting a single value and use that as the
|
|
|
|
# thing to select.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.filter(name__in=Tag.objects.filter(parent=self.t1).values('name')),
|
|
|
|
['<Tag: t2>', '<Tag: t3>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# Multi-valued values() and values_list() querysets should raise errors.
|
2016-01-04 16:50:08 +08:00
|
|
|
with self.assertRaisesMessage(TypeError, 'Cannot use multi-field values as a filter value.'):
|
|
|
|
Tag.objects.filter(name__in=Tag.objects.filter(parent=self.t1).values('name', 'id'))
|
|
|
|
with self.assertRaisesMessage(TypeError, 'Cannot use multi-field values as a filter value.'):
|
|
|
|
Tag.objects.filter(name__in=Tag.objects.filter(parent=self.t1).values_list('name', 'id'))
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_ticket9985(self):
|
|
|
|
# qs.values_list(...).values(...) combinations should work.
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(
|
2010-10-19 12:07:22 +08:00
|
|
|
Note.objects.values_list("note", flat=True).values("id").order_by("id"),
|
|
|
|
[{'id': 1}, {'id': 2}, {'id': 3}]
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Annotation.objects.filter(notes__in=Note.objects.filter(note="n1").values_list('note').values('id')),
|
|
|
|
['<Annotation: a1>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket10205(self):
|
|
|
|
# When bailing out early because of an empty "__in" filter, we need
|
|
|
|
# to set things up correctly internally so that subqueries can continue properly.
|
|
|
|
self.assertEqual(Tag.objects.filter(name__in=()).update(name="foo"), 0)
|
|
|
|
|
|
|
|
def test_ticket10432(self):
|
|
|
|
# Testing an empty "__in" filter with a generator as the value.
|
|
|
|
def f():
|
|
|
|
return iter([])
|
|
|
|
n_obj = Note.objects.all()[0]
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
def g():
|
2017-02-24 09:06:01 +08:00
|
|
|
yield n_obj.pk
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertQuerysetEqual(Note.objects.filter(pk__in=f()), [])
|
|
|
|
self.assertEqual(list(Note.objects.filter(pk__in=g())), [n_obj])
|
|
|
|
|
|
|
|
def test_ticket10742(self):
|
|
|
|
# Queries used in an __in clause don't execute subqueries
|
|
|
|
|
|
|
|
subq = Author.objects.filter(num__lt=3000)
|
|
|
|
qs = Author.objects.filter(pk__in=subq)
|
|
|
|
self.assertQuerysetEqual(qs, ['<Author: a1>', '<Author: a2>'])
|
|
|
|
|
|
|
|
# The subquery result cache should not be populated
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIsNone(subq._result_cache)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
subq = Author.objects.filter(num__lt=3000)
|
|
|
|
qs = Author.objects.exclude(pk__in=subq)
|
|
|
|
self.assertQuerysetEqual(qs, ['<Author: a3>', '<Author: a4>'])
|
|
|
|
|
|
|
|
# The subquery result cache should not be populated
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIsNone(subq._result_cache)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
subq = Author.objects.filter(num__lt=3000)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.filter(Q(pk__in=subq) & Q(name='a1')),
|
|
|
|
['<Author: a1>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# The subquery result cache should not be populated
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIsNone(subq._result_cache)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_ticket7076(self):
|
|
|
|
# Excluding shouldn't eliminate NULL entries.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.exclude(modified=self.time1).order_by('name'),
|
|
|
|
['<Item: four>', '<Item: three>', '<Item: two>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.exclude(parent__name=self.t1.name),
|
|
|
|
['<Tag: t1>', '<Tag: t4>', '<Tag: t5>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket7181(self):
|
2013-07-28 09:45:25 +08:00
|
|
|
# Ordering by related tables should accommodate nullable fields (this
|
2010-10-19 12:07:22 +08:00
|
|
|
# test is a little tricky, since NULL ordering is database dependent.
|
|
|
|
# Instead, we just count the number of results).
|
|
|
|
self.assertEqual(len(Tag.objects.order_by('parent__name')), 5)
|
|
|
|
|
|
|
|
# Empty querysets can be merged with others.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Note.objects.none() | Note.objects.all(),
|
|
|
|
['<Note: n1>', '<Note: n2>', '<Note: n3>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Note.objects.all() | Note.objects.none(),
|
|
|
|
['<Note: n1>', '<Note: n2>', '<Note: n3>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(Note.objects.none() & Note.objects.all(), [])
|
|
|
|
self.assertQuerysetEqual(Note.objects.all() & Note.objects.none(), [])
|
|
|
|
|
|
|
|
def test_ticket9411(self):
|
|
|
|
# Make sure bump_prefix() (an internal Query method) doesn't (re-)break. It's
|
|
|
|
# sufficient that this query runs without error.
|
|
|
|
qs = Tag.objects.values_list('id', flat=True).order_by('id')
|
2013-08-13 19:11:52 +08:00
|
|
|
qs.query.bump_prefix(qs.query)
|
2010-10-19 12:07:22 +08:00
|
|
|
first = qs[0]
|
2013-11-03 05:34:05 +08:00
|
|
|
self.assertEqual(list(qs), list(range(first, first + 5)))
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_ticket8439(self):
|
|
|
|
# Complex combinations of conjunctions, disjunctions and nullable
|
|
|
|
# relations.
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Author.objects.filter(Q(item__note__extrainfo=self.e2) | Q(report=self.r1, name='xyz')),
|
2010-10-19 12:07:22 +08:00
|
|
|
['<Author: a2>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Author.objects.filter(Q(report=self.r1, name='xyz') | Q(item__note__extrainfo=self.e2)),
|
2010-10-19 12:07:22 +08:00
|
|
|
['<Author: a2>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Annotation.objects.filter(Q(tag__parent=self.t1) | Q(notes__note='n1', name='a1')),
|
2010-10-19 12:07:22 +08:00
|
|
|
['<Annotation: a1>']
|
|
|
|
)
|
|
|
|
xx = ExtraInfo.objects.create(info='xx', note=self.n3)
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Note.objects.filter(Q(extrainfo__author=self.a1) | Q(extrainfo=xx)),
|
2010-10-19 12:07:22 +08:00
|
|
|
['<Note: n1>', '<Note: n3>']
|
|
|
|
)
|
2013-10-21 20:52:21 +08:00
|
|
|
q = Note.objects.filter(Q(extrainfo__author=self.a1) | Q(extrainfo=xx)).query
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertEqual(
|
2014-11-17 16:26:10 +08:00
|
|
|
len([x for x in q.alias_map.values() if x.join_type == LOUTER and q.alias_refcount[x.table_alias]]),
|
2010-10-19 12:07:22 +08:00
|
|
|
1
|
|
|
|
)
|
|
|
|
|
2012-01-03 17:06:19 +08:00
|
|
|
def test_ticket17429(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Meta.ordering=None works the same as Meta.ordering=[]
|
2012-01-03 17:06:19 +08:00
|
|
|
"""
|
|
|
|
original_ordering = Tag._meta.ordering
|
|
|
|
Tag._meta.ordering = None
|
2012-12-13 19:33:11 +08:00
|
|
|
try:
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.all(),
|
|
|
|
['<Tag: t1>', '<Tag: t2>', '<Tag: t3>', '<Tag: t4>', '<Tag: t5>'],
|
|
|
|
ordered=False
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
Tag._meta.ordering = original_ordering
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2012-05-25 19:00:42 +08:00
|
|
|
def test_exclude(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.exclude(tags__name='t4'),
|
|
|
|
[repr(i) for i in Item.objects.filter(~Q(tags__name='t4'))])
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Item.objects.exclude(Q(tags__name='t4') | Q(tags__name='t3')),
|
|
|
|
[repr(i) for i in Item.objects.filter(~(Q(tags__name='t4') | Q(tags__name='t3')))])
|
2012-05-25 19:00:42 +08:00
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Item.objects.exclude(Q(tags__name='t4') | ~Q(tags__name='t3')),
|
|
|
|
[repr(i) for i in Item.objects.filter(~(Q(tags__name='t4') | ~Q(tags__name='t3')))])
|
2012-05-25 19:00:42 +08:00
|
|
|
|
|
|
|
def test_nested_exclude(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.exclude(~Q(tags__name='t4')),
|
|
|
|
[repr(i) for i in Item.objects.filter(~~Q(tags__name='t4'))])
|
|
|
|
|
|
|
|
def test_double_exclude(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(Q(tags__name='t4')),
|
|
|
|
[repr(i) for i in Item.objects.filter(~~Q(tags__name='t4'))])
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(Q(tags__name='t4')),
|
|
|
|
[repr(i) for i in Item.objects.filter(~Q(~Q(tags__name='t4')))])
|
|
|
|
|
|
|
|
def test_exclude_in(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.exclude(Q(tags__name__in=['t4', 't3'])),
|
|
|
|
[repr(i) for i in Item.objects.filter(~Q(tags__name__in=['t4', 't3']))])
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(Q(tags__name__in=['t4', 't3'])),
|
|
|
|
[repr(i) for i in Item.objects.filter(~~Q(tags__name__in=['t4', 't3']))])
|
|
|
|
|
2012-08-25 21:33:07 +08:00
|
|
|
def test_ticket_10790_1(self):
|
|
|
|
# Querying direct fields with isnull should trim the left outer join.
|
|
|
|
# It also should not create INNER JOIN.
|
|
|
|
q = Tag.objects.filter(parent__isnull=True)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(q, ['<Tag: t1>'])
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertNotIn('JOIN', str(q.query))
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q = Tag.objects.filter(parent__isnull=False)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Tag: t2>', '<Tag: t3>', '<Tag: t4>', '<Tag: t5>'],
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertNotIn('JOIN', str(q.query))
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q = Tag.objects.exclude(parent__isnull=True)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Tag: t2>', '<Tag: t3>', '<Tag: t4>', '<Tag: t5>'],
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertNotIn('JOIN', str(q.query))
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q = Tag.objects.exclude(parent__isnull=False)
|
|
|
|
self.assertQuerysetEqual(q, ['<Tag: t1>'])
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertNotIn('JOIN', str(q.query))
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q = Tag.objects.exclude(parent__parent__isnull=False)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Tag: t1>', '<Tag: t2>', '<Tag: t3>'],
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 1)
|
|
|
|
self.assertNotIn('INNER JOIN', str(q.query))
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
def test_ticket_10790_2(self):
|
|
|
|
# Querying across several tables should strip only the last outer join,
|
2014-03-02 22:25:53 +08:00
|
|
|
# while preserving the preceding inner joins.
|
2012-08-25 21:33:07 +08:00
|
|
|
q = Tag.objects.filter(parent__parent__isnull=False)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Tag: t4>', '<Tag: t5>'],
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
self.assertEqual(str(q.query).count('INNER JOIN'), 1)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
# Querying without isnull should not convert anything to left outer join.
|
|
|
|
q = Tag.objects.filter(parent__parent=self.t1)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Tag: t4>', '<Tag: t5>'],
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
self.assertEqual(str(q.query).count('INNER JOIN'), 1)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
def test_ticket_10790_3(self):
|
|
|
|
# Querying via indirect fields should populate the left outer join
|
|
|
|
q = NamedCategory.objects.filter(tag__isnull=True)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 1)
|
2012-08-25 21:33:07 +08:00
|
|
|
# join to dumbcategory ptr_id
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('INNER JOIN'), 1)
|
2012-08-25 21:33:07 +08:00
|
|
|
self.assertQuerysetEqual(q, [])
|
|
|
|
|
|
|
|
# Querying across several tables should strip only the last join, while
|
|
|
|
# preserving the preceding left outer joins.
|
|
|
|
q = NamedCategory.objects.filter(tag__parent__isnull=True)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('INNER JOIN'), 1)
|
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 1)
|
2013-09-07 06:13:51 +08:00
|
|
|
self.assertQuerysetEqual(q, ['<NamedCategory: Generic>'])
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
def test_ticket_10790_4(self):
|
|
|
|
# Querying across m2m field should not strip the m2m table from join.
|
|
|
|
q = Author.objects.filter(item__tags__isnull=True)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Author: a2>', '<Author: a3>'],
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 2)
|
|
|
|
self.assertNotIn('INNER JOIN', str(q.query))
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q = Author.objects.filter(item__tags__parent__isnull=True)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Author: a1>', '<Author: a2>', '<Author: a2>', '<Author: a3>'],
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 3)
|
|
|
|
self.assertNotIn('INNER JOIN', str(q.query))
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
def test_ticket_10790_5(self):
|
|
|
|
# Querying with isnull=False across m2m field should not create outer joins
|
|
|
|
q = Author.objects.filter(item__tags__isnull=False)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Author: a1>', '<Author: a1>', '<Author: a2>', '<Author: a2>', '<Author: a4>']
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
self.assertEqual(str(q.query).count('INNER JOIN'), 2)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q = Author.objects.filter(item__tags__parent__isnull=False)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Author: a1>', '<Author: a2>', '<Author: a4>']
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
self.assertEqual(str(q.query).count('INNER JOIN'), 3)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q = Author.objects.filter(item__tags__parent__parent__isnull=False)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Author: a4>']
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
self.assertEqual(str(q.query).count('INNER JOIN'), 4)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
def test_ticket_10790_6(self):
|
|
|
|
# Querying with isnull=True across m2m field should not create inner joins
|
|
|
|
# and strip last outer join
|
|
|
|
q = Author.objects.filter(item__tags__parent__parent__isnull=True)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Author: a1>', '<Author: a1>', '<Author: a2>', '<Author: a2>',
|
|
|
|
'<Author: a2>', '<Author: a3>']
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 4)
|
|
|
|
self.assertEqual(str(q.query).count('INNER JOIN'), 0)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q = Author.objects.filter(item__tags__parent__isnull=True)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Author: a1>', '<Author: a2>', '<Author: a2>', '<Author: a3>']
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 3)
|
|
|
|
self.assertEqual(str(q.query).count('INNER JOIN'), 0)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
def test_ticket_10790_7(self):
|
|
|
|
# Reverse querying with isnull should not strip the join
|
|
|
|
q = Author.objects.filter(item__isnull=True)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Author: a3>']
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 1)
|
|
|
|
self.assertEqual(str(q.query).count('INNER JOIN'), 0)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q = Author.objects.filter(item__isnull=False)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Author: a1>', '<Author: a2>', '<Author: a2>', '<Author: a4>']
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
self.assertEqual(str(q.query).count('INNER JOIN'), 1)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
def test_ticket_10790_8(self):
|
|
|
|
# Querying with combined q-objects should also strip the left outer join
|
|
|
|
q = Tag.objects.filter(Q(parent__isnull=True) | Q(parent=self.t1))
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q,
|
|
|
|
['<Tag: t1>', '<Tag: t2>', '<Tag: t3>']
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
self.assertEqual(str(q.query).count('INNER JOIN'), 0)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
def test_ticket_10790_combine(self):
|
|
|
|
# Combining queries should not re-populate the left outer join
|
|
|
|
q1 = Tag.objects.filter(parent__isnull=True)
|
|
|
|
q2 = Tag.objects.filter(parent__isnull=False)
|
|
|
|
|
|
|
|
q3 = q1 | q2
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q3,
|
|
|
|
['<Tag: t1>', '<Tag: t2>', '<Tag: t3>', '<Tag: t4>', '<Tag: t5>'],
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q3.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
self.assertEqual(str(q3.query).count('INNER JOIN'), 0)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q3 = q1 & q2
|
|
|
|
self.assertQuerysetEqual(q3, [])
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q3.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
self.assertEqual(str(q3.query).count('INNER JOIN'), 0)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q2 = Tag.objects.filter(parent=self.t1)
|
|
|
|
q3 = q1 | q2
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q3,
|
|
|
|
['<Tag: t1>', '<Tag: t2>', '<Tag: t3>']
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q3.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
self.assertEqual(str(q3.query).count('INNER JOIN'), 0)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q3 = q2 | q1
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q3,
|
|
|
|
['<Tag: t1>', '<Tag: t2>', '<Tag: t3>']
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q3.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
self.assertEqual(str(q3.query).count('INNER JOIN'), 0)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q1 = Tag.objects.filter(parent__isnull=True)
|
|
|
|
q2 = Tag.objects.filter(parent__parent__isnull=True)
|
|
|
|
|
|
|
|
q3 = q1 | q2
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q3,
|
|
|
|
['<Tag: t1>', '<Tag: t2>', '<Tag: t3>']
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q3.query).count('LEFT OUTER JOIN'), 1)
|
|
|
|
self.assertEqual(str(q3.query).count('INNER JOIN'), 0)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
|
|
|
q3 = q2 | q1
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
q3,
|
|
|
|
['<Tag: t1>', '<Tag: t2>', '<Tag: t3>']
|
|
|
|
)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertEqual(str(q3.query).count('LEFT OUTER JOIN'), 1)
|
|
|
|
self.assertEqual(str(q3.query).count('INNER JOIN'), 0)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
2013-02-21 03:09:27 +08:00
|
|
|
def test_ticket19672(self):
|
|
|
|
self.assertQuerysetEqual(
|
2016-04-04 08:37:32 +08:00
|
|
|
Report.objects.filter(Q(creator__isnull=False) & ~Q(creator__extra__value=41)),
|
2013-02-21 03:09:27 +08:00
|
|
|
['<Report: r1>']
|
|
|
|
)
|
|
|
|
|
2013-05-29 20:48:02 +08:00
|
|
|
def test_ticket_20250(self):
|
|
|
|
# A negated Q along with an annotated queryset failed in Django 1.4
|
|
|
|
qs = Author.objects.annotate(Count('item'))
|
2018-09-14 00:29:48 +08:00
|
|
|
qs = qs.filter(~Q(extra__value=0)).order_by('name')
|
2013-05-29 20:48:02 +08:00
|
|
|
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('SELECT', str(qs.query))
|
2013-05-29 20:48:02 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
qs,
|
|
|
|
['<Author: a1>', '<Author: a2>', '<Author: a3>', '<Author: a4>']
|
|
|
|
)
|
|
|
|
|
2015-03-22 21:04:53 +08:00
|
|
|
def test_lookup_constraint_fielderror(self):
|
|
|
|
msg = (
|
|
|
|
"Cannot resolve keyword 'unknown_field' into field. Choices are: "
|
|
|
|
"annotation, category, category_id, children, id, item, "
|
2015-08-21 00:46:14 +08:00
|
|
|
"managedmodel, name, note, parent, parent_id"
|
2015-03-22 21:04:53 +08:00
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
|
|
|
Tag.objects.filter(unknown_field__name='generic')
|
|
|
|
|
2018-01-21 03:48:01 +08:00
|
|
|
def test_common_mixed_case_foreign_keys(self):
|
|
|
|
"""
|
|
|
|
Valid query should be generated when fields fetched from joined tables
|
|
|
|
include FKs whose names only differ by case.
|
|
|
|
"""
|
|
|
|
c1 = SimpleCategory.objects.create(name='c1')
|
|
|
|
c2 = SimpleCategory.objects.create(name='c2')
|
|
|
|
c3 = SimpleCategory.objects.create(name='c3')
|
|
|
|
category = CategoryItem.objects.create(category=c1)
|
|
|
|
mixed_case_field_category = MixedCaseFieldCategoryItem.objects.create(CaTeGoRy=c2)
|
|
|
|
mixed_case_db_column_category = MixedCaseDbColumnCategoryItem.objects.create(category=c3)
|
|
|
|
CommonMixedCaseForeignKeys.objects.create(
|
|
|
|
category=category,
|
|
|
|
mixed_case_field_category=mixed_case_field_category,
|
|
|
|
mixed_case_db_column_category=mixed_case_db_column_category,
|
|
|
|
)
|
|
|
|
qs = CommonMixedCaseForeignKeys.objects.values(
|
|
|
|
'category',
|
|
|
|
'mixed_case_field_category',
|
|
|
|
'mixed_case_db_column_category',
|
|
|
|
'category__category',
|
|
|
|
'mixed_case_field_category__CaTeGoRy',
|
|
|
|
'mixed_case_db_column_category__category',
|
|
|
|
)
|
|
|
|
self.assertTrue(qs.first())
|
|
|
|
|
2019-03-30 03:04:26 +08:00
|
|
|
def test_excluded_intermediary_m2m_table_joined(self):
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
Note.objects.filter(~Q(tag__annotation__name=F('note'))),
|
|
|
|
[self.n1, self.n2, self.n3],
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
Note.objects.filter(tag__annotation__name='a1').filter(~Q(tag__annotation__name=F('note'))),
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
|
2012-08-25 21:33:07 +08:00
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
class Queries2Tests(TestCase):
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2010-10-19 12:07:22 +08:00
|
|
|
Number.objects.create(num=4)
|
|
|
|
Number.objects.create(num=8)
|
|
|
|
Number.objects.create(num=12)
|
|
|
|
|
|
|
|
def test_ticket4289(self):
|
|
|
|
# A slight variation on the restricting the filtering choices by the
|
|
|
|
# lookup constraints.
|
|
|
|
self.assertQuerysetEqual(Number.objects.filter(num__lt=4), [])
|
|
|
|
self.assertQuerysetEqual(Number.objects.filter(num__gt=8, num__lt=12), [])
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(num__gt=8, num__lt=13),
|
|
|
|
['<Number: 12>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(Q(num__lt=4) | Q(num__gt=8, num__lt=12)),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(Q(num__gt=8, num__lt=12) | Q(num__lt=4)),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(Q(num__gt=8) & Q(num__lt=12) | Q(num__lt=4)),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(Q(num__gt=7) & Q(num__lt=12) | Q(num__lt=4)),
|
|
|
|
['<Number: 8>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket12239(self):
|
2016-04-24 01:13:31 +08:00
|
|
|
# Custom lookups are registered to round float values correctly on gte
|
|
|
|
# and lt IntegerField queries.
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(num__gt=11.9),
|
|
|
|
['<Number: 12>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(Number.objects.filter(num__gt=12), [])
|
|
|
|
self.assertQuerysetEqual(Number.objects.filter(num__gt=12.0), [])
|
|
|
|
self.assertQuerysetEqual(Number.objects.filter(num__gt=12.1), [])
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(num__lt=12),
|
2012-12-13 19:33:11 +08:00
|
|
|
['<Number: 4>', '<Number: 8>'],
|
|
|
|
ordered=False
|
2010-10-19 12:07:22 +08:00
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(num__lt=12.0),
|
2012-12-13 19:33:11 +08:00
|
|
|
['<Number: 4>', '<Number: 8>'],
|
|
|
|
ordered=False
|
2010-10-19 12:07:22 +08:00
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(num__lt=12.1),
|
2012-12-13 19:33:11 +08:00
|
|
|
['<Number: 4>', '<Number: 8>', '<Number: 12>'],
|
|
|
|
ordered=False
|
2010-10-19 12:07:22 +08:00
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(num__gte=11.9),
|
|
|
|
['<Number: 12>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(num__gte=12),
|
|
|
|
['<Number: 12>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(num__gte=12.0),
|
|
|
|
['<Number: 12>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(Number.objects.filter(num__gte=12.1), [])
|
|
|
|
self.assertQuerysetEqual(Number.objects.filter(num__gte=12.9), [])
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(num__lte=11.9),
|
2012-12-13 19:33:11 +08:00
|
|
|
['<Number: 4>', '<Number: 8>'],
|
|
|
|
ordered=False
|
2010-10-19 12:07:22 +08:00
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(num__lte=12),
|
2012-12-13 19:33:11 +08:00
|
|
|
['<Number: 4>', '<Number: 8>', '<Number: 12>'],
|
|
|
|
ordered=False
|
2010-10-19 12:07:22 +08:00
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(num__lte=12.0),
|
2012-12-13 19:33:11 +08:00
|
|
|
['<Number: 4>', '<Number: 8>', '<Number: 12>'],
|
|
|
|
ordered=False
|
2010-10-19 12:07:22 +08:00
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(num__lte=12.1),
|
2012-12-13 19:33:11 +08:00
|
|
|
['<Number: 4>', '<Number: 8>', '<Number: 12>'],
|
|
|
|
ordered=False
|
2010-10-19 12:07:22 +08:00
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Number.objects.filter(num__lte=12.9),
|
2012-12-13 19:33:11 +08:00
|
|
|
['<Number: 4>', '<Number: 8>', '<Number: 12>'],
|
|
|
|
ordered=False
|
2010-10-19 12:07:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket7759(self):
|
|
|
|
# Count should work with a partially read result set.
|
|
|
|
count = Number.objects.count()
|
|
|
|
qs = Number.objects.all()
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
def run():
|
|
|
|
for obj in qs:
|
|
|
|
return qs.count() == count
|
|
|
|
self.assertTrue(run())
|
|
|
|
|
|
|
|
|
2016-09-10 17:36:27 +08:00
|
|
|
class Queries3Tests(TestCase):
|
2010-10-19 12:07:22 +08:00
|
|
|
def test_ticket7107(self):
|
|
|
|
# This shouldn't create an infinite loop.
|
|
|
|
self.assertQuerysetEqual(Valid.objects.all(), [])
|
|
|
|
|
|
|
|
def test_ticket8683(self):
|
2015-09-11 23:14:35 +08:00
|
|
|
# An error should be raised when QuerySet.datetimes() is passed the
|
|
|
|
# wrong type of field.
|
2016-06-19 11:38:24 +08:00
|
|
|
with self.assertRaisesMessage(AssertionError, "'name' isn't a DateField, TimeField, or DateTimeField."):
|
2016-01-04 16:50:08 +08:00
|
|
|
Item.objects.datetimes('name', 'month')
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2014-02-16 05:44:14 +08:00
|
|
|
def test_ticket22023(self):
|
2016-02-04 19:10:50 +08:00
|
|
|
with self.assertRaisesMessage(TypeError, "Cannot call only() after .values() or .values_list()"):
|
2014-02-16 05:44:14 +08:00
|
|
|
Valid.objects.values().only()
|
|
|
|
|
2016-02-04 19:10:50 +08:00
|
|
|
with self.assertRaisesMessage(TypeError, "Cannot call defer() after .values() or .values_list()"):
|
2014-02-16 05:44:14 +08:00
|
|
|
Valid.objects.values().defer()
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2016-09-10 17:36:27 +08:00
|
|
|
class Queries4Tests(TestCase):
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2010-10-19 12:07:22 +08:00
|
|
|
generic = NamedCategory.objects.create(name="Generic")
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.t1 = Tag.objects.create(name='t1', category=generic)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2015-08-21 00:46:14 +08:00
|
|
|
n1 = Note.objects.create(note='n1', misc='foo')
|
|
|
|
n2 = Note.objects.create(note='n2', misc='bar')
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
e1 = ExtraInfo.objects.create(info='e1', note=n1)
|
|
|
|
e2 = ExtraInfo.objects.create(info='e2', note=n2)
|
|
|
|
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.a1 = Author.objects.create(name='a1', num=1001, extra=e1)
|
|
|
|
cls.a3 = Author.objects.create(name='a3', num=3003, extra=e2)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.r1 = Report.objects.create(name='r1', creator=cls.a1)
|
|
|
|
cls.r2 = Report.objects.create(name='r2', creator=cls.a3)
|
|
|
|
cls.r3 = Report.objects.create(name='r3')
|
2011-08-22 15:40:12 +08:00
|
|
|
|
2014-10-19 05:30:58 +08:00
|
|
|
Item.objects.create(name='i1', created=datetime.datetime.now(), note=n1, creator=cls.a1)
|
|
|
|
Item.objects.create(name='i2', created=datetime.datetime.now(), note=n1, creator=cls.a3)
|
2011-08-22 15:40:12 +08:00
|
|
|
|
2015-08-21 00:46:14 +08:00
|
|
|
def test_ticket24525(self):
|
|
|
|
tag = Tag.objects.create()
|
|
|
|
anth100 = tag.note_set.create(note='ANTH', misc='100')
|
|
|
|
math101 = tag.note_set.create(note='MATH', misc='101')
|
|
|
|
s1 = tag.annotation_set.create(name='1')
|
|
|
|
s2 = tag.annotation_set.create(name='2')
|
2015-10-09 05:17:10 +08:00
|
|
|
s1.notes.set([math101, anth100])
|
|
|
|
s2.notes.set([math101])
|
2015-08-21 00:46:14 +08:00
|
|
|
result = math101.annotation_set.all() & tag.annotation_set.exclude(notes__in=[anth100])
|
|
|
|
self.assertEqual(list(result), [s2])
|
|
|
|
|
2013-09-07 06:13:51 +08:00
|
|
|
def test_ticket11811(self):
|
|
|
|
unsaved_category = NamedCategory(name="Other")
|
2016-01-18 16:45:45 +08:00
|
|
|
msg = 'Unsaved model instance <NamedCategory: Other> cannot be used in an ORM query.'
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2013-09-07 06:13:51 +08:00
|
|
|
Tag.objects.filter(pk=self.t1.pk).update(category=unsaved_category)
|
|
|
|
|
2011-08-22 15:40:12 +08:00
|
|
|
def test_ticket14876(self):
|
2012-08-25 21:33:07 +08:00
|
|
|
# Note: when combining the query we need to have information available
|
|
|
|
# about the join type of the trimmed "creator__isnull" join. If we
|
|
|
|
# don't have that information, then the join is created as INNER JOIN
|
|
|
|
# and results will be incorrect.
|
2011-08-22 15:40:12 +08:00
|
|
|
q1 = Report.objects.filter(Q(creator__isnull=True) | Q(creator__extra__info='e1'))
|
|
|
|
q2 = Report.objects.filter(Q(creator__isnull=True)) | Report.objects.filter(Q(creator__extra__info='e1'))
|
2012-12-11 23:25:47 +08:00
|
|
|
self.assertQuerysetEqual(q1, ["<Report: r1>", "<Report: r3>"], ordered=False)
|
2011-08-22 15:40:12 +08:00
|
|
|
self.assertEqual(str(q1.query), str(q2.query))
|
|
|
|
|
|
|
|
q1 = Report.objects.filter(Q(creator__extra__info='e1') | Q(creator__isnull=True))
|
|
|
|
q2 = Report.objects.filter(Q(creator__extra__info='e1')) | Report.objects.filter(Q(creator__isnull=True))
|
2012-12-11 23:25:47 +08:00
|
|
|
self.assertQuerysetEqual(q1, ["<Report: r1>", "<Report: r3>"], ordered=False)
|
2011-08-22 15:40:12 +08:00
|
|
|
self.assertEqual(str(q1.query), str(q2.query))
|
|
|
|
|
|
|
|
q1 = Item.objects.filter(Q(creator=self.a1) | Q(creator__report__name='r1')).order_by()
|
2015-09-12 07:33:12 +08:00
|
|
|
q2 = (
|
|
|
|
Item.objects
|
|
|
|
.filter(Q(creator=self.a1)).order_by() | Item.objects.filter(Q(creator__report__name='r1'))
|
|
|
|
.order_by()
|
|
|
|
)
|
2011-08-22 15:40:12 +08:00
|
|
|
self.assertQuerysetEqual(q1, ["<Item: i1>"])
|
|
|
|
self.assertEqual(str(q1.query), str(q2.query))
|
|
|
|
|
|
|
|
q1 = Item.objects.filter(Q(creator__report__name='e1') | Q(creator=self.a1)).order_by()
|
2015-09-12 07:33:12 +08:00
|
|
|
q2 = (
|
2016-04-04 08:37:32 +08:00
|
|
|
Item.objects.filter(Q(creator__report__name='e1')).order_by() |
|
|
|
|
Item.objects.filter(Q(creator=self.a1)).order_by()
|
2015-09-12 07:33:12 +08:00
|
|
|
)
|
2011-08-22 15:40:12 +08:00
|
|
|
self.assertQuerysetEqual(q1, ["<Item: i1>"])
|
|
|
|
self.assertEqual(str(q1.query), str(q2.query))
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2012-08-11 03:00:21 +08:00
|
|
|
def test_combine_join_reuse(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
# Joins having identical connections are correctly recreated in the
|
|
|
|
# rhs query, in case the query is ORed together (#18748).
|
2012-08-11 03:00:21 +08:00
|
|
|
Report.objects.create(name='r4', creator=self.a1)
|
|
|
|
q1 = Author.objects.filter(report__name='r5')
|
|
|
|
q2 = Author.objects.filter(report__name='r4').filter(report__name='r1')
|
2013-10-21 20:52:21 +08:00
|
|
|
combined = q1 | q2
|
2013-01-09 02:07:12 +08:00
|
|
|
self.assertEqual(str(combined.query).count('JOIN'), 2)
|
|
|
|
self.assertEqual(len(combined), 1)
|
|
|
|
self.assertEqual(combined[0].name, 'a1')
|
2012-08-11 03:00:21 +08:00
|
|
|
|
2017-03-02 22:36:25 +08:00
|
|
|
def test_join_reuse_order(self):
|
|
|
|
# Join aliases are reused in order. This shouldn't raise AssertionError
|
|
|
|
# because change_map contains a circular reference (#26522).
|
|
|
|
s1 = School.objects.create()
|
|
|
|
s2 = School.objects.create()
|
|
|
|
s3 = School.objects.create()
|
|
|
|
t1 = Teacher.objects.create()
|
|
|
|
otherteachers = Teacher.objects.exclude(pk=t1.pk).exclude(friends=t1)
|
|
|
|
qs1 = otherteachers.filter(schools=s1).filter(schools=s2)
|
|
|
|
qs2 = otherteachers.filter(schools=s1).filter(schools=s3)
|
|
|
|
self.assertQuerysetEqual(qs1 | qs2, [])
|
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
def test_ticket7095(self):
|
|
|
|
# Updates that are filtered on the model being updated are somewhat
|
2016-10-27 15:53:39 +08:00
|
|
|
# tricky in MySQL.
|
2010-10-19 12:07:22 +08:00
|
|
|
ManagedModel.objects.create(data='mm1', tag=self.t1, public=True)
|
|
|
|
self.assertEqual(ManagedModel.objects.update(data='mm'), 1)
|
|
|
|
|
|
|
|
# A values() or values_list() query across joined models must use outer
|
|
|
|
# joins appropriately.
|
2012-06-08 00:08:47 +08:00
|
|
|
# Note: In Oracle, we expect a null CharField to return '' instead of
|
2010-10-19 12:07:22 +08:00
|
|
|
# None.
|
|
|
|
if connection.features.interprets_empty_strings_as_nulls:
|
2012-06-08 00:08:47 +08:00
|
|
|
expected_null_charfield_repr = ''
|
2010-10-19 12:07:22 +08:00
|
|
|
else:
|
|
|
|
expected_null_charfield_repr = None
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(
|
2010-10-19 12:07:22 +08:00
|
|
|
Report.objects.values_list("creator__extra__info", flat=True).order_by("name"),
|
2012-06-08 00:08:47 +08:00
|
|
|
['e1', 'e2', expected_null_charfield_repr],
|
2010-10-19 12:07:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Similarly for select_related(), joins beyond an initial nullable join
|
|
|
|
# must use outer joins so that all results are included.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Report.objects.select_related("creator", "creator__extra").order_by("name"),
|
|
|
|
['<Report: r1>', '<Report: r2>', '<Report: r3>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# When there are multiple paths to a table from another table, we have
|
|
|
|
# to be careful not to accidentally reuse an inappropriate join when
|
|
|
|
# using select_related(). We used to return the parent's Detail record
|
|
|
|
# here by mistake.
|
|
|
|
|
|
|
|
d1 = Detail.objects.create(data="d1")
|
|
|
|
d2 = Detail.objects.create(data="d2")
|
|
|
|
m1 = Member.objects.create(name="m1", details=d1)
|
|
|
|
m2 = Member.objects.create(name="m2", details=d2)
|
|
|
|
Child.objects.create(person=m2, parent=m1)
|
|
|
|
obj = m1.children.select_related("person__details")[0]
|
2012-06-08 00:08:47 +08:00
|
|
|
self.assertEqual(obj.person.details.data, 'd2')
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_order_by_resetting(self):
|
|
|
|
# Calling order_by() with no parameters removes any existing ordering on the
|
|
|
|
# model. But it should still be possible to add new ordering after that.
|
|
|
|
qs = Author.objects.order_by().order_by('name')
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('ORDER BY', qs.query.get_compiler(qs.db).as_sql()[0])
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2014-05-05 20:22:01 +08:00
|
|
|
def test_order_by_reverse_fk(self):
|
|
|
|
# It is possible to order by reverse of foreign key, although that can lead
|
|
|
|
# to duplicate results.
|
|
|
|
c1 = SimpleCategory.objects.create(name="category1")
|
|
|
|
c2 = SimpleCategory.objects.create(name="category2")
|
|
|
|
CategoryItem.objects.create(category=c1)
|
|
|
|
CategoryItem.objects.create(category=c2)
|
|
|
|
CategoryItem.objects.create(category=c1)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(SimpleCategory.objects.order_by('categoryitem', 'pk'), [c1, c2, c1])
|
2014-05-05 20:22:01 +08:00
|
|
|
|
2019-06-24 23:48:10 +08:00
|
|
|
def test_filter_reverse_non_integer_pk(self):
|
|
|
|
date_obj = DateTimePK.objects.create()
|
|
|
|
extra_obj = ExtraInfo.objects.create(info='extra', date=date_obj)
|
|
|
|
self.assertEqual(
|
|
|
|
DateTimePK.objects.filter(extrainfo=extra_obj).get(),
|
|
|
|
date_obj,
|
|
|
|
)
|
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
def test_ticket10181(self):
|
|
|
|
# Avoid raising an EmptyResultSet if an inner query is probably
|
|
|
|
# empty (and hence, not executed).
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.filter(id__in=Tag.objects.filter(id__in=[])),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
2011-08-23 11:38:42 +08:00
|
|
|
def test_ticket15316_filter_false(self):
|
|
|
|
c1 = SimpleCategory.objects.create(name="category1")
|
2016-04-08 10:04:45 +08:00
|
|
|
c2 = SpecialCategory.objects.create(name="named category1", special_name="special1")
|
|
|
|
c3 = SpecialCategory.objects.create(name="named category2", special_name="special2")
|
2011-08-23 11:38:42 +08:00
|
|
|
|
2013-10-19 20:31:38 +08:00
|
|
|
CategoryItem.objects.create(category=c1)
|
2011-08-23 11:38:42 +08:00
|
|
|
ci2 = CategoryItem.objects.create(category=c2)
|
|
|
|
ci3 = CategoryItem.objects.create(category=c3)
|
|
|
|
|
|
|
|
qs = CategoryItem.objects.filter(category__specialcategory__isnull=False)
|
|
|
|
self.assertEqual(qs.count(), 2)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [ci2, ci3])
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
def test_ticket15316_exclude_false(self):
|
|
|
|
c1 = SimpleCategory.objects.create(name="category1")
|
2016-04-08 10:04:45 +08:00
|
|
|
c2 = SpecialCategory.objects.create(name="named category1", special_name="special1")
|
|
|
|
c3 = SpecialCategory.objects.create(name="named category2", special_name="special2")
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
ci1 = CategoryItem.objects.create(category=c1)
|
2013-10-19 20:31:38 +08:00
|
|
|
CategoryItem.objects.create(category=c2)
|
|
|
|
CategoryItem.objects.create(category=c3)
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
qs = CategoryItem.objects.exclude(category__specialcategory__isnull=False)
|
|
|
|
self.assertEqual(qs.count(), 1)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [ci1])
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
def test_ticket15316_filter_true(self):
|
|
|
|
c1 = SimpleCategory.objects.create(name="category1")
|
2016-04-08 10:04:45 +08:00
|
|
|
c2 = SpecialCategory.objects.create(name="named category1", special_name="special1")
|
|
|
|
c3 = SpecialCategory.objects.create(name="named category2", special_name="special2")
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
ci1 = CategoryItem.objects.create(category=c1)
|
2013-10-19 20:31:38 +08:00
|
|
|
CategoryItem.objects.create(category=c2)
|
|
|
|
CategoryItem.objects.create(category=c3)
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
qs = CategoryItem.objects.filter(category__specialcategory__isnull=True)
|
|
|
|
self.assertEqual(qs.count(), 1)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [ci1])
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
def test_ticket15316_exclude_true(self):
|
|
|
|
c1 = SimpleCategory.objects.create(name="category1")
|
2016-04-08 10:04:45 +08:00
|
|
|
c2 = SpecialCategory.objects.create(name="named category1", special_name="special1")
|
|
|
|
c3 = SpecialCategory.objects.create(name="named category2", special_name="special2")
|
2011-08-23 11:38:42 +08:00
|
|
|
|
2013-10-19 20:31:38 +08:00
|
|
|
CategoryItem.objects.create(category=c1)
|
2011-08-23 11:38:42 +08:00
|
|
|
ci2 = CategoryItem.objects.create(category=c2)
|
|
|
|
ci3 = CategoryItem.objects.create(category=c3)
|
|
|
|
|
|
|
|
qs = CategoryItem.objects.exclude(category__specialcategory__isnull=True)
|
|
|
|
self.assertEqual(qs.count(), 2)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [ci2, ci3])
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
def test_ticket15316_one2one_filter_false(self):
|
2013-10-22 21:31:43 +08:00
|
|
|
c = SimpleCategory.objects.create(name="cat")
|
2011-08-23 11:38:42 +08:00
|
|
|
c0 = SimpleCategory.objects.create(name="cat0")
|
|
|
|
c1 = SimpleCategory.objects.create(name="category1")
|
2011-09-10 07:02:33 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
OneToOneCategory.objects.create(category=c1, new_name="new1")
|
|
|
|
OneToOneCategory.objects.create(category=c0, new_name="new2")
|
2011-08-23 11:38:42 +08:00
|
|
|
|
2013-10-19 20:31:38 +08:00
|
|
|
CategoryItem.objects.create(category=c)
|
2011-08-23 11:38:42 +08:00
|
|
|
ci2 = CategoryItem.objects.create(category=c0)
|
|
|
|
ci3 = CategoryItem.objects.create(category=c1)
|
|
|
|
|
2016-09-14 01:57:10 +08:00
|
|
|
qs = CategoryItem.objects.filter(category__onetoonecategory__isnull=False).order_by('pk')
|
2011-08-23 11:38:42 +08:00
|
|
|
self.assertEqual(qs.count(), 2)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [ci2, ci3])
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
def test_ticket15316_one2one_exclude_false(self):
|
2013-10-22 21:31:43 +08:00
|
|
|
c = SimpleCategory.objects.create(name="cat")
|
2011-08-23 11:38:42 +08:00
|
|
|
c0 = SimpleCategory.objects.create(name="cat0")
|
|
|
|
c1 = SimpleCategory.objects.create(name="category1")
|
2011-09-10 07:02:33 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
OneToOneCategory.objects.create(category=c1, new_name="new1")
|
|
|
|
OneToOneCategory.objects.create(category=c0, new_name="new2")
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
ci1 = CategoryItem.objects.create(category=c)
|
2013-10-19 20:31:38 +08:00
|
|
|
CategoryItem.objects.create(category=c0)
|
|
|
|
CategoryItem.objects.create(category=c1)
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
qs = CategoryItem.objects.exclude(category__onetoonecategory__isnull=False)
|
|
|
|
self.assertEqual(qs.count(), 1)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [ci1])
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
def test_ticket15316_one2one_filter_true(self):
|
2013-10-22 21:31:43 +08:00
|
|
|
c = SimpleCategory.objects.create(name="cat")
|
2011-08-23 11:38:42 +08:00
|
|
|
c0 = SimpleCategory.objects.create(name="cat0")
|
|
|
|
c1 = SimpleCategory.objects.create(name="category1")
|
2011-09-10 07:02:33 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
OneToOneCategory.objects.create(category=c1, new_name="new1")
|
|
|
|
OneToOneCategory.objects.create(category=c0, new_name="new2")
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
ci1 = CategoryItem.objects.create(category=c)
|
2013-10-19 20:31:38 +08:00
|
|
|
CategoryItem.objects.create(category=c0)
|
|
|
|
CategoryItem.objects.create(category=c1)
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
qs = CategoryItem.objects.filter(category__onetoonecategory__isnull=True)
|
|
|
|
self.assertEqual(qs.count(), 1)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [ci1])
|
2011-08-23 11:38:42 +08:00
|
|
|
|
|
|
|
def test_ticket15316_one2one_exclude_true(self):
|
2013-10-22 21:31:43 +08:00
|
|
|
c = SimpleCategory.objects.create(name="cat")
|
2011-08-23 11:38:42 +08:00
|
|
|
c0 = SimpleCategory.objects.create(name="cat0")
|
|
|
|
c1 = SimpleCategory.objects.create(name="category1")
|
2011-09-10 07:02:33 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
OneToOneCategory.objects.create(category=c1, new_name="new1")
|
|
|
|
OneToOneCategory.objects.create(category=c0, new_name="new2")
|
2011-08-23 11:38:42 +08:00
|
|
|
|
2013-10-19 20:31:38 +08:00
|
|
|
CategoryItem.objects.create(category=c)
|
2011-08-23 11:38:42 +08:00
|
|
|
ci2 = CategoryItem.objects.create(category=c0)
|
|
|
|
ci3 = CategoryItem.objects.create(category=c1)
|
|
|
|
|
2016-09-14 01:57:10 +08:00
|
|
|
qs = CategoryItem.objects.exclude(category__onetoonecategory__isnull=True).order_by('pk')
|
2011-08-23 11:38:42 +08:00
|
|
|
self.assertEqual(qs.count(), 2)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [ci2, ci3])
|
2011-08-23 11:38:42 +08:00
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
class Queries5Tests(TestCase):
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2011-08-23 11:38:42 +08:00
|
|
|
# Ordering by 'rank' gives us rank2, rank1, rank3. Ordering by the
|
|
|
|
# Meta.ordering will be rank3, rank2, rank1.
|
2010-10-19 12:07:22 +08:00
|
|
|
n1 = Note.objects.create(note='n1', misc='foo', id=1)
|
|
|
|
n2 = Note.objects.create(note='n2', misc='bar', id=2)
|
|
|
|
e1 = ExtraInfo.objects.create(info='e1', note=n1)
|
|
|
|
e2 = ExtraInfo.objects.create(info='e2', note=n2)
|
|
|
|
a1 = Author.objects.create(name='a1', num=1001, extra=e1)
|
|
|
|
a2 = Author.objects.create(name='a2', num=2002, extra=e1)
|
|
|
|
a3 = Author.objects.create(name='a3', num=3003, extra=e2)
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.rank1 = Ranking.objects.create(rank=2, author=a2)
|
2010-10-19 12:07:22 +08:00
|
|
|
Ranking.objects.create(rank=1, author=a3)
|
|
|
|
Ranking.objects.create(rank=3, author=a1)
|
|
|
|
|
|
|
|
def test_ordering(self):
|
|
|
|
# Cross model ordering is possible in Meta, too.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Ranking.objects.all(),
|
|
|
|
['<Ranking: 3: a1>', '<Ranking: 2: a2>', '<Ranking: 1: a3>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Ranking.objects.all().order_by('rank'),
|
|
|
|
['<Ranking: 1: a3>', '<Ranking: 2: a2>', '<Ranking: 3: a1>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# Ordering of extra() pieces is possible, too and you can mix extra
|
|
|
|
# fields and model fields in the ordering.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Ranking.objects.extra(tables=['django_site'], order_by=['-django_site.id', 'rank']),
|
|
|
|
['<Ranking: 1: a3>', '<Ranking: 2: a2>', '<Ranking: 3: a1>']
|
|
|
|
)
|
|
|
|
|
2018-06-06 22:13:25 +08:00
|
|
|
sql = 'case when %s > 2 then 1 else 0 end' % connection.ops.quote_name('rank')
|
|
|
|
qs = Ranking.objects.extra(select={'good': sql})
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertEqual(
|
|
|
|
[o.good for o in qs.extra(order_by=('-good',))],
|
|
|
|
[True, False, False]
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
qs.extra(order_by=('-good', 'id')),
|
|
|
|
['<Ranking: 3: a1>', '<Ranking: 2: a2>', '<Ranking: 1: a3>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# Despite having some extra aliases in the query, we can still omit
|
|
|
|
# them in a values() query.
|
|
|
|
dicts = qs.values('id', 'rank').order_by('id')
|
|
|
|
self.assertEqual(
|
2012-08-14 20:24:43 +08:00
|
|
|
[d['rank'] for d in dicts],
|
|
|
|
[2, 1, 3]
|
2010-10-19 12:07:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket7256(self):
|
|
|
|
# An empty values() call includes all aliases, including those from an
|
|
|
|
# extra()
|
2018-06-06 22:13:25 +08:00
|
|
|
sql = 'case when %s > 2 then 1 else 0 end' % connection.ops.quote_name('rank')
|
|
|
|
qs = Ranking.objects.extra(select={'good': sql})
|
2010-10-19 12:07:22 +08:00
|
|
|
dicts = qs.values().order_by('id')
|
2013-10-11 19:25:14 +08:00
|
|
|
for d in dicts:
|
|
|
|
del d['id']
|
|
|
|
del d['author_id']
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertEqual(
|
|
|
|
[sorted(d.items()) for d in dicts],
|
|
|
|
[[('good', 0), ('rank', 2)], [('good', 0), ('rank', 1)], [('good', 1), ('rank', 3)]]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket7045(self):
|
|
|
|
# Extra tables used to crash SQL construction on the second use.
|
|
|
|
qs = Ranking.objects.extra(tables=['django_site'])
|
|
|
|
qs.query.get_compiler(qs.db).as_sql()
|
2010-11-26 22:24:08 +08:00
|
|
|
# test passes if this doesn't raise an exception.
|
|
|
|
qs.query.get_compiler(qs.db).as_sql()
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_ticket9848(self):
|
2010-11-26 22:24:08 +08:00
|
|
|
# Make sure that updates which only filter on sub-tables don't
|
|
|
|
# inadvertently update the wrong records (bug #9848).
|
2017-01-14 10:12:24 +08:00
|
|
|
author_start = Author.objects.get(name='a1')
|
|
|
|
ranking_start = Ranking.objects.get(author__name='a1')
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
# Make sure that the IDs from different tables don't happen to match.
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Ranking.objects.filter(author__name='a1'),
|
|
|
|
['<Ranking: 3: a1>']
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2017-01-14 10:12:24 +08:00
|
|
|
Ranking.objects.filter(author__name='a1').update(rank=4636),
|
2010-10-19 12:07:22 +08:00
|
|
|
1
|
|
|
|
)
|
2017-01-14 10:12:24 +08:00
|
|
|
|
|
|
|
r = Ranking.objects.get(author__name='a1')
|
|
|
|
self.assertEqual(r.id, ranking_start.id)
|
|
|
|
self.assertEqual(r.author.id, author_start.id)
|
|
|
|
self.assertEqual(r.rank, 4636)
|
2010-10-19 12:07:22 +08:00
|
|
|
r.rank = 3
|
|
|
|
r.save()
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Ranking.objects.all(),
|
|
|
|
['<Ranking: 3: a1>', '<Ranking: 2: a2>', '<Ranking: 1: a3>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket5261(self):
|
2012-05-26 10:55:33 +08:00
|
|
|
# Test different empty excludes.
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Note.objects.exclude(Q()),
|
|
|
|
['<Note: n1>', '<Note: n2>']
|
|
|
|
)
|
2012-05-26 10:55:33 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Note.objects.filter(~Q()),
|
|
|
|
['<Note: n1>', '<Note: n2>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Note.objects.filter(~Q() | ~Q()),
|
2012-05-26 10:55:33 +08:00
|
|
|
['<Note: n1>', '<Note: n2>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
Note.objects.exclude(~Q() & ~Q()),
|
2012-05-26 10:55:33 +08:00
|
|
|
['<Note: n1>', '<Note: n2>']
|
|
|
|
)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2014-09-10 15:23:58 +08:00
|
|
|
def test_extra_select_literal_percent_s(self):
|
|
|
|
# Allow %%s to escape select clauses
|
|
|
|
self.assertEqual(
|
|
|
|
Note.objects.extra(select={'foo': "'%%s'"})[0].foo,
|
|
|
|
'%s'
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
Note.objects.extra(select={'foo': "'%%s bar %%s'"})[0].foo,
|
|
|
|
'%s bar %s'
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
Note.objects.extra(select={'foo': "'bar %%s'"})[0].foo,
|
|
|
|
'bar %s'
|
|
|
|
)
|
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
class SelectRelatedTests(TestCase):
|
|
|
|
def test_tickets_3045_3288(self):
|
|
|
|
# Once upon a time, select_related() with circular relations would loop
|
|
|
|
# infinitely if you forgot to specify "depth". Now we set an arbitrary
|
|
|
|
# default upper bound.
|
|
|
|
self.assertQuerysetEqual(X.objects.all(), [])
|
|
|
|
self.assertQuerysetEqual(X.objects.select_related(), [])
|
|
|
|
|
|
|
|
|
|
|
|
class SubclassFKTests(TestCase):
|
|
|
|
def test_ticket7778(self):
|
|
|
|
# Model subclasses could not be deleted if a nullable foreign key
|
|
|
|
# relates to a model that relates back.
|
|
|
|
|
|
|
|
num_celebs = Celebrity.objects.count()
|
|
|
|
tvc = TvChef.objects.create(name="Huey")
|
|
|
|
self.assertEqual(Celebrity.objects.count(), num_celebs + 1)
|
|
|
|
Fan.objects.create(fan_of=tvc)
|
|
|
|
Fan.objects.create(fan_of=tvc)
|
|
|
|
tvc.delete()
|
|
|
|
|
|
|
|
# The parent object should have been deleted as well.
|
|
|
|
self.assertEqual(Celebrity.objects.count(), num_celebs)
|
|
|
|
|
|
|
|
|
|
|
|
class CustomPkTests(TestCase):
|
|
|
|
def test_ticket7371(self):
|
|
|
|
self.assertQuerysetEqual(Related.objects.order_by('custom'), [])
|
|
|
|
|
|
|
|
|
|
|
|
class NullableRelOrderingTests(TestCase):
|
|
|
|
def test_ticket10028(self):
|
|
|
|
# Ordering by model related to nullable relations(!) should use outer
|
|
|
|
# joins, so that all results are included.
|
2012-05-24 04:25:23 +08:00
|
|
|
Plaything.objects.create(name="p1")
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Plaything.objects.all(),
|
|
|
|
['<Plaything: p1>']
|
|
|
|
)
|
|
|
|
|
2012-05-24 04:25:23 +08:00
|
|
|
def test_join_already_in_query(self):
|
|
|
|
# Ordering by model related to nullable relations should not change
|
|
|
|
# the join type of already existing joins.
|
|
|
|
Plaything.objects.create(name="p1")
|
|
|
|
s = SingleObject.objects.create(name='s')
|
2012-08-25 21:33:07 +08:00
|
|
|
r = RelatedObject.objects.create(single=s, f=1)
|
2012-05-24 04:25:23 +08:00
|
|
|
Plaything.objects.create(name="p2", others=r)
|
|
|
|
qs = Plaything.objects.all().filter(others__isnull=False).order_by('pk')
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertNotIn('JOIN', str(qs.query))
|
2012-08-25 21:33:07 +08:00
|
|
|
qs = Plaything.objects.all().filter(others__f__isnull=False).order_by('pk')
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn('INNER', str(qs.query))
|
2012-05-24 04:25:23 +08:00
|
|
|
qs = qs.order_by('others__single__name')
|
|
|
|
# The ordering by others__single__pk will add one new join (to single)
|
|
|
|
# and that join must be LEFT join. The already existing join to related
|
2014-05-29 08:39:14 +08:00
|
|
|
# objects must be kept INNER. So, we have both an INNER and a LEFT join
|
2012-05-24 04:25:23 +08:00
|
|
|
# in the query.
|
2013-01-09 02:07:12 +08:00
|
|
|
self.assertEqual(str(qs.query).count('LEFT'), 1)
|
|
|
|
self.assertEqual(str(qs.query).count('INNER'), 1)
|
2012-05-24 04:25:23 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
qs,
|
|
|
|
['<Plaything: p2>']
|
|
|
|
)
|
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
class DisjunctiveFilterTests(TestCase):
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.n1 = Note.objects.create(note='n1', misc='foo', id=1)
|
|
|
|
ExtraInfo.objects.create(info='e1', note=cls.n1)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_ticket7872(self):
|
|
|
|
# Another variation on the disjunctive filtering theme.
|
|
|
|
|
|
|
|
# For the purposes of this regression test, it's important that there is no
|
2015-01-20 22:54:12 +08:00
|
|
|
# Join object related to the LeafA we create.
|
2010-10-19 12:07:22 +08:00
|
|
|
LeafA.objects.create(data='first')
|
|
|
|
self.assertQuerysetEqual(LeafA.objects.all(), ['<LeafA: first>'])
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
LeafA.objects.filter(Q(data='first') | Q(join__b__data='second')),
|
2010-10-19 12:07:22 +08:00
|
|
|
['<LeafA: first>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket8283(self):
|
|
|
|
# Checking that applying filters after a disjunction works correctly.
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
(ExtraInfo.objects.filter(note=self.n1) | ExtraInfo.objects.filter(info='e2')).filter(note=self.n1),
|
2010-10-19 12:07:22 +08:00
|
|
|
['<ExtraInfo: e1>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-21 20:52:21 +08:00
|
|
|
(ExtraInfo.objects.filter(info='e2') | ExtraInfo.objects.filter(note=self.n1)).filter(note=self.n1),
|
2010-10-19 12:07:22 +08:00
|
|
|
['<ExtraInfo: e1>']
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Queries6Tests(TestCase):
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2010-10-19 12:07:22 +08:00
|
|
|
generic = NamedCategory.objects.create(name="Generic")
|
2018-09-27 02:18:48 +08:00
|
|
|
cls.t1 = Tag.objects.create(name='t1', category=generic)
|
|
|
|
cls.t2 = Tag.objects.create(name='t2', parent=cls.t1, category=generic)
|
|
|
|
cls.t3 = Tag.objects.create(name='t3', parent=cls.t1)
|
|
|
|
cls.t4 = Tag.objects.create(name='t4', parent=cls.t3)
|
|
|
|
cls.t5 = Tag.objects.create(name='t5', parent=cls.t3)
|
2010-10-19 12:07:22 +08:00
|
|
|
n1 = Note.objects.create(note='n1', misc='foo', id=1)
|
2018-09-27 02:18:48 +08:00
|
|
|
ann1 = Annotation.objects.create(name='a1', tag=cls.t1)
|
2010-10-19 12:07:22 +08:00
|
|
|
ann1.notes.add(n1)
|
2018-09-27 02:18:48 +08:00
|
|
|
Annotation.objects.create(name='a2', tag=cls.t4)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_parallel_iterators(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
# Parallel iterators work.
|
2010-10-19 12:07:22 +08:00
|
|
|
qs = Tag.objects.all()
|
|
|
|
i1, i2 = iter(qs), iter(qs)
|
2012-05-11 02:14:04 +08:00
|
|
|
self.assertEqual(repr(next(i1)), '<Tag: t1>')
|
|
|
|
self.assertEqual(repr(next(i1)), '<Tag: t2>')
|
|
|
|
self.assertEqual(repr(next(i2)), '<Tag: t1>')
|
|
|
|
self.assertEqual(repr(next(i2)), '<Tag: t2>')
|
|
|
|
self.assertEqual(repr(next(i2)), '<Tag: t3>')
|
|
|
|
self.assertEqual(repr(next(i1)), '<Tag: t3>')
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
qs = X.objects.all()
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertFalse(qs)
|
|
|
|
self.assertFalse(qs)
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
def test_nested_queries_sql(self):
|
|
|
|
# Nested queries should not evaluate the inner query as part of constructing the
|
|
|
|
# SQL (so we should see a nested query here, indicated by two "SELECT" calls).
|
|
|
|
qs = Annotation.objects.filter(notes__in=Note.objects.filter(note="xyzzy"))
|
|
|
|
self.assertEqual(
|
|
|
|
qs.query.get_compiler(qs.db).as_sql()[0].count('SELECT'),
|
|
|
|
2
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_tickets_8921_9188(self):
|
|
|
|
# Incorrect SQL was being generated for certain types of exclude()
|
|
|
|
# queries that crossed multi-valued relations (#8921, #9188 and some
|
2015-12-03 07:55:50 +08:00
|
|
|
# preemptively discovered cases).
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
PointerA.objects.filter(connection__pointerb__id=1),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
PointerA.objects.exclude(connection__pointerb__id=1),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.exclude(children=None),
|
|
|
|
['<Tag: t1>', '<Tag: t3>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# This example is tricky because the parent could be NULL, so only checking
|
|
|
|
# parents with annotations omits some results (tag t1, in this case).
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Tag.objects.exclude(parent__annotation__name="a1"),
|
|
|
|
['<Tag: t1>', '<Tag: t4>', '<Tag: t5>']
|
|
|
|
)
|
|
|
|
|
|
|
|
# The annotation->tag link is single values and tag->children links is
|
2010-11-26 22:24:08 +08:00
|
|
|
# multi-valued. So we have to split the exclude filter in the middle
|
2011-12-18 01:38:08 +08:00
|
|
|
# and then optimize the inner query without losing results.
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Annotation.objects.exclude(tag__children__name="t2"),
|
|
|
|
['<Annotation: a2>']
|
|
|
|
)
|
|
|
|
|
2010-11-26 22:24:08 +08:00
|
|
|
# Nested queries are possible (although should be used with care, since
|
|
|
|
# they have performance problems on backends like MySQL.
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Annotation.objects.filter(notes__in=Note.objects.filter(note="n1")),
|
|
|
|
['<Annotation: a1>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ticket3739(self):
|
|
|
|
# The all() method on querysets returns a copy of the queryset.
|
|
|
|
q1 = Tag.objects.order_by('name')
|
2010-11-26 22:24:08 +08:00
|
|
|
self.assertIsNot(q1, q1.all())
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2013-11-06 01:26:59 +08:00
|
|
|
def test_ticket_11320(self):
|
|
|
|
qs = Tag.objects.exclude(category=None).exclude(category__name='foo')
|
|
|
|
self.assertEqual(str(qs.query).count(' INNER JOIN '), 1)
|
|
|
|
|
2018-02-08 22:59:25 +08:00
|
|
|
def test_distinct_ordered_sliced_subquery_aggregation(self):
|
|
|
|
self.assertEqual(Tag.objects.distinct().order_by('category__name')[:3].count(), 3)
|
|
|
|
|
2018-09-27 02:18:48 +08:00
|
|
|
def test_multiple_columns_with_the_same_name_slice(self):
|
|
|
|
self.assertEqual(
|
|
|
|
list(Tag.objects.order_by('name').values_list('name', 'category__name')[:2]),
|
|
|
|
[('t1', 'Generic'), ('t2', 'Generic')],
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
Tag.objects.order_by('name').select_related('category')[:2],
|
|
|
|
[self.t1, self.t2],
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
list(Tag.objects.order_by('-name').values_list('name', 'parent__name')[:2]),
|
|
|
|
[('t5', 't3'), ('t4', 't3')],
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
Tag.objects.order_by('-name').select_related('parent')[:2],
|
|
|
|
[self.t5, self.t4],
|
|
|
|
)
|
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2011-04-23 02:17:26 +08:00
|
|
|
class RawQueriesTests(TestCase):
|
|
|
|
def setUp(self):
|
2013-10-19 20:31:38 +08:00
|
|
|
Note.objects.create(note='n1', misc='foo', id=1)
|
2011-04-23 02:17:26 +08:00
|
|
|
|
|
|
|
def test_ticket14729(self):
|
|
|
|
# Test representation of raw query with one or few parameters passed as list
|
|
|
|
query = "SELECT * FROM queries_note WHERE note = %s"
|
|
|
|
params = ['n1']
|
|
|
|
qs = Note.objects.raw(query, params=params)
|
2014-08-17 22:21:48 +08:00
|
|
|
self.assertEqual(repr(qs), "<RawQuerySet: SELECT * FROM queries_note WHERE note = n1>")
|
2011-04-23 02:17:26 +08:00
|
|
|
|
|
|
|
query = "SELECT * FROM queries_note WHERE note = %s and misc = %s"
|
|
|
|
params = ['n1', 'foo']
|
|
|
|
qs = Note.objects.raw(query, params=params)
|
2014-08-17 22:21:48 +08:00
|
|
|
self.assertEqual(repr(qs), "<RawQuerySet: SELECT * FROM queries_note WHERE note = n1 and misc = foo>")
|
2011-04-23 02:17:26 +08:00
|
|
|
|
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class GeneratorExpressionTests(SimpleTestCase):
|
2010-10-19 12:07:22 +08:00
|
|
|
def test_ticket10432(self):
|
2019-02-09 22:18:48 +08:00
|
|
|
# Using an empty iterator as the rvalue for an "__in"
|
2010-10-19 12:07:22 +08:00
|
|
|
# lookup is legal.
|
2019-02-09 22:18:48 +08:00
|
|
|
self.assertCountEqual(Note.objects.filter(pk__in=iter(())), [])
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ComparisonTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.n1 = Note.objects.create(note='n1', misc='foo', id=1)
|
|
|
|
e1 = ExtraInfo.objects.create(info='e1', note=self.n1)
|
|
|
|
self.a2 = Author.objects.create(name='a2', num=2002, extra=e1)
|
|
|
|
|
|
|
|
def test_ticket8597(self):
|
|
|
|
# Regression tests for case-insensitive comparisons
|
2013-10-19 20:31:38 +08:00
|
|
|
Item.objects.create(name="a_b", created=datetime.datetime.now(), creator=self.a2, note=self.n1)
|
|
|
|
Item.objects.create(name="x%y", created=datetime.datetime.now(), creator=self.a2, note=self.n1)
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(name__iexact="A_b"),
|
|
|
|
['<Item: a_b>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(name__iexact="x%Y"),
|
|
|
|
['<Item: x%y>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(name__istartswith="A_b"),
|
|
|
|
['<Item: a_b>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Item.objects.filter(name__iendswith="A_b"),
|
|
|
|
['<Item: a_b>']
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ExistsSql(TestCase):
|
|
|
|
def test_exists(self):
|
2013-10-06 02:15:25 +08:00
|
|
|
with CaptureQueriesContext(connection) as captured_queries:
|
|
|
|
self.assertFalse(Tag.objects.exists())
|
2010-10-19 12:07:22 +08:00
|
|
|
# Ok - so the exist query worked - but did it include too many columns?
|
2013-10-06 02:15:25 +08:00
|
|
|
self.assertEqual(len(captured_queries), 1)
|
2015-07-10 02:44:16 +08:00
|
|
|
qstr = captured_queries[0]['sql']
|
2013-10-06 02:15:25 +08:00
|
|
|
id, name = connection.ops.quote_name('id'), connection.ops.quote_name('name')
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertNotIn(id, qstr)
|
|
|
|
self.assertNotIn(name, qstr)
|
2010-04-05 01:05:43 +08:00
|
|
|
|
2013-10-06 02:40:36 +08:00
|
|
|
def test_ticket_18414(self):
|
|
|
|
Article.objects.create(name='one', created=datetime.datetime.now())
|
|
|
|
Article.objects.create(name='one', created=datetime.datetime.now())
|
|
|
|
Article.objects.create(name='two', created=datetime.datetime.now())
|
|
|
|
self.assertTrue(Article.objects.exists())
|
|
|
|
self.assertTrue(Article.objects.distinct().exists())
|
|
|
|
self.assertTrue(Article.objects.distinct()[1:3].exists())
|
|
|
|
self.assertFalse(Article.objects.distinct()[1:1].exists())
|
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('can_distinct_on_fields')
|
2013-10-06 02:40:36 +08:00
|
|
|
def test_ticket_18414_distinct_on(self):
|
|
|
|
Article.objects.create(name='one', created=datetime.datetime.now())
|
|
|
|
Article.objects.create(name='one', created=datetime.datetime.now())
|
|
|
|
Article.objects.create(name='two', created=datetime.datetime.now())
|
|
|
|
self.assertTrue(Article.objects.distinct('name').exists())
|
|
|
|
self.assertTrue(Article.objects.distinct('name')[1:2].exists())
|
|
|
|
self.assertFalse(Article.objects.distinct('name')[2:3].exists())
|
|
|
|
|
2009-04-23 06:16:19 +08:00
|
|
|
|
|
|
|
class QuerysetOrderedTests(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Tests for the Queryset.ordered attribute.
|
|
|
|
"""
|
2010-04-05 01:05:43 +08:00
|
|
|
|
2009-04-23 06:16:19 +08:00
|
|
|
def test_no_default_or_explicit_ordering(self):
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(Annotation.objects.all().ordered, False)
|
2009-04-23 06:16:19 +08:00
|
|
|
|
|
|
|
def test_cleared_default_ordering(self):
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(Tag.objects.all().ordered, True)
|
|
|
|
self.assertIs(Tag.objects.all().order_by().ordered, False)
|
2010-04-05 01:05:43 +08:00
|
|
|
|
2009-04-23 06:16:19 +08:00
|
|
|
def test_explicit_ordering(self):
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(Annotation.objects.all().order_by('id').ordered, True)
|
2010-04-05 01:05:43 +08:00
|
|
|
|
2018-07-16 05:47:17 +08:00
|
|
|
def test_empty_queryset(self):
|
|
|
|
self.assertIs(Annotation.objects.none().ordered, True)
|
|
|
|
|
2009-04-23 06:16:19 +08:00
|
|
|
def test_order_by_extra(self):
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(Annotation.objects.all().extra(order_by=['id']).ordered, True)
|
2010-04-05 01:05:43 +08:00
|
|
|
|
2009-04-23 06:16:19 +08:00
|
|
|
def test_annotated_ordering(self):
|
|
|
|
qs = Annotation.objects.annotate(num_notes=Count('notes'))
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(qs.ordered, False)
|
|
|
|
self.assertIs(qs.order_by('num_notes').ordered, True)
|
2010-04-05 01:05:43 +08:00
|
|
|
|
|
|
|
|
2018-02-07 15:27:47 +08:00
|
|
|
@skipUnlessDBFeature('allow_sliced_subqueries_with_in')
|
2010-04-05 01:05:43 +08:00
|
|
|
class SubqueryTests(TestCase):
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2017-04-27 12:49:17 +08:00
|
|
|
NamedCategory.objects.create(id=1, name='first')
|
|
|
|
NamedCategory.objects.create(id=2, name='second')
|
|
|
|
NamedCategory.objects.create(id=3, name='third')
|
|
|
|
NamedCategory.objects.create(id=4, name='fourth')
|
2010-04-05 01:05:43 +08:00
|
|
|
|
|
|
|
def test_ordered_subselect(self):
|
|
|
|
"Subselects honor any manual ordering"
|
2014-04-15 04:11:52 +08:00
|
|
|
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[0:2])
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual(set(query.values_list('id', flat=True)), {3, 4})
|
2010-04-05 01:05:43 +08:00
|
|
|
|
2014-04-15 04:11:52 +08:00
|
|
|
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[:2])
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual(set(query.values_list('id', flat=True)), {3, 4})
|
2013-09-26 05:14:33 +08:00
|
|
|
|
2014-04-15 04:11:52 +08:00
|
|
|
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:2])
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual(set(query.values_list('id', flat=True)), {3})
|
2010-04-05 01:05:43 +08:00
|
|
|
|
2014-04-15 04:11:52 +08:00
|
|
|
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[2:])
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual(set(query.values_list('id', flat=True)), {1, 2})
|
2013-09-26 05:14:33 +08:00
|
|
|
|
|
|
|
def test_slice_subquery_and_query(self):
|
|
|
|
"""
|
|
|
|
Slice a query that has a sliced subquery
|
|
|
|
"""
|
2014-04-15 04:11:52 +08:00
|
|
|
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[0:2])[0:2]
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual({x.id for x in query}, {3, 4})
|
2014-04-15 04:11:52 +08:00
|
|
|
|
|
|
|
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:3])[1:3]
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual({x.id for x in query}, {3})
|
2013-09-26 05:14:33 +08:00
|
|
|
|
2014-04-15 04:11:52 +08:00
|
|
|
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[2:])[1:]
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual({x.id for x in query}, {2})
|
2014-04-15 04:11:52 +08:00
|
|
|
|
|
|
|
def test_related_sliced_subquery(self):
|
|
|
|
"""
|
|
|
|
Related objects constraints can safely contain sliced subqueries.
|
|
|
|
refs #22434
|
|
|
|
"""
|
2014-12-14 04:15:39 +08:00
|
|
|
generic = NamedCategory.objects.create(id=5, name="Generic")
|
2014-04-15 04:11:52 +08:00
|
|
|
t1 = Tag.objects.create(name='t1', category=generic)
|
|
|
|
t2 = Tag.objects.create(name='t2', category=generic)
|
|
|
|
ManagedModel.objects.create(data='mm1', tag=t1, public=True)
|
|
|
|
mm2 = ManagedModel.objects.create(data='mm2', tag=t2, public=True)
|
2013-09-26 05:14:33 +08:00
|
|
|
|
2014-04-15 04:11:52 +08:00
|
|
|
query = ManagedModel.normal_manager.filter(
|
|
|
|
tag__in=Tag.objects.order_by('-id')[:1]
|
|
|
|
)
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual({x.id for x in query}, {mm2.id})
|
2010-04-05 01:05:43 +08:00
|
|
|
|
|
|
|
def test_sliced_delete(self):
|
|
|
|
"Delete queries can safely contain sliced subqueries"
|
2014-04-15 04:11:52 +08:00
|
|
|
DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[0:1]).delete()
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual(set(DumbCategory.objects.values_list('id', flat=True)), {1, 2, 3})
|
2014-04-15 04:11:52 +08:00
|
|
|
|
|
|
|
DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:2]).delete()
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual(set(DumbCategory.objects.values_list('id', flat=True)), {1, 3})
|
2014-04-15 04:11:52 +08:00
|
|
|
|
|
|
|
DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:]).delete()
|
2014-09-26 20:31:50 +08:00
|
|
|
self.assertEqual(set(DumbCategory.objects.values_list('id', flat=True)), {3})
|
2010-04-13 23:18:10 +08:00
|
|
|
|
2017-04-27 12:49:17 +08:00
|
|
|
def test_distinct_ordered_sliced_subquery(self):
|
|
|
|
# Implicit values('id').
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
NamedCategory.objects.filter(
|
|
|
|
id__in=NamedCategory.objects.distinct().order_by('name')[0:2],
|
|
|
|
).order_by('name').values_list('name', flat=True), ['first', 'fourth']
|
|
|
|
)
|
|
|
|
# Explicit values('id').
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
NamedCategory.objects.filter(
|
|
|
|
id__in=NamedCategory.objects.distinct().order_by('-name').values('id')[0:2],
|
|
|
|
).order_by('name').values_list('name', flat=True), ['second', 'third']
|
|
|
|
)
|
|
|
|
# Annotated value.
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
DumbCategory.objects.filter(
|
|
|
|
id__in=DumbCategory.objects.annotate(
|
|
|
|
double_id=F('id') * 2
|
|
|
|
).order_by('id').distinct().values('double_id')[0:2],
|
|
|
|
).order_by('id').values_list('id', flat=True), [2, 4]
|
|
|
|
)
|
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2018-11-15 22:43:58 +08:00
|
|
|
@skipUnlessDBFeature('allow_sliced_subqueries_with_in')
|
|
|
|
class QuerySetBitwiseOperationTests(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
school = School.objects.create()
|
|
|
|
cls.room_1 = Classroom.objects.create(school=school, has_blackboard=False, name='Room 1')
|
|
|
|
cls.room_2 = Classroom.objects.create(school=school, has_blackboard=True, name='Room 2')
|
|
|
|
cls.room_3 = Classroom.objects.create(school=school, has_blackboard=True, name='Room 3')
|
|
|
|
cls.room_4 = Classroom.objects.create(school=school, has_blackboard=False, name='Room 4')
|
|
|
|
|
|
|
|
def test_or_with_rhs_slice(self):
|
|
|
|
qs1 = Classroom.objects.filter(has_blackboard=True)
|
|
|
|
qs2 = Classroom.objects.filter(has_blackboard=False)[:1]
|
|
|
|
self.assertCountEqual(qs1 | qs2, [self.room_1, self.room_2, self.room_3])
|
|
|
|
|
|
|
|
def test_or_with_lhs_slice(self):
|
|
|
|
qs1 = Classroom.objects.filter(has_blackboard=True)[:1]
|
|
|
|
qs2 = Classroom.objects.filter(has_blackboard=False)
|
|
|
|
self.assertCountEqual(qs1 | qs2, [self.room_1, self.room_2, self.room_4])
|
|
|
|
|
|
|
|
def test_or_with_both_slice(self):
|
|
|
|
qs1 = Classroom.objects.filter(has_blackboard=False)[:1]
|
|
|
|
qs2 = Classroom.objects.filter(has_blackboard=True)[:1]
|
|
|
|
self.assertCountEqual(qs1 | qs2, [self.room_1, self.room_2])
|
|
|
|
|
|
|
|
def test_or_with_both_slice_and_ordering(self):
|
|
|
|
qs1 = Classroom.objects.filter(has_blackboard=False).order_by('-pk')[:1]
|
|
|
|
qs2 = Classroom.objects.filter(has_blackboard=True).order_by('-name')[:1]
|
|
|
|
self.assertCountEqual(qs1 | qs2, [self.room_3, self.room_4])
|
|
|
|
|
|
|
|
|
2010-04-13 23:18:10 +08:00
|
|
|
class CloneTests(TestCase):
|
2012-06-02 09:13:36 +08:00
|
|
|
|
2010-04-13 23:18:10 +08:00
|
|
|
def test_evaluated_queryset_as_argument(self):
|
|
|
|
"#13227 -- If a queryset is already evaluated, it can still be used as a query arg"
|
|
|
|
n = Note(note='Test1', misc='misc')
|
|
|
|
n.save()
|
|
|
|
e = ExtraInfo(info='good', note=n)
|
|
|
|
e.save()
|
|
|
|
|
|
|
|
n_list = Note.objects.all()
|
|
|
|
# Evaluate the Note queryset, populating the query cache
|
|
|
|
list(n_list)
|
2014-04-27 01:18:45 +08:00
|
|
|
# Use the note queryset in a query, and evaluate
|
2010-04-13 23:18:10 +08:00
|
|
|
# that query in a way that involves cloning.
|
2012-09-08 03:06:23 +08:00
|
|
|
self.assertEqual(ExtraInfo.objects.filter(note__in=n_list)[0].info, 'good')
|
2010-10-10 00:45:58 +08:00
|
|
|
|
2012-06-02 09:13:36 +08:00
|
|
|
def test_no_model_options_cloning(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Cloning a queryset does not get out of hand. While complete
|
2012-06-02 09:13:36 +08:00
|
|
|
testing is impossible, this is a sanity check against invalid use of
|
|
|
|
deepcopy. refs #16759.
|
|
|
|
"""
|
|
|
|
opts_class = type(Note._meta)
|
|
|
|
note_deepcopy = getattr(opts_class, "__deepcopy__", None)
|
|
|
|
opts_class.__deepcopy__ = lambda obj, memo: self.fail("Model options shouldn't be cloned.")
|
|
|
|
try:
|
|
|
|
Note.objects.filter(pk__lte=F('pk') + 1).all()
|
|
|
|
finally:
|
|
|
|
if note_deepcopy is None:
|
|
|
|
delattr(opts_class, "__deepcopy__")
|
|
|
|
else:
|
|
|
|
opts_class.__deepcopy__ = note_deepcopy
|
|
|
|
|
|
|
|
def test_no_fields_cloning(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Cloning a queryset does not get out of hand. While complete
|
2012-06-02 09:13:36 +08:00
|
|
|
testing is impossible, this is a sanity check against invalid use of
|
|
|
|
deepcopy. refs #16759.
|
|
|
|
"""
|
2015-01-07 08:16:35 +08:00
|
|
|
opts_class = type(Note._meta.get_field("misc"))
|
2012-06-02 09:13:36 +08:00
|
|
|
note_deepcopy = getattr(opts_class, "__deepcopy__", None)
|
|
|
|
opts_class.__deepcopy__ = lambda obj, memo: self.fail("Model fields shouldn't be cloned")
|
|
|
|
try:
|
|
|
|
Note.objects.filter(note=F('misc')).all()
|
|
|
|
finally:
|
|
|
|
if note_deepcopy is None:
|
|
|
|
delattr(opts_class, "__deepcopy__")
|
|
|
|
else:
|
|
|
|
opts_class.__deepcopy__ = note_deepcopy
|
2010-10-10 00:45:58 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class EmptyQuerySetTests(SimpleTestCase):
|
2010-10-10 00:45:58 +08:00
|
|
|
def test_emptyqueryset_values(self):
|
2012-10-24 05:04:37 +08:00
|
|
|
# #14366 -- Calling .values() on an empty QuerySet and then cloning
|
|
|
|
# that should not cause an error
|
2018-11-27 03:05:02 +08:00
|
|
|
self.assertCountEqual(Number.objects.none().values('num').order_by('num'), [])
|
2010-10-14 07:36:16 +08:00
|
|
|
|
2010-10-10 00:51:34 +08:00
|
|
|
def test_values_subquery(self):
|
2018-11-27 03:05:02 +08:00
|
|
|
self.assertCountEqual(Number.objects.filter(pk__in=Number.objects.none().values('pk')), [])
|
|
|
|
self.assertCountEqual(Number.objects.filter(pk__in=Number.objects.none().values_list('pk')), [])
|
2010-10-14 07:36:16 +08:00
|
|
|
|
2012-10-21 06:11:16 +08:00
|
|
|
def test_ticket_19151(self):
|
2012-10-24 05:04:37 +08:00
|
|
|
# #19151 -- Calling .values() or .values_list() on an empty QuerySet
|
|
|
|
# should return an empty QuerySet and not cause an error.
|
|
|
|
q = Author.objects.none()
|
2018-11-27 03:05:02 +08:00
|
|
|
self.assertCountEqual(q.values(), [])
|
|
|
|
self.assertCountEqual(q.values_list(), [])
|
2012-10-21 06:11:16 +08:00
|
|
|
|
2010-10-14 07:36:16 +08:00
|
|
|
|
2016-09-10 17:36:27 +08:00
|
|
|
class ValuesQuerysetTests(TestCase):
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2010-11-26 22:24:08 +08:00
|
|
|
Number.objects.create(num=72)
|
2013-02-06 07:11:28 +08:00
|
|
|
|
|
|
|
def test_flat_values_list(self):
|
2010-11-26 22:24:08 +08:00
|
|
|
qs = Number.objects.values_list("num")
|
|
|
|
qs = qs.values_list("num", flat=True)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [72])
|
2013-02-06 07:11:28 +08:00
|
|
|
|
|
|
|
def test_extra_values(self):
|
|
|
|
# testing for ticket 14930 issues
|
2019-02-05 19:22:08 +08:00
|
|
|
qs = Number.objects.extra(select={'value_plus_x': 'num+%s', 'value_minus_x': 'num-%s'}, select_params=(1, 2))
|
2013-02-06 07:11:28 +08:00
|
|
|
qs = qs.order_by('value_minus_x')
|
|
|
|
qs = qs.values('num')
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [{'num': 72}])
|
2013-02-06 07:11:28 +08:00
|
|
|
|
|
|
|
def test_extra_values_order_twice(self):
|
|
|
|
# testing for ticket 14930 issues
|
|
|
|
qs = Number.objects.extra(select={'value_plus_one': 'num+1', 'value_minus_one': 'num-1'})
|
|
|
|
qs = qs.order_by('value_minus_one').order_by('value_plus_one')
|
|
|
|
qs = qs.values('num')
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [{'num': 72}])
|
2013-02-06 07:11:28 +08:00
|
|
|
|
|
|
|
def test_extra_values_order_multiple(self):
|
|
|
|
# Postgres doesn't allow constants in order by, so check for that.
|
|
|
|
qs = Number.objects.extra(select={
|
|
|
|
'value_plus_one': 'num+1',
|
|
|
|
'value_minus_one': 'num-1',
|
|
|
|
'constant_value': '1'
|
|
|
|
})
|
|
|
|
qs = qs.order_by('value_plus_one', 'value_minus_one', 'constant_value')
|
|
|
|
qs = qs.values('num')
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [{'num': 72}])
|
2013-02-06 07:11:28 +08:00
|
|
|
|
|
|
|
def test_extra_values_order_in_extra(self):
|
|
|
|
# testing for ticket 14930 issues
|
|
|
|
qs = Number.objects.extra(
|
|
|
|
select={'value_plus_one': 'num+1', 'value_minus_one': 'num-1'},
|
2018-03-16 17:54:34 +08:00
|
|
|
order_by=['value_minus_one'],
|
|
|
|
)
|
2013-02-06 07:11:28 +08:00
|
|
|
qs = qs.values('num')
|
|
|
|
|
2014-08-08 18:08:26 +08:00
|
|
|
def test_extra_select_params_values_order_in_extra(self):
|
|
|
|
# testing for 23259 issue
|
|
|
|
qs = Number.objects.extra(
|
|
|
|
select={'value_plus_x': 'num+%s'},
|
|
|
|
select_params=[1],
|
2018-03-16 17:54:34 +08:00
|
|
|
order_by=['value_plus_x'],
|
|
|
|
)
|
2014-08-08 18:08:26 +08:00
|
|
|
qs = qs.filter(num=72)
|
|
|
|
qs = qs.values('num')
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [{'num': 72}])
|
2014-08-08 18:08:26 +08:00
|
|
|
|
|
|
|
def test_extra_multiple_select_params_values_order_by(self):
|
|
|
|
# testing for 23259 issue
|
2019-02-05 19:22:08 +08:00
|
|
|
qs = Number.objects.extra(select={'value_plus_x': 'num+%s', 'value_minus_x': 'num-%s'}, select_params=(72, 72))
|
2014-08-08 18:08:26 +08:00
|
|
|
qs = qs.order_by('value_minus_x')
|
|
|
|
qs = qs.filter(num=1)
|
|
|
|
qs = qs.values('num')
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [])
|
2014-08-08 18:08:26 +08:00
|
|
|
|
2013-02-06 07:11:28 +08:00
|
|
|
def test_extra_values_list(self):
|
|
|
|
# testing for ticket 14930 issues
|
|
|
|
qs = Number.objects.extra(select={'value_plus_one': 'num+1'})
|
|
|
|
qs = qs.order_by('value_plus_one')
|
|
|
|
qs = qs.values_list('num')
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [(72,)])
|
2013-02-06 07:11:28 +08:00
|
|
|
|
|
|
|
def test_flat_extra_values_list(self):
|
|
|
|
# testing for ticket 14930 issues
|
|
|
|
qs = Number.objects.extra(select={'value_plus_one': 'num+1'})
|
|
|
|
qs = qs.order_by('value_plus_one')
|
|
|
|
qs = qs.values_list('num', flat=True)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [72])
|
2010-11-26 22:24:08 +08:00
|
|
|
|
2014-09-07 06:13:46 +08:00
|
|
|
def test_field_error_values_list(self):
|
|
|
|
# see #23443
|
2016-02-04 19:10:50 +08:00
|
|
|
msg = "Cannot resolve keyword %r into field. Join on 'name' not permitted." % 'foo'
|
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
2014-09-07 06:13:46 +08:00
|
|
|
Tag.objects.values_list('name__foo')
|
|
|
|
|
2017-08-04 18:28:39 +08:00
|
|
|
def test_named_values_list_flat(self):
|
|
|
|
msg = "'flat' and 'named' can't be used together."
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
Number.objects.values_list('num', flat=True, named=True)
|
|
|
|
|
|
|
|
def test_named_values_list_bad_field_name(self):
|
|
|
|
msg = "Type names and field names must be valid identifiers: '1'"
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
Number.objects.extra(select={'1': 'num+1'}).values_list('1', named=True).first()
|
|
|
|
|
|
|
|
def test_named_values_list_with_fields(self):
|
|
|
|
qs = Number.objects.extra(select={'num2': 'num+1'}).annotate(Count('id'))
|
|
|
|
values = qs.values_list('num', 'num2', named=True).first()
|
|
|
|
self.assertEqual(type(values).__name__, 'Row')
|
|
|
|
self.assertEqual(values._fields, ('num', 'num2'))
|
|
|
|
self.assertEqual(values.num, 72)
|
|
|
|
self.assertEqual(values.num2, 73)
|
|
|
|
|
|
|
|
def test_named_values_list_without_fields(self):
|
|
|
|
qs = Number.objects.extra(select={'num2': 'num+1'}).annotate(Count('id'))
|
|
|
|
values = qs.values_list(named=True).first()
|
|
|
|
self.assertEqual(type(values).__name__, 'Row')
|
|
|
|
self.assertEqual(values._fields, ('num2', 'id', 'num', 'id__count'))
|
|
|
|
self.assertEqual(values.num, 72)
|
|
|
|
self.assertEqual(values.num2, 73)
|
|
|
|
self.assertEqual(values.id__count, 1)
|
|
|
|
|
|
|
|
def test_named_values_list_expression_with_default_alias(self):
|
|
|
|
expr = Count('id')
|
|
|
|
values = Number.objects.annotate(id__count1=expr).values_list(expr, 'id__count1', named=True).first()
|
|
|
|
self.assertEqual(values._fields, ('id__count2', 'id__count1'))
|
|
|
|
|
|
|
|
def test_named_values_list_expression(self):
|
|
|
|
expr = F('num') + 1
|
|
|
|
qs = Number.objects.annotate(combinedexpression1=expr).values_list(expr, 'combinedexpression1', named=True)
|
|
|
|
values = qs.first()
|
|
|
|
self.assertEqual(values._fields, ('combinedexpression2', 'combinedexpression1'))
|
|
|
|
|
2010-11-26 22:24:08 +08:00
|
|
|
|
2014-05-16 17:55:09 +08:00
|
|
|
class QuerySetSupportsPythonIdioms(TestCase):
|
|
|
|
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2014-05-16 17:55:09 +08:00
|
|
|
some_date = datetime.datetime(2014, 5, 16, 12, 1)
|
|
|
|
for i in range(1, 8):
|
|
|
|
Article.objects.create(
|
|
|
|
name="Article {}".format(i), created=some_date)
|
|
|
|
|
|
|
|
def get_ordered_articles(self):
|
|
|
|
return Article.objects.all().order_by('name')
|
|
|
|
|
|
|
|
def test_can_get_items_using_index_and_slice_notation(self):
|
|
|
|
self.assertEqual(self.get_ordered_articles()[0].name, 'Article 1')
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.get_ordered_articles()[1:3],
|
|
|
|
["<Article: Article 2>", "<Article: Article 3>"]
|
|
|
|
)
|
2014-05-16 17:55:09 +08:00
|
|
|
|
|
|
|
def test_slicing_with_steps_can_be_used(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.get_ordered_articles()[::2], [
|
|
|
|
"<Article: Article 1>",
|
|
|
|
"<Article: Article 3>",
|
|
|
|
"<Article: Article 5>",
|
|
|
|
"<Article: Article 7>"
|
|
|
|
]
|
|
|
|
)
|
2014-05-16 17:55:09 +08:00
|
|
|
|
|
|
|
def test_slicing_without_step_is_lazy(self):
|
|
|
|
with self.assertNumQueries(0):
|
|
|
|
self.get_ordered_articles()[0:5]
|
|
|
|
|
|
|
|
def test_slicing_with_tests_is_not_lazy(self):
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
self.get_ordered_articles()[0:5:3]
|
|
|
|
|
|
|
|
def test_slicing_can_slice_again_after_slicing(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.get_ordered_articles()[0:5][0:2],
|
|
|
|
["<Article: Article 1>", "<Article: Article 2>"]
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(self.get_ordered_articles()[0:5][4:], ["<Article: Article 5>"])
|
2014-05-16 17:55:09 +08:00
|
|
|
self.assertQuerysetEqual(self.get_ordered_articles()[0:5][5:], [])
|
|
|
|
|
|
|
|
# Some more tests!
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.get_ordered_articles()[2:][0:2],
|
|
|
|
["<Article: Article 3>", "<Article: Article 4>"]
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.get_ordered_articles()[2:][:2],
|
|
|
|
["<Article: Article 3>", "<Article: Article 4>"]
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(self.get_ordered_articles()[2:][2:3], ["<Article: Article 5>"])
|
2014-05-16 17:55:09 +08:00
|
|
|
|
|
|
|
# Using an offset without a limit is also possible.
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.get_ordered_articles()[5:],
|
|
|
|
["<Article: Article 6>", "<Article: Article 7>"]
|
|
|
|
)
|
2014-05-16 17:55:09 +08:00
|
|
|
|
|
|
|
def test_slicing_cannot_filter_queryset_once_sliced(self):
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(AssertionError, "Cannot filter a query once a slice has been taken."):
|
2017-12-29 04:07:29 +08:00
|
|
|
Article.objects.all()[0:5].filter(id=1)
|
2014-05-16 17:55:09 +08:00
|
|
|
|
|
|
|
def test_slicing_cannot_reorder_queryset_once_sliced(self):
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(AssertionError, "Cannot reorder a query once a slice has been taken."):
|
2017-12-29 04:07:29 +08:00
|
|
|
Article.objects.all()[0:5].order_by('id')
|
2014-05-16 17:55:09 +08:00
|
|
|
|
|
|
|
def test_slicing_cannot_combine_queries_once_sliced(self):
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(AssertionError, "Cannot combine queries once a slice has been taken."):
|
|
|
|
Article.objects.all()[0:1] & Article.objects.all()[4:5]
|
2014-05-16 17:55:09 +08:00
|
|
|
|
|
|
|
def test_slicing_negative_indexing_not_supported_for_single_element(self):
|
|
|
|
"""hint: inverting your ordering might do what you need"""
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(AssertionError, "Negative indexing is not supported."):
|
|
|
|
Article.objects.all()[-1]
|
2014-05-16 17:55:09 +08:00
|
|
|
|
|
|
|
def test_slicing_negative_indexing_not_supported_for_range(self):
|
|
|
|
"""hint: inverting your ordering might do what you need"""
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(AssertionError, "Negative indexing is not supported."):
|
|
|
|
Article.objects.all()[0:-5]
|
2014-05-16 17:55:09 +08:00
|
|
|
|
|
|
|
def test_can_get_number_of_items_in_queryset_using_standard_len(self):
|
|
|
|
self.assertEqual(len(Article.objects.filter(name__exact='Article 1')), 1)
|
|
|
|
|
|
|
|
def test_can_combine_queries_using_and_and_or_operators(self):
|
|
|
|
s1 = Article.objects.filter(name__exact='Article 1')
|
|
|
|
s2 = Article.objects.filter(name__exact='Article 2')
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
(s1 | s2).order_by('name'),
|
|
|
|
["<Article: Article 1>", "<Article: Article 2>"]
|
|
|
|
)
|
2014-05-16 17:55:09 +08:00
|
|
|
self.assertQuerysetEqual(s1 & s2, [])
|
|
|
|
|
|
|
|
|
2016-09-10 17:36:27 +08:00
|
|
|
class WeirdQuerysetSlicingTests(TestCase):
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2010-10-14 07:36:16 +08:00
|
|
|
Number.objects.create(num=1)
|
|
|
|
Number.objects.create(num=2)
|
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
Article.objects.create(name='one', created=datetime.datetime.now())
|
|
|
|
Article.objects.create(name='two', created=datetime.datetime.now())
|
|
|
|
Article.objects.create(name='three', created=datetime.datetime.now())
|
|
|
|
Article.objects.create(name='four', created=datetime.datetime.now())
|
|
|
|
|
2015-08-16 07:05:42 +08:00
|
|
|
food = Food.objects.create(name='spam')
|
|
|
|
Eaten.objects.create(meal='spam with eggs', food=food)
|
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
def test_tickets_7698_10202(self):
|
|
|
|
# People like to slice with '0' as the high-water mark.
|
|
|
|
self.assertQuerysetEqual(Article.objects.all()[0:0], [])
|
|
|
|
self.assertQuerysetEqual(Article.objects.all()[0:0][:10], [])
|
|
|
|
self.assertEqual(Article.objects.all()[:0].count(), 0)
|
2017-06-22 04:28:16 +08:00
|
|
|
with self.assertRaisesMessage(TypeError, 'Cannot reverse a query once a slice has been taken.'):
|
2016-01-04 16:50:08 +08:00
|
|
|
Article.objects.all()[:0].latest('created')
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2010-10-14 07:36:16 +08:00
|
|
|
def test_empty_resultset_sql(self):
|
|
|
|
# ticket #12192
|
|
|
|
self.assertNumQueries(0, lambda: list(Number.objects.all()[1:1]))
|
2010-10-19 12:07:22 +08:00
|
|
|
|
2015-08-16 07:05:42 +08:00
|
|
|
def test_empty_sliced_subquery(self):
|
|
|
|
self.assertEqual(Eaten.objects.filter(food__in=Food.objects.all()[0:0]).count(), 0)
|
|
|
|
|
|
|
|
def test_empty_sliced_subquery_exclude(self):
|
|
|
|
self.assertEqual(Eaten.objects.exclude(food__in=Food.objects.all()[0:0]).count(), 1)
|
|
|
|
|
2015-12-10 18:42:25 +08:00
|
|
|
def test_zero_length_values_slicing(self):
|
|
|
|
n = 42
|
|
|
|
with self.assertNumQueries(0):
|
|
|
|
self.assertQuerysetEqual(Article.objects.values()[n:n], [])
|
|
|
|
self.assertQuerysetEqual(Article.objects.values_list()[n:n], [])
|
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
class EscapingTests(TestCase):
|
|
|
|
def test_ticket_7302(self):
|
|
|
|
# Reserved names are appropriately escaped
|
2013-10-19 20:31:38 +08:00
|
|
|
ReservedName.objects.create(name='a', order=42)
|
2010-11-26 22:24:08 +08:00
|
|
|
ReservedName.objects.create(name='b', order=37)
|
2010-10-19 12:07:22 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
ReservedName.objects.all().order_by('order'),
|
|
|
|
['<ReservedName: b>', '<ReservedName: a>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-27 03:15:03 +08:00
|
|
|
ReservedName.objects.extra(select={'stuff': 'name'}, order_by=('order', 'stuff')),
|
2010-10-19 12:07:22 +08:00
|
|
|
['<ReservedName: b>', '<ReservedName: a>']
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2011-01-25 11:14:28 +08:00
|
|
|
class ToFieldTests(TestCase):
|
|
|
|
def test_in_query(self):
|
|
|
|
apple = Food.objects.create(name="apple")
|
|
|
|
pear = Food.objects.create(name="pear")
|
|
|
|
lunch = Eaten.objects.create(food=apple, meal="lunch")
|
|
|
|
dinner = Eaten.objects.create(food=pear, meal="dinner")
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
set(Eaten.objects.filter(food__in=[apple, pear])),
|
2014-09-26 20:31:50 +08:00
|
|
|
{lunch, dinner},
|
2011-01-25 11:14:28 +08:00
|
|
|
)
|
|
|
|
|
2016-02-11 14:39:37 +08:00
|
|
|
def test_in_subquery(self):
|
|
|
|
apple = Food.objects.create(name="apple")
|
|
|
|
lunch = Eaten.objects.create(food=apple, meal="lunch")
|
|
|
|
self.assertEqual(
|
|
|
|
set(Eaten.objects.filter(food__in=Food.objects.filter(name='apple'))),
|
|
|
|
{lunch}
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
set(Eaten.objects.filter(food__in=Food.objects.filter(name='apple').values('eaten__meal'))),
|
|
|
|
set()
|
|
|
|
)
|
2016-03-17 21:45:00 +08:00
|
|
|
self.assertEqual(
|
|
|
|
set(Food.objects.filter(eaten__in=Eaten.objects.filter(meal='lunch'))),
|
|
|
|
{apple}
|
|
|
|
)
|
2016-02-11 14:39:37 +08:00
|
|
|
|
2017-04-23 05:04:02 +08:00
|
|
|
def test_nested_in_subquery(self):
|
|
|
|
extra = ExtraInfo.objects.create()
|
|
|
|
author = Author.objects.create(num=42, extra=extra)
|
|
|
|
report = Report.objects.create(creator=author)
|
|
|
|
comment = ReportComment.objects.create(report=report)
|
|
|
|
comments = ReportComment.objects.filter(
|
|
|
|
report__in=Report.objects.filter(
|
|
|
|
creator__in=extra.author_set.all(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(comments, [comment])
|
|
|
|
|
2011-01-25 11:14:28 +08:00
|
|
|
def test_reverse_in(self):
|
|
|
|
apple = Food.objects.create(name="apple")
|
|
|
|
pear = Food.objects.create(name="pear")
|
|
|
|
lunch_apple = Eaten.objects.create(food=apple, meal="lunch")
|
|
|
|
lunch_pear = Eaten.objects.create(food=pear, meal="dinner")
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
set(Food.objects.filter(eaten__in=[lunch_apple, lunch_pear])),
|
2014-09-26 20:31:50 +08:00
|
|
|
{apple, pear}
|
2011-01-25 11:14:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_single_object(self):
|
|
|
|
apple = Food.objects.create(name="apple")
|
|
|
|
lunch = Eaten.objects.create(food=apple, meal="lunch")
|
|
|
|
dinner = Eaten.objects.create(food=apple, meal="dinner")
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
set(Eaten.objects.filter(food=apple)),
|
2014-09-26 20:31:50 +08:00
|
|
|
{lunch, dinner}
|
2011-01-25 11:14:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_single_object_reverse(self):
|
|
|
|
apple = Food.objects.create(name="apple")
|
|
|
|
lunch = Eaten.objects.create(food=apple, meal="lunch")
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
set(Food.objects.filter(eaten=lunch)),
|
2014-09-26 20:31:50 +08:00
|
|
|
{apple}
|
2011-01-25 11:14:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_recursive_fk(self):
|
|
|
|
node1 = Node.objects.create(num=42)
|
|
|
|
node2 = Node.objects.create(num=1, parent=node1)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
list(Node.objects.filter(parent=node1)),
|
|
|
|
[node2]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_recursive_fk_reverse(self):
|
|
|
|
node1 = Node.objects.create(num=42)
|
|
|
|
node2 = Node.objects.create(num=1, parent=node1)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
list(Node.objects.filter(node=node2)),
|
|
|
|
[node1]
|
|
|
|
)
|
|
|
|
|
2016-08-18 04:09:50 +08:00
|
|
|
|
|
|
|
class IsNullTests(TestCase):
|
|
|
|
def test_primary_key(self):
|
|
|
|
custom = CustomPk.objects.create(name='pk')
|
|
|
|
null = Related.objects.create()
|
|
|
|
notnull = Related.objects.create(custom=custom)
|
|
|
|
self.assertSequenceEqual(Related.objects.filter(custom__isnull=False), [notnull])
|
|
|
|
self.assertSequenceEqual(Related.objects.filter(custom__isnull=True), [null])
|
|
|
|
|
|
|
|
def test_to_field(self):
|
2016-08-02 23:27:45 +08:00
|
|
|
apple = Food.objects.create(name="apple")
|
|
|
|
Eaten.objects.create(food=apple, meal="lunch")
|
|
|
|
Eaten.objects.create(meal="lunch")
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Eaten.objects.filter(food__isnull=False),
|
|
|
|
['<Eaten: apple at lunch>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Eaten.objects.filter(food__isnull=True),
|
|
|
|
['<Eaten: None at lunch>']
|
|
|
|
)
|
|
|
|
|
2011-01-25 11:14:28 +08:00
|
|
|
|
2016-09-10 17:36:27 +08:00
|
|
|
class ConditionalTests(TestCase):
|
2011-12-23 04:42:40 +08:00
|
|
|
"""Tests whose execution depend on different environment conditions like
|
2010-10-19 12:07:22 +08:00
|
|
|
Python version or DB backend features"""
|
|
|
|
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2010-10-20 01:27:38 +08:00
|
|
|
generic = NamedCategory.objects.create(name="Generic")
|
|
|
|
t1 = Tag.objects.create(name='t1', category=generic)
|
2013-10-19 20:31:38 +08:00
|
|
|
Tag.objects.create(name='t2', parent=t1, category=generic)
|
2010-10-20 01:27:38 +08:00
|
|
|
t3 = Tag.objects.create(name='t3', parent=t1)
|
2013-10-19 20:31:38 +08:00
|
|
|
Tag.objects.create(name='t4', parent=t3)
|
|
|
|
Tag.objects.create(name='t5', parent=t3)
|
2010-10-20 01:27:38 +08:00
|
|
|
|
2010-10-19 12:07:22 +08:00
|
|
|
def test_infinite_loop(self):
|
|
|
|
# If you're not careful, it's possible to introduce infinite loops via
|
|
|
|
# default ordering on foreign keys in a cycle. We detect that.
|
2016-01-04 16:50:08 +08:00
|
|
|
with self.assertRaisesMessage(FieldError, 'Infinite loop caused by ordering.'):
|
|
|
|
list(LoopX.objects.all()) # Force queryset evaluation with list()
|
|
|
|
with self.assertRaisesMessage(FieldError, 'Infinite loop caused by ordering.'):
|
|
|
|
list(LoopZ.objects.all()) # Force queryset evaluation with list()
|
2010-10-19 12:07:22 +08:00
|
|
|
|
|
|
|
# Note that this doesn't cause an infinite loop, since the default
|
|
|
|
# ordering on the Tag model is empty (and thus defaults to using "id"
|
|
|
|
# for the related field).
|
|
|
|
self.assertEqual(len(Tag.objects.order_by('parent')), 5)
|
|
|
|
|
2014-03-02 22:25:53 +08:00
|
|
|
# ... but you can still order in a non-recursive fashion among linked
|
2010-10-19 12:07:22 +08:00
|
|
|
# fields (the previous test failed because the default ordering was
|
|
|
|
# recursive).
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
LoopX.objects.all().order_by('y__x__y__x__id'),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
|
|
|
# When grouping without specifying ordering, we add an explicit "ORDER BY NULL"
|
|
|
|
# portion in MySQL to prevent unnecessary sorting.
|
|
|
|
@skipUnlessDBFeature('requires_explicit_null_ordering_when_grouping')
|
|
|
|
def test_null_ordering_added(self):
|
|
|
|
query = Tag.objects.values_list('parent_id', flat=True).order_by().query
|
|
|
|
query.group_by = ['parent_id']
|
|
|
|
sql = query.get_compiler(DEFAULT_DB_ALIAS).as_sql()[0]
|
|
|
|
fragment = "ORDER BY "
|
|
|
|
pos = sql.find(fragment)
|
|
|
|
self.assertEqual(sql.find(fragment, pos + 1), -1)
|
|
|
|
self.assertEqual(sql.find("NULL", pos + len(fragment)), pos + len(fragment))
|
|
|
|
|
2017-03-25 01:37:03 +08:00
|
|
|
def test_in_list_limit(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
# The "in" lookup works with lists of 1000 items or more.
|
2013-02-11 03:07:24 +08:00
|
|
|
# The numbers amount is picked to force three different IN batches
|
|
|
|
# for Oracle, yet to be less than 2100 parameter limit for MSSQL.
|
2014-12-31 22:49:03 +08:00
|
|
|
numbers = list(range(2050))
|
2017-03-25 01:37:03 +08:00
|
|
|
max_query_params = connection.features.max_query_params
|
|
|
|
if max_query_params is None or max_query_params >= len(numbers):
|
|
|
|
Number.objects.bulk_create(Number(num=num) for num in numbers)
|
|
|
|
for number in [1000, 1001, 2000, len(numbers)]:
|
|
|
|
with self.subTest(number=number):
|
|
|
|
self.assertEqual(Number.objects.filter(num__in=numbers[:number]).count(), number)
|
2011-03-03 21:51:54 +08:00
|
|
|
|
2011-12-23 04:42:40 +08:00
|
|
|
|
2011-03-03 21:51:54 +08:00
|
|
|
class UnionTests(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Tests for the union of two querysets. Bug #12252.
|
|
|
|
"""
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2011-03-03 21:51:54 +08:00
|
|
|
objectas = []
|
|
|
|
objectbs = []
|
|
|
|
objectcs = []
|
|
|
|
a_info = ['one', 'two', 'three']
|
|
|
|
for name in a_info:
|
|
|
|
o = ObjectA(name=name)
|
|
|
|
o.save()
|
|
|
|
objectas.append(o)
|
|
|
|
b_info = [('un', 1, objectas[0]), ('deux', 2, objectas[0]), ('trois', 3, objectas[2])]
|
|
|
|
for name, number, objecta in b_info:
|
2011-03-09 03:26:32 +08:00
|
|
|
o = ObjectB(name=name, num=number, objecta=objecta)
|
2011-03-03 21:51:54 +08:00
|
|
|
o.save()
|
|
|
|
objectbs.append(o)
|
|
|
|
c_info = [('ein', objectas[2], objectbs[2]), ('zwei', objectas[1], objectbs[1])]
|
|
|
|
for name, objecta, objectb in c_info:
|
|
|
|
o = ObjectC(name=name, objecta=objecta, objectb=objectb)
|
|
|
|
o.save()
|
|
|
|
objectcs.append(o)
|
|
|
|
|
|
|
|
def check_union(self, model, Q1, Q2):
|
|
|
|
filter = model.objects.filter
|
|
|
|
self.assertEqual(set(filter(Q1) | filter(Q2)), set(filter(Q1 | Q2)))
|
|
|
|
self.assertEqual(set(filter(Q2) | filter(Q1)), set(filter(Q1 | Q2)))
|
|
|
|
|
|
|
|
def test_A_AB(self):
|
|
|
|
Q1 = Q(name='two')
|
|
|
|
Q2 = Q(objectb__name='deux')
|
|
|
|
self.check_union(ObjectA, Q1, Q2)
|
|
|
|
|
|
|
|
def test_A_AB2(self):
|
|
|
|
Q1 = Q(name='two')
|
2011-03-09 03:26:32 +08:00
|
|
|
Q2 = Q(objectb__name='deux', objectb__num=2)
|
2011-03-03 21:51:54 +08:00
|
|
|
self.check_union(ObjectA, Q1, Q2)
|
|
|
|
|
|
|
|
def test_AB_ACB(self):
|
|
|
|
Q1 = Q(objectb__name='deux')
|
|
|
|
Q2 = Q(objectc__objectb__name='deux')
|
|
|
|
self.check_union(ObjectA, Q1, Q2)
|
|
|
|
|
|
|
|
def test_BAB_BAC(self):
|
|
|
|
Q1 = Q(objecta__objectb__name='deux')
|
|
|
|
Q2 = Q(objecta__objectc__name='ein')
|
|
|
|
self.check_union(ObjectB, Q1, Q2)
|
|
|
|
|
|
|
|
def test_BAB_BACB(self):
|
|
|
|
Q1 = Q(objecta__objectb__name='deux')
|
|
|
|
Q2 = Q(objecta__objectc__objectb__name='trois')
|
|
|
|
self.check_union(ObjectB, Q1, Q2)
|
|
|
|
|
|
|
|
def test_BA_BCA__BAB_BAC_BCA(self):
|
|
|
|
Q1 = Q(objecta__name='one', objectc__objecta__name='two')
|
|
|
|
Q2 = Q(objecta__objectc__name='ein', objectc__objecta__name='three', objecta__objectb__name='trois')
|
|
|
|
self.check_union(ObjectB, Q1, Q2)
|
2011-10-17 04:06:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DefaultValuesInsertTest(TestCase):
|
|
|
|
def test_no_extra_params(self):
|
2016-06-28 23:21:26 +08:00
|
|
|
"""
|
|
|
|
Can create an instance of a model with only the PK field (#17056)."
|
|
|
|
"""
|
|
|
|
DumbCategory.objects.create()
|
2012-04-29 18:45:46 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-11-03 04:35:45 +08:00
|
|
|
class ExcludeTests(TestCase):
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2012-08-25 21:33:07 +08:00
|
|
|
f1 = Food.objects.create(name='apples')
|
|
|
|
Food.objects.create(name='oranges')
|
|
|
|
Eaten.objects.create(food=f1, meal='dinner')
|
|
|
|
j1 = Job.objects.create(name='Manager')
|
|
|
|
r1 = Responsibility.objects.create(description='Playing golf')
|
|
|
|
j2 = Job.objects.create(name='Programmer')
|
|
|
|
r2 = Responsibility.objects.create(description='Programming')
|
|
|
|
JobResponsibilities.objects.create(job=j1, responsibility=r1)
|
|
|
|
JobResponsibilities.objects.create(job=j2, responsibility=r2)
|
|
|
|
|
|
|
|
def test_to_field(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Food.objects.exclude(eaten__meal='dinner'),
|
|
|
|
['<Food: oranges>'])
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Job.objects.exclude(responsibilities__description='Playing golf'),
|
|
|
|
['<Job: Programmer>'])
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Responsibility.objects.exclude(jobs__name='Manager'),
|
|
|
|
['<Responsibility: Programming>'])
|
|
|
|
|
2013-11-03 04:35:45 +08:00
|
|
|
def test_ticket14511(self):
|
|
|
|
alex = Person.objects.get_or_create(name='Alex')[0]
|
|
|
|
jane = Person.objects.get_or_create(name='Jane')[0]
|
|
|
|
|
|
|
|
oracle = Company.objects.get_or_create(name='Oracle')[0]
|
|
|
|
google = Company.objects.get_or_create(name='Google')[0]
|
|
|
|
microsoft = Company.objects.get_or_create(name='Microsoft')[0]
|
|
|
|
intel = Company.objects.get_or_create(name='Intel')[0]
|
|
|
|
|
|
|
|
def employ(employer, employee, title):
|
|
|
|
Employment.objects.get_or_create(employee=employee, employer=employer, title=title)
|
|
|
|
|
|
|
|
employ(oracle, alex, 'Engineer')
|
|
|
|
employ(oracle, alex, 'Developer')
|
|
|
|
employ(google, alex, 'Engineer')
|
|
|
|
employ(google, alex, 'Manager')
|
|
|
|
employ(microsoft, alex, 'Manager')
|
|
|
|
employ(intel, alex, 'Manager')
|
|
|
|
|
|
|
|
employ(microsoft, jane, 'Developer')
|
|
|
|
employ(intel, jane, 'Manager')
|
|
|
|
|
|
|
|
alex_tech_employers = alex.employers.filter(
|
|
|
|
employment__title__in=('Engineer', 'Developer')).distinct().order_by('name')
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(alex_tech_employers, [google, oracle])
|
2013-11-03 04:35:45 +08:00
|
|
|
|
|
|
|
alex_nontech_employers = alex.employers.exclude(
|
|
|
|
employment__title__in=('Engineer', 'Developer')).distinct().order_by('name')
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(alex_nontech_employers, [google, intel, microsoft])
|
2013-11-03 04:35:45 +08:00
|
|
|
|
2019-03-08 15:06:34 +08:00
|
|
|
def test_exclude_reverse_fk_field_ref(self):
|
|
|
|
tag = Tag.objects.create()
|
|
|
|
Note.objects.create(tag=tag, note='note')
|
|
|
|
annotation = Annotation.objects.create(name='annotation', tag=tag)
|
|
|
|
self.assertEqual(Annotation.objects.exclude(tag__note__note=F('name')).get(), annotation)
|
|
|
|
|
2019-03-28 14:10:54 +08:00
|
|
|
def test_exclude_with_circular_fk_relation(self):
|
|
|
|
self.assertEqual(ObjectB.objects.exclude(objecta__objectb__name=F('name')).count(), 0)
|
|
|
|
|
2013-11-03 04:35:45 +08:00
|
|
|
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
class ExcludeTest17600(TestCase):
|
|
|
|
"""
|
|
|
|
Some regressiontests for ticket #17600. Some of these likely duplicate
|
|
|
|
other existing tests.
|
|
|
|
"""
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
# Create a few Orders.
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.o1 = Order.objects.create(pk=1)
|
|
|
|
cls.o2 = Order.objects.create(pk=2)
|
|
|
|
cls.o3 = Order.objects.create(pk=3)
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
|
|
|
|
# Create some OrderItems for the first order with homogeneous
|
|
|
|
# status_id values
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.oi1 = OrderItem.objects.create(order=cls.o1, status=1)
|
|
|
|
cls.oi2 = OrderItem.objects.create(order=cls.o1, status=1)
|
|
|
|
cls.oi3 = OrderItem.objects.create(order=cls.o1, status=1)
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
|
|
|
|
# Create some OrderItems for the second order with heterogeneous
|
|
|
|
# status_id values
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.oi4 = OrderItem.objects.create(order=cls.o2, status=1)
|
|
|
|
cls.oi5 = OrderItem.objects.create(order=cls.o2, status=2)
|
|
|
|
cls.oi6 = OrderItem.objects.create(order=cls.o2, status=3)
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
|
|
|
|
# Create some OrderItems for the second order with heterogeneous
|
|
|
|
# status_id values
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.oi7 = OrderItem.objects.create(order=cls.o3, status=2)
|
|
|
|
cls.oi8 = OrderItem.objects.create(order=cls.o3, status=3)
|
|
|
|
cls.oi9 = OrderItem.objects.create(order=cls.o3, status=4)
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
|
|
|
|
def test_exclude_plain(self):
|
|
|
|
"""
|
|
|
|
This should exclude Orders which have some items with status 1
|
|
|
|
"""
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Order.objects.exclude(items__status=1),
|
|
|
|
['<Order: 3>'])
|
|
|
|
|
|
|
|
def test_exclude_plain_distinct(self):
|
|
|
|
"""
|
|
|
|
This should exclude Orders which have some items with status 1
|
|
|
|
"""
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Order.objects.exclude(items__status=1).distinct(),
|
|
|
|
['<Order: 3>'])
|
|
|
|
|
|
|
|
def test_exclude_with_q_object_distinct(self):
|
|
|
|
"""
|
|
|
|
This should exclude Orders which have some items with status 1
|
|
|
|
"""
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Order.objects.exclude(Q(items__status=1)).distinct(),
|
|
|
|
['<Order: 3>'])
|
|
|
|
|
|
|
|
def test_exclude_with_q_object_no_distinct(self):
|
|
|
|
"""
|
|
|
|
This should exclude Orders which have some items with status 1
|
|
|
|
"""
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Order.objects.exclude(Q(items__status=1)),
|
|
|
|
['<Order: 3>'])
|
|
|
|
|
|
|
|
def test_exclude_with_q_is_equal_to_plain_exclude(self):
|
|
|
|
"""
|
|
|
|
Using exclude(condition) and exclude(Q(condition)) should
|
|
|
|
yield the same QuerySet
|
|
|
|
"""
|
|
|
|
self.assertEqual(
|
|
|
|
list(Order.objects.exclude(items__status=1).distinct()),
|
|
|
|
list(Order.objects.exclude(Q(items__status=1)).distinct()))
|
|
|
|
|
|
|
|
def test_exclude_with_q_is_equal_to_plain_exclude_variation(self):
|
|
|
|
"""
|
|
|
|
Using exclude(condition) and exclude(Q(condition)) should
|
|
|
|
yield the same QuerySet
|
|
|
|
"""
|
|
|
|
self.assertEqual(
|
|
|
|
list(Order.objects.exclude(items__status=1)),
|
|
|
|
list(Order.objects.exclude(Q(items__status=1)).distinct()))
|
|
|
|
|
|
|
|
@unittest.expectedFailure
|
|
|
|
def test_only_orders_with_all_items_having_status_1(self):
|
|
|
|
"""
|
|
|
|
This should only return orders having ALL items set to status 1, or
|
|
|
|
those items not having any orders at all. The correct way to write
|
|
|
|
this query in SQL seems to be using two nested subqueries.
|
|
|
|
"""
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Order.objects.exclude(~Q(items__status=1)).distinct(),
|
|
|
|
['<Order: 1>'])
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-09-30 01:02:25 +08:00
|
|
|
class Exclude15786(TestCase):
|
|
|
|
"""Regression test for #15786"""
|
|
|
|
def test_ticket15786(self):
|
|
|
|
c1 = SimpleCategory.objects.create(name='c1')
|
|
|
|
c2 = SimpleCategory.objects.create(name='c2')
|
2013-10-19 20:31:38 +08:00
|
|
|
OneToOneCategory.objects.create(category=c1)
|
|
|
|
OneToOneCategory.objects.create(category=c2)
|
2013-09-30 01:02:25 +08:00
|
|
|
rel = CategoryRelationship.objects.create(first=c1, second=c2)
|
|
|
|
self.assertEqual(
|
|
|
|
CategoryRelationship.objects.exclude(
|
|
|
|
first__onetoonecategory=F('second__onetoonecategory')
|
|
|
|
).get(), rel
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2012-04-29 18:45:46 +08:00
|
|
|
class NullInExcludeTest(TestCase):
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2012-04-29 18:45:46 +08:00
|
|
|
NullableName.objects.create(name='i1')
|
|
|
|
NullableName.objects.create()
|
|
|
|
|
|
|
|
def test_null_in_exclude_qs(self):
|
|
|
|
none_val = '' if connection.features.interprets_empty_strings_as_nulls else None
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
NullableName.objects.exclude(name__in=[]),
|
|
|
|
['i1', none_val], attrgetter('name'))
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
NullableName.objects.exclude(name__in=['i1']),
|
|
|
|
[none_val], attrgetter('name'))
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
NullableName.objects.exclude(name__in=['i3']),
|
|
|
|
['i1', none_val], attrgetter('name'))
|
|
|
|
inner_qs = NullableName.objects.filter(name='i1').values_list('name')
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
NullableName.objects.exclude(name__in=inner_qs),
|
|
|
|
[none_val], attrgetter('name'))
|
2016-10-27 15:53:39 +08:00
|
|
|
# The inner queryset wasn't executed - it should be turned
|
2012-04-29 18:45:46 +08:00
|
|
|
# into subquery above
|
|
|
|
self.assertIs(inner_qs._result_cache, None)
|
|
|
|
|
|
|
|
@unittest.expectedFailure
|
|
|
|
def test_col_not_in_list_containing_null(self):
|
|
|
|
"""
|
|
|
|
The following case is not handled properly because
|
|
|
|
SQL's COL NOT IN (list containing null) handling is too weird to
|
|
|
|
abstract away.
|
|
|
|
"""
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
NullableName.objects.exclude(name__in=[None]),
|
|
|
|
['i1'], attrgetter('name'))
|
2012-04-30 00:25:46 +08:00
|
|
|
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
def test_double_exclude(self):
|
|
|
|
self.assertEqual(
|
|
|
|
list(NullableName.objects.filter(~~Q(name='i1'))),
|
|
|
|
list(NullableName.objects.filter(Q(name='i1'))))
|
|
|
|
self.assertNotIn(
|
|
|
|
'IS NOT NULL',
|
|
|
|
str(NullableName.objects.filter(~~Q(name='i1')).query))
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-04-30 00:25:46 +08:00
|
|
|
class EmptyStringsAsNullTest(TestCase):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Filtering on non-null character fields works as expected.
|
2012-04-30 00:25:46 +08:00
|
|
|
The reason for these tests is that Oracle treats '' as NULL, and this
|
|
|
|
can cause problems in query construction. Refs #17957.
|
|
|
|
"""
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.nc = NamedCategory.objects.create(name='')
|
2012-04-30 00:25:46 +08:00
|
|
|
|
|
|
|
def test_direct_exclude(self):
|
|
|
|
self.assertQuerysetEqual(
|
2017-02-03 09:43:21 +08:00
|
|
|
NamedCategory.objects.exclude(name__in=['nonexistent']),
|
2012-04-30 00:25:46 +08:00
|
|
|
[self.nc.pk], attrgetter('pk')
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_joined_exclude(self):
|
|
|
|
self.assertQuerysetEqual(
|
2017-02-03 09:43:21 +08:00
|
|
|
DumbCategory.objects.exclude(namedcategory__name__in=['nonexistent']),
|
2012-04-30 00:25:46 +08:00
|
|
|
[self.nc.pk], attrgetter('pk')
|
|
|
|
)
|
2012-05-10 01:33:31 +08:00
|
|
|
|
2013-09-24 02:18:03 +08:00
|
|
|
def test_21001(self):
|
|
|
|
foo = NamedCategory.objects.create(name='foo')
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
NamedCategory.objects.exclude(name=''),
|
|
|
|
[foo.pk], attrgetter('pk')
|
|
|
|
)
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-05-10 01:33:31 +08:00
|
|
|
class ProxyQueryCleanupTest(TestCase):
|
|
|
|
def test_evaluated_proxy_count(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Generating the query string doesn't alter the query's state
|
2012-05-10 01:33:31 +08:00
|
|
|
in irreversible ways. Refs #18248.
|
|
|
|
"""
|
|
|
|
ProxyCategory.objects.create()
|
|
|
|
qs = ProxyCategory.objects.all()
|
|
|
|
self.assertEqual(qs.count(), 1)
|
|
|
|
str(qs.query)
|
|
|
|
self.assertEqual(qs.count(), 1)
|
2012-05-26 10:55:33 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class WhereNodeTest(SimpleTestCase):
|
2017-01-19 15:39:46 +08:00
|
|
|
class DummyNode:
|
2014-11-16 09:56:42 +08:00
|
|
|
def as_sql(self, compiler, connection):
|
2012-05-26 10:55:33 +08:00
|
|
|
return 'dummy', []
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class MockCompiler:
|
2014-01-18 17:09:43 +08:00
|
|
|
def compile(self, node):
|
|
|
|
return node.as_sql(self, connection)
|
|
|
|
|
|
|
|
def __call__(self, name):
|
|
|
|
return connection.ops.quote_name(name)
|
|
|
|
|
2012-05-26 10:55:33 +08:00
|
|
|
def test_empty_full_handling_conjunction(self):
|
2014-11-16 09:56:42 +08:00
|
|
|
compiler = WhereNodeTest.MockCompiler()
|
2012-05-26 10:55:33 +08:00
|
|
|
w = WhereNode(children=[NothingNode()])
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(EmptyResultSet):
|
|
|
|
w.as_sql(compiler, connection)
|
2012-05-26 10:55:33 +08:00
|
|
|
w.negate()
|
2014-11-16 09:56:42 +08:00
|
|
|
self.assertEqual(w.as_sql(compiler, connection), ('', []))
|
2012-05-26 10:55:33 +08:00
|
|
|
w = WhereNode(children=[self.DummyNode(), self.DummyNode()])
|
2014-11-16 09:56:42 +08:00
|
|
|
self.assertEqual(w.as_sql(compiler, connection), ('(dummy AND dummy)', []))
|
2012-05-26 10:55:33 +08:00
|
|
|
w.negate()
|
2014-11-16 09:56:42 +08:00
|
|
|
self.assertEqual(w.as_sql(compiler, connection), ('NOT (dummy AND dummy)', []))
|
2012-05-26 10:55:33 +08:00
|
|
|
w = WhereNode(children=[NothingNode(), self.DummyNode()])
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(EmptyResultSet):
|
|
|
|
w.as_sql(compiler, connection)
|
2012-05-26 10:55:33 +08:00
|
|
|
w.negate()
|
2014-11-16 09:56:42 +08:00
|
|
|
self.assertEqual(w.as_sql(compiler, connection), ('', []))
|
2012-05-26 10:55:33 +08:00
|
|
|
|
|
|
|
def test_empty_full_handling_disjunction(self):
|
2014-11-16 09:56:42 +08:00
|
|
|
compiler = WhereNodeTest.MockCompiler()
|
2012-05-26 10:55:33 +08:00
|
|
|
w = WhereNode(children=[NothingNode()], connector='OR')
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(EmptyResultSet):
|
|
|
|
w.as_sql(compiler, connection)
|
2012-05-26 10:55:33 +08:00
|
|
|
w.negate()
|
2014-11-16 09:56:42 +08:00
|
|
|
self.assertEqual(w.as_sql(compiler, connection), ('', []))
|
2012-05-26 10:55:33 +08:00
|
|
|
w = WhereNode(children=[self.DummyNode(), self.DummyNode()], connector='OR')
|
2014-11-16 09:56:42 +08:00
|
|
|
self.assertEqual(w.as_sql(compiler, connection), ('(dummy OR dummy)', []))
|
2012-05-26 10:55:33 +08:00
|
|
|
w.negate()
|
2014-11-16 09:56:42 +08:00
|
|
|
self.assertEqual(w.as_sql(compiler, connection), ('NOT (dummy OR dummy)', []))
|
2012-05-26 10:55:33 +08:00
|
|
|
w = WhereNode(children=[NothingNode(), self.DummyNode()], connector='OR')
|
2014-11-16 09:56:42 +08:00
|
|
|
self.assertEqual(w.as_sql(compiler, connection), ('dummy', []))
|
2012-05-26 10:55:33 +08:00
|
|
|
w.negate()
|
2014-11-16 09:56:42 +08:00
|
|
|
self.assertEqual(w.as_sql(compiler, connection), ('NOT (dummy)', []))
|
2012-05-26 10:55:33 +08:00
|
|
|
|
|
|
|
def test_empty_nodes(self):
|
2014-11-16 09:56:42 +08:00
|
|
|
compiler = WhereNodeTest.MockCompiler()
|
2012-05-26 10:55:33 +08:00
|
|
|
empty_w = WhereNode()
|
|
|
|
w = WhereNode(children=[empty_w, empty_w])
|
2015-02-04 16:21:29 +08:00
|
|
|
self.assertEqual(w.as_sql(compiler, connection), ('', []))
|
2012-05-26 10:55:33 +08:00
|
|
|
w.negate()
|
2015-02-04 16:21:29 +08:00
|
|
|
with self.assertRaises(EmptyResultSet):
|
|
|
|
w.as_sql(compiler, connection)
|
2012-05-26 10:55:33 +08:00
|
|
|
w.connector = 'OR'
|
2015-02-04 16:21:29 +08:00
|
|
|
with self.assertRaises(EmptyResultSet):
|
|
|
|
w.as_sql(compiler, connection)
|
2012-05-26 10:55:33 +08:00
|
|
|
w.negate()
|
2015-02-04 16:21:29 +08:00
|
|
|
self.assertEqual(w.as_sql(compiler, connection), ('', []))
|
2012-05-26 10:55:33 +08:00
|
|
|
w = WhereNode(children=[empty_w, NothingNode()], connector='OR')
|
2015-02-04 16:21:29 +08:00
|
|
|
self.assertEqual(w.as_sql(compiler, connection), ('', []))
|
|
|
|
w = WhereNode(children=[empty_w, NothingNode()], connector='AND')
|
|
|
|
with self.assertRaises(EmptyResultSet):
|
|
|
|
w.as_sql(compiler, connection)
|
2012-08-21 23:43:08 +08:00
|
|
|
|
2012-08-02 05:09:26 +08:00
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class QuerySetExceptionTests(SimpleTestCase):
|
2012-08-02 05:09:26 +08:00
|
|
|
def test_iter_exceptions(self):
|
|
|
|
qs = ExtraInfo.objects.only('author')
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = "'ManyToOneRel' object has no attribute 'attname'"
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
2012-08-02 05:09:26 +08:00
|
|
|
list(qs)
|
|
|
|
|
2013-05-22 13:28:17 +08:00
|
|
|
def test_invalid_qs_list(self):
|
|
|
|
# Test for #19895 - second iteration over invalid queryset
|
|
|
|
# raises errors.
|
|
|
|
qs = Article.objects.order_by('invalid_column')
|
2016-10-14 08:54:25 +08:00
|
|
|
msg = "Cannot resolve keyword 'invalid_column' into field."
|
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
list(qs)
|
2016-10-14 08:54:25 +08:00
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
list(qs)
|
2013-05-22 13:28:17 +08:00
|
|
|
|
2016-10-14 08:54:25 +08:00
|
|
|
def test_invalid_order_by(self):
|
|
|
|
msg = "Invalid order_by arguments: ['*']"
|
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
|
|
|
list(Article.objects.order_by('*'))
|
|
|
|
|
2016-10-14 20:48:50 +08:00
|
|
|
def test_invalid_queryset_model(self):
|
|
|
|
msg = 'Cannot use QuerySet for "Article": Use a QuerySet for "ExtraInfo".'
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
list(Author.objects.filter(extra=Article.objects.all()))
|
|
|
|
|
2012-08-02 05:09:26 +08:00
|
|
|
|
2012-08-21 23:43:08 +08:00
|
|
|
class NullJoinPromotionOrTest(TestCase):
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.d1 = ModelD.objects.create(name='foo')
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
d2 = ModelD.objects.create(name='bar')
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.a1 = ModelA.objects.create(name='a1', d=cls.d1)
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
c = ModelC.objects.create(name='c')
|
|
|
|
b = ModelB.objects.create(name='b', c=c)
|
2014-10-19 05:30:58 +08:00
|
|
|
cls.a2 = ModelA.objects.create(name='a2', b=b, d=d2)
|
2012-08-21 23:43:08 +08:00
|
|
|
|
|
|
|
def test_ticket_17886(self):
|
|
|
|
# The first Q-object is generating the match, the rest of the filters
|
|
|
|
# should not remove the match even if they do not match anything. The
|
|
|
|
# problem here was that b__name generates a LOUTER JOIN, then
|
|
|
|
# b__c__name generates join to c, which the ORM tried to promote but
|
|
|
|
# failed as that join isn't nullable.
|
2013-10-11 19:25:14 +08:00
|
|
|
q_obj = (
|
2013-10-23 18:09:29 +08:00
|
|
|
Q(d__name='foo') |
|
|
|
|
Q(b__name='foo') |
|
2012-08-21 23:43:08 +08:00
|
|
|
Q(b__c__name='foo')
|
|
|
|
)
|
|
|
|
qset = ModelA.objects.filter(q_obj)
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
self.assertEqual(list(qset), [self.a1])
|
2012-08-21 23:43:08 +08:00
|
|
|
# We generate one INNER JOIN to D. The join is direct and not nullable
|
|
|
|
# so we can use INNER JOIN for it. However, we can NOT use INNER JOIN
|
|
|
|
# for the b->c join, as a->b is nullable.
|
|
|
|
self.assertEqual(str(qset.query).count('INNER JOIN'), 1)
|
2012-08-25 21:33:07 +08:00
|
|
|
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
def test_isnull_filter_promotion(self):
|
|
|
|
qs = ModelA.objects.filter(Q(b__name__isnull=True))
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER'), 1)
|
|
|
|
self.assertEqual(list(qs), [self.a1])
|
|
|
|
|
|
|
|
qs = ModelA.objects.filter(~Q(b__name__isnull=True))
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
self.assertEqual(list(qs), [self.a2])
|
|
|
|
|
|
|
|
qs = ModelA.objects.filter(~~Q(b__name__isnull=True))
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER'), 1)
|
|
|
|
self.assertEqual(list(qs), [self.a1])
|
|
|
|
|
|
|
|
qs = ModelA.objects.filter(Q(b__name__isnull=False))
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
self.assertEqual(list(qs), [self.a2])
|
|
|
|
|
|
|
|
qs = ModelA.objects.filter(~Q(b__name__isnull=False))
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER'), 1)
|
|
|
|
self.assertEqual(list(qs), [self.a1])
|
|
|
|
|
|
|
|
qs = ModelA.objects.filter(~~Q(b__name__isnull=False))
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
self.assertEqual(list(qs), [self.a2])
|
|
|
|
|
2013-09-25 00:37:55 +08:00
|
|
|
def test_null_join_demotion(self):
|
|
|
|
qs = ModelA.objects.filter(Q(b__name__isnull=False) & Q(b__name__isnull=True))
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn(' INNER JOIN ', str(qs.query))
|
2013-09-25 00:37:55 +08:00
|
|
|
qs = ModelA.objects.filter(Q(b__name__isnull=True) & Q(b__name__isnull=False))
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn(' INNER JOIN ', str(qs.query))
|
2013-09-25 00:37:55 +08:00
|
|
|
qs = ModelA.objects.filter(Q(b__name__isnull=False) | Q(b__name__isnull=True))
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn(' LEFT OUTER JOIN ', str(qs.query))
|
2013-09-25 00:37:55 +08:00
|
|
|
qs = ModelA.objects.filter(Q(b__name__isnull=True) | Q(b__name__isnull=False))
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn(' LEFT OUTER JOIN ', str(qs.query))
|
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to
get complex queries to work correctly. The logic was refactored so that
it should be simpler to understand. The new logic should also produce
leaner WHERE conditions.
The changes cascade somewhat, as some other parts of Django (like
add_filter() and WhereNode) expect boolean trees in certain format or
they fail to work. So to fix the add_q() one must fix utils/tree.py,
some things in add_filter(), WhereNode and so on.
This commit also fixed add_filter to see negate clauses up the path.
A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to
.filter(~Q(reversefk__in=a_list)). The reason for this is that only
the immediate parent negate clauses were seen by add_filter, and thus a
tree like AND: (NOT AND: (AND: condition)) will not be handled
correctly, as there is one intermediary AND node in the tree. The
example tree is generated by .exclude(~Q(reversefk__in=a_list)).
Still, aggregation lost connectors in OR cases, and F() objects and
aggregates in same filter clause caused GROUP BY problems on some
databases.
Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
2012-05-25 05:27:24 +08:00
|
|
|
|
2013-11-03 02:39:56 +08:00
|
|
|
def test_ticket_21366(self):
|
|
|
|
n = Note.objects.create(note='n', misc='m')
|
|
|
|
e = ExtraInfo.objects.create(info='info', note=n)
|
|
|
|
a = Author.objects.create(name='Author1', num=1, extra=e)
|
|
|
|
Ranking.objects.create(rank=1, author=a)
|
|
|
|
r1 = Report.objects.create(name='Foo', creator=a)
|
|
|
|
r2 = Report.objects.create(name='Bar')
|
|
|
|
Report.objects.create(name='Bar', creator=a)
|
|
|
|
qs = Report.objects.filter(
|
|
|
|
Q(creator__ranking__isnull=True) |
|
|
|
|
Q(creator__ranking__rank=1, name='Foo')
|
|
|
|
)
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 2)
|
|
|
|
self.assertEqual(str(qs.query).count(' JOIN '), 2)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs.order_by('name'), [r2, r1])
|
2013-11-03 02:39:56 +08:00
|
|
|
|
2014-01-09 01:35:47 +08:00
|
|
|
def test_ticket_21748(self):
|
|
|
|
i1 = Identifier.objects.create(name='i1')
|
|
|
|
i2 = Identifier.objects.create(name='i2')
|
|
|
|
i3 = Identifier.objects.create(name='i3')
|
|
|
|
Program.objects.create(identifier=i1)
|
|
|
|
Channel.objects.create(identifier=i1)
|
|
|
|
Program.objects.create(identifier=i2)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(Identifier.objects.filter(program=None, channel=None), [i3])
|
|
|
|
self.assertSequenceEqual(Identifier.objects.exclude(program=None, channel=None).order_by('name'), [i1, i2])
|
2014-01-09 01:35:47 +08:00
|
|
|
|
|
|
|
def test_ticket_21748_double_negated_and(self):
|
|
|
|
i1 = Identifier.objects.create(name='i1')
|
|
|
|
i2 = Identifier.objects.create(name='i2')
|
|
|
|
Identifier.objects.create(name='i3')
|
|
|
|
p1 = Program.objects.create(identifier=i1)
|
|
|
|
c1 = Channel.objects.create(identifier=i1)
|
|
|
|
Program.objects.create(identifier=i2)
|
|
|
|
# Check the ~~Q() (or equivalently .exclude(~Q)) works like Q() for
|
|
|
|
# join promotion.
|
|
|
|
qs1_doubleneg = Identifier.objects.exclude(~Q(program__id=p1.id, channel__id=c1.id)).order_by('pk')
|
|
|
|
qs1_filter = Identifier.objects.filter(program__id=p1.id, channel__id=c1.id).order_by('pk')
|
|
|
|
self.assertQuerysetEqual(qs1_doubleneg, qs1_filter, lambda x: x)
|
|
|
|
self.assertEqual(str(qs1_filter.query).count('JOIN'),
|
|
|
|
str(qs1_doubleneg.query).count('JOIN'))
|
|
|
|
self.assertEqual(2, str(qs1_doubleneg.query).count('INNER JOIN'))
|
|
|
|
self.assertEqual(str(qs1_filter.query).count('INNER JOIN'),
|
|
|
|
str(qs1_doubleneg.query).count('INNER JOIN'))
|
|
|
|
|
|
|
|
def test_ticket_21748_double_negated_or(self):
|
|
|
|
i1 = Identifier.objects.create(name='i1')
|
|
|
|
i2 = Identifier.objects.create(name='i2')
|
|
|
|
Identifier.objects.create(name='i3')
|
|
|
|
p1 = Program.objects.create(identifier=i1)
|
|
|
|
c1 = Channel.objects.create(identifier=i1)
|
|
|
|
p2 = Program.objects.create(identifier=i2)
|
2015-12-03 07:55:50 +08:00
|
|
|
# Test OR + doubleneg. The expected result is that channel is LOUTER
|
2014-01-09 01:35:47 +08:00
|
|
|
# joined, program INNER joined
|
|
|
|
qs1_filter = Identifier.objects.filter(
|
2016-04-04 08:37:32 +08:00
|
|
|
Q(program__id=p2.id, channel__id=c1.id) | Q(program__id=p1.id)
|
2014-01-09 01:35:47 +08:00
|
|
|
).order_by('pk')
|
|
|
|
qs1_doubleneg = Identifier.objects.exclude(
|
2016-04-04 08:37:32 +08:00
|
|
|
~Q(Q(program__id=p2.id, channel__id=c1.id) | Q(program__id=p1.id))
|
2014-01-09 01:35:47 +08:00
|
|
|
).order_by('pk')
|
|
|
|
self.assertQuerysetEqual(qs1_doubleneg, qs1_filter, lambda x: x)
|
|
|
|
self.assertEqual(str(qs1_filter.query).count('JOIN'),
|
|
|
|
str(qs1_doubleneg.query).count('JOIN'))
|
|
|
|
self.assertEqual(1, str(qs1_doubleneg.query).count('INNER JOIN'))
|
|
|
|
self.assertEqual(str(qs1_filter.query).count('INNER JOIN'),
|
|
|
|
str(qs1_doubleneg.query).count('INNER JOIN'))
|
|
|
|
|
|
|
|
def test_ticket_21748_complex_filter(self):
|
|
|
|
i1 = Identifier.objects.create(name='i1')
|
|
|
|
i2 = Identifier.objects.create(name='i2')
|
|
|
|
Identifier.objects.create(name='i3')
|
|
|
|
p1 = Program.objects.create(identifier=i1)
|
|
|
|
c1 = Channel.objects.create(identifier=i1)
|
|
|
|
p2 = Program.objects.create(identifier=i2)
|
|
|
|
# Finally, a more complex case, one time in a way where each
|
|
|
|
# NOT is pushed to lowest level in the boolean tree, and
|
|
|
|
# another query where this isn't done.
|
|
|
|
qs1 = Identifier.objects.filter(
|
2016-04-04 08:37:32 +08:00
|
|
|
~Q(~Q(program__id=p2.id, channel__id=c1.id) & Q(program__id=p1.id))
|
|
|
|
).order_by('pk')
|
2014-01-09 01:35:47 +08:00
|
|
|
qs2 = Identifier.objects.filter(
|
2016-04-04 08:37:32 +08:00
|
|
|
Q(Q(program__id=p2.id, channel__id=c1.id) | ~Q(program__id=p1.id))
|
|
|
|
).order_by('pk')
|
2014-01-09 01:35:47 +08:00
|
|
|
self.assertQuerysetEqual(qs1, qs2, lambda x: x)
|
|
|
|
self.assertEqual(str(qs1.query).count('JOIN'),
|
|
|
|
str(qs2.query).count('JOIN'))
|
|
|
|
self.assertEqual(0, str(qs1.query).count('INNER JOIN'))
|
|
|
|
self.assertEqual(str(qs1.query).count('INNER JOIN'),
|
|
|
|
str(qs2.query).count('INNER JOIN'))
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-08-25 21:33:07 +08:00
|
|
|
class ReverseJoinTrimmingTest(TestCase):
|
|
|
|
def test_reverse_trimming(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
# We don't accidentally trim reverse joins - we can't know if there is
|
|
|
|
# anything on the other side of the join, so trimming reverse joins
|
|
|
|
# can't be done, ever.
|
2012-08-25 21:33:07 +08:00
|
|
|
t = Tag.objects.create()
|
|
|
|
qs = Tag.objects.filter(annotation__tag=t.pk)
|
|
|
|
self.assertIn('INNER JOIN', str(qs.query))
|
2013-01-09 02:07:12 +08:00
|
|
|
self.assertEqual(list(qs), [])
|
2012-12-21 03:25:48 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-12-21 03:25:48 +08:00
|
|
|
class JoinReuseTest(TestCase):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The queries reuse joins sensibly (for example, direct joins
|
2012-12-21 03:25:48 +08:00
|
|
|
are always reused).
|
|
|
|
"""
|
|
|
|
def test_fk_reuse(self):
|
|
|
|
qs = Annotation.objects.filter(tag__name='foo').filter(tag__name='bar')
|
|
|
|
self.assertEqual(str(qs.query).count('JOIN'), 1)
|
|
|
|
|
|
|
|
def test_fk_reuse_select_related(self):
|
|
|
|
qs = Annotation.objects.filter(tag__name='foo').select_related('tag')
|
|
|
|
self.assertEqual(str(qs.query).count('JOIN'), 1)
|
|
|
|
|
|
|
|
def test_fk_reuse_annotation(self):
|
|
|
|
qs = Annotation.objects.filter(tag__name='foo').annotate(cnt=Count('tag__name'))
|
|
|
|
self.assertEqual(str(qs.query).count('JOIN'), 1)
|
|
|
|
|
|
|
|
def test_fk_reuse_disjunction(self):
|
|
|
|
qs = Annotation.objects.filter(Q(tag__name='foo') | Q(tag__name='bar'))
|
|
|
|
self.assertEqual(str(qs.query).count('JOIN'), 1)
|
|
|
|
|
|
|
|
def test_fk_reuse_order_by(self):
|
|
|
|
qs = Annotation.objects.filter(tag__name='foo').order_by('tag__name')
|
|
|
|
self.assertEqual(str(qs.query).count('JOIN'), 1)
|
|
|
|
|
|
|
|
def test_revo2o_reuse(self):
|
|
|
|
qs = Detail.objects.filter(member__name='foo').filter(member__name='foo')
|
|
|
|
self.assertEqual(str(qs.query).count('JOIN'), 1)
|
|
|
|
|
|
|
|
def test_revfk_noreuse(self):
|
|
|
|
qs = Author.objects.filter(report__name='r4').filter(report__name='r1')
|
|
|
|
self.assertEqual(str(qs.query).count('JOIN'), 2)
|
2012-08-25 19:13:37 +08:00
|
|
|
|
2016-10-27 06:01:37 +08:00
|
|
|
def test_inverted_q_across_relations(self):
|
|
|
|
"""
|
|
|
|
When a trimmable join is specified in the query (here school__), the
|
|
|
|
ORM detects it and removes unnecessary joins. The set of reusable joins
|
|
|
|
are updated after trimming the query so that other lookups don't
|
|
|
|
consider that the outer query's filters are in effect for the subquery
|
|
|
|
(#26551).
|
|
|
|
"""
|
|
|
|
springfield_elementary = School.objects.create()
|
|
|
|
hogward = School.objects.create()
|
|
|
|
Student.objects.create(school=springfield_elementary)
|
|
|
|
hp = Student.objects.create(school=hogward)
|
|
|
|
Classroom.objects.create(school=hogward, name='Potion')
|
|
|
|
Classroom.objects.create(school=springfield_elementary, name='Main')
|
|
|
|
qs = Student.objects.filter(
|
|
|
|
~(Q(school__classroom__name='Main') & Q(school__classroom__has_blackboard=None))
|
|
|
|
)
|
|
|
|
self.assertSequenceEqual(qs, [hp])
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2012-08-25 19:13:37 +08:00
|
|
|
class DisjunctionPromotionTests(TestCase):
|
2015-12-03 07:55:50 +08:00
|
|
|
def test_disjunction_promotion_select_related(self):
|
2013-06-10 23:22:30 +08:00
|
|
|
fk1 = FK1.objects.create(f1='f1', f2='f2')
|
|
|
|
basea = BaseA.objects.create(a=fk1)
|
|
|
|
qs = BaseA.objects.filter(Q(a=fk1) | Q(b=2))
|
|
|
|
self.assertEqual(str(qs.query).count(' JOIN '), 0)
|
|
|
|
qs = qs.select_related('a', 'b')
|
|
|
|
self.assertEqual(str(qs.query).count(' INNER JOIN '), 0)
|
|
|
|
self.assertEqual(str(qs.query).count(' LEFT OUTER JOIN '), 2)
|
|
|
|
with self.assertNumQueries(1):
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [basea])
|
2013-06-10 23:22:30 +08:00
|
|
|
self.assertEqual(qs[0].a, fk1)
|
|
|
|
self.assertIs(qs[0].b, None)
|
|
|
|
|
2012-08-25 19:13:37 +08:00
|
|
|
def test_disjunction_promotion1(self):
|
|
|
|
# Pre-existing join, add two ORed filters to the same join,
|
|
|
|
# all joins can be INNER JOINS.
|
|
|
|
qs = BaseA.objects.filter(a__f1='foo')
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
qs = qs.filter(Q(b__f1='foo') | Q(b__f2='foo'))
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 2)
|
|
|
|
# Reverse the order of AND and OR filters.
|
|
|
|
qs = BaseA.objects.filter(Q(b__f1='foo') | Q(b__f2='foo'))
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
qs = qs.filter(a__f1='foo')
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 2)
|
|
|
|
|
|
|
|
def test_disjunction_promotion2(self):
|
|
|
|
qs = BaseA.objects.filter(a__f1='foo')
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
# Now we have two different joins in an ORed condition, these
|
|
|
|
# must be OUTER joins. The pre-existing join should remain INNER.
|
|
|
|
qs = qs.filter(Q(b__f1='foo') | Q(c__f2='foo'))
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 2)
|
|
|
|
# Reverse case.
|
|
|
|
qs = BaseA.objects.filter(Q(b__f1='foo') | Q(c__f2='foo'))
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 2)
|
|
|
|
qs = qs.filter(a__f1='foo')
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 2)
|
|
|
|
|
|
|
|
def test_disjunction_promotion3(self):
|
|
|
|
qs = BaseA.objects.filter(a__f2='bar')
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
# The ANDed a__f2 filter allows us to use keep using INNER JOIN
|
|
|
|
# even inside the ORed case. If the join to a__ returns nothing,
|
|
|
|
# the ANDed filter for a__f2 can't be true.
|
|
|
|
qs = qs.filter(Q(a__f1='foo') | Q(b__f2='foo'))
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 1)
|
|
|
|
|
2013-09-25 00:37:55 +08:00
|
|
|
def test_disjunction_promotion3_demote(self):
|
|
|
|
# This one needs demotion logic: the first filter causes a to be
|
|
|
|
# outer joined, the second filter makes it inner join again.
|
2012-08-25 19:13:37 +08:00
|
|
|
qs = BaseA.objects.filter(
|
|
|
|
Q(a__f1='foo') | Q(b__f2='foo')).filter(a__f2='bar')
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 1)
|
|
|
|
|
2013-09-25 00:37:55 +08:00
|
|
|
def test_disjunction_promotion4_demote(self):
|
2012-08-25 19:13:37 +08:00
|
|
|
qs = BaseA.objects.filter(Q(a=1) | Q(a=2))
|
|
|
|
self.assertEqual(str(qs.query).count('JOIN'), 0)
|
2013-09-25 00:37:55 +08:00
|
|
|
# Demote needed for the "a" join. It is marked as outer join by
|
|
|
|
# above filter (even if it is trimmed away).
|
2012-08-25 19:13:37 +08:00
|
|
|
qs = qs.filter(a__f1='foo')
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
2013-06-10 23:22:30 +08:00
|
|
|
|
|
|
|
def test_disjunction_promotion4(self):
|
2012-08-25 19:13:37 +08:00
|
|
|
qs = BaseA.objects.filter(a__f1='foo')
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
qs = qs.filter(Q(a=1) | Q(a=2))
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
|
2013-09-25 00:37:55 +08:00
|
|
|
def test_disjunction_promotion5_demote(self):
|
2012-08-25 19:13:37 +08:00
|
|
|
qs = BaseA.objects.filter(Q(a=1) | Q(a=2))
|
|
|
|
# Note that the above filters on a force the join to an
|
|
|
|
# inner join even if it is trimmed.
|
|
|
|
self.assertEqual(str(qs.query).count('JOIN'), 0)
|
|
|
|
qs = qs.filter(Q(a__f1='foo') | Q(b__f1='foo'))
|
|
|
|
# So, now the a__f1 join doesn't need promotion.
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
2013-11-03 02:39:56 +08:00
|
|
|
# But b__f1 does.
|
2012-08-25 19:13:37 +08:00
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 1)
|
|
|
|
qs = BaseA.objects.filter(Q(a__f1='foo') | Q(b__f1='foo'))
|
|
|
|
# Now the join to a is created as LOUTER
|
2013-09-25 00:37:55 +08:00
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 2)
|
|
|
|
qs = qs.filter(Q(a=1) | Q(a=2))
|
2012-08-25 19:13:37 +08:00
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 1)
|
|
|
|
|
|
|
|
def test_disjunction_promotion6(self):
|
|
|
|
qs = BaseA.objects.filter(Q(a=1) | Q(a=2))
|
|
|
|
self.assertEqual(str(qs.query).count('JOIN'), 0)
|
|
|
|
qs = BaseA.objects.filter(Q(a__f1='foo') & Q(b__f1='foo'))
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 2)
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
|
|
|
|
qs = BaseA.objects.filter(Q(a__f1='foo') & Q(b__f1='foo'))
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 2)
|
|
|
|
qs = qs.filter(Q(a=1) | Q(a=2))
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 2)
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 0)
|
|
|
|
|
|
|
|
def test_disjunction_promotion7(self):
|
|
|
|
qs = BaseA.objects.filter(Q(a=1) | Q(a=2))
|
|
|
|
self.assertEqual(str(qs.query).count('JOIN'), 0)
|
|
|
|
qs = BaseA.objects.filter(Q(a__f1='foo') | (Q(b__f1='foo') & Q(a__f1='bar')))
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 1)
|
|
|
|
qs = BaseA.objects.filter(
|
|
|
|
(Q(a__f1='foo') | Q(b__f1='foo')) & (Q(a__f1='bar') | Q(c__f1='foo'))
|
|
|
|
)
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 3)
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 0)
|
|
|
|
qs = BaseA.objects.filter(
|
|
|
|
(Q(a__f1='foo') | (Q(a__f1='bar')) & (Q(b__f1='bar') | Q(c__f1='foo')))
|
|
|
|
)
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 2)
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
|
|
|
|
def test_disjunction_promotion_fexpression(self):
|
|
|
|
qs = BaseA.objects.filter(Q(a__f1=F('b__f1')) | Q(b__f1='foo'))
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 1)
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 1)
|
|
|
|
qs = BaseA.objects.filter(Q(a__f1=F('c__f1')) | Q(b__f1='foo'))
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 3)
|
|
|
|
qs = BaseA.objects.filter(Q(a__f1=F('b__f1')) | Q(a__f2=F('b__f2')) | Q(c__f1='foo'))
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 3)
|
|
|
|
qs = BaseA.objects.filter(Q(a__f1=F('c__f1')) | (Q(pk=1) & Q(pk=2)))
|
|
|
|
self.assertEqual(str(qs.query).count('LEFT OUTER JOIN'), 2)
|
|
|
|
self.assertEqual(str(qs.query).count('INNER JOIN'), 0)
|
2013-02-18 07:56:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ManyToManyExcludeTest(TestCase):
|
|
|
|
def test_exclude_many_to_many(self):
|
|
|
|
Identifier.objects.create(name='extra')
|
|
|
|
program = Program.objects.create(identifier=Identifier.objects.create(name='program'))
|
|
|
|
channel = Channel.objects.create(identifier=Identifier.objects.create(name='channel'))
|
|
|
|
channel.programs.add(program)
|
|
|
|
|
|
|
|
# channel contains 'program1', so all Identifiers except that one
|
|
|
|
# should be returned
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Identifier.objects.exclude(program__channel=channel).order_by('name'),
|
|
|
|
['<Identifier: channel>', '<Identifier: extra>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Identifier.objects.exclude(program__channel=None).order_by('name'),
|
|
|
|
['<Identifier: program>']
|
|
|
|
)
|
2013-02-28 21:04:12 +08:00
|
|
|
|
|
|
|
def test_ticket_12823(self):
|
|
|
|
pg3 = Page.objects.create(text='pg3')
|
|
|
|
pg2 = Page.objects.create(text='pg2')
|
|
|
|
pg1 = Page.objects.create(text='pg1')
|
|
|
|
pa1 = Paragraph.objects.create(text='pa1')
|
2015-10-09 05:17:10 +08:00
|
|
|
pa1.page.set([pg1, pg2])
|
2013-02-28 21:04:12 +08:00
|
|
|
pa2 = Paragraph.objects.create(text='pa2')
|
2015-10-09 05:17:10 +08:00
|
|
|
pa2.page.set([pg2, pg3])
|
2013-02-28 21:04:12 +08:00
|
|
|
pa3 = Paragraph.objects.create(text='pa3')
|
|
|
|
ch1 = Chapter.objects.create(title='ch1', paragraph=pa1)
|
|
|
|
ch2 = Chapter.objects.create(title='ch2', paragraph=pa2)
|
|
|
|
ch3 = Chapter.objects.create(title='ch3', paragraph=pa3)
|
|
|
|
b1 = Book.objects.create(title='b1', chapter=ch1)
|
|
|
|
b2 = Book.objects.create(title='b2', chapter=ch2)
|
|
|
|
b3 = Book.objects.create(title='b3', chapter=ch3)
|
|
|
|
q = Book.objects.exclude(chapter__paragraph__page__text='pg1')
|
|
|
|
self.assertNotIn('IS NOT NULL', str(q.query))
|
|
|
|
self.assertEqual(len(q), 2)
|
|
|
|
self.assertNotIn(b1, q)
|
|
|
|
self.assertIn(b2, q)
|
|
|
|
self.assertIn(b3, q)
|
2013-03-02 07:06:56 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-03-02 07:06:56 +08:00
|
|
|
class RelabelCloneTest(TestCase):
|
|
|
|
def test_ticket_19964(self):
|
|
|
|
my1 = MyObject.objects.create(data='foo')
|
|
|
|
my1.parent = my1
|
|
|
|
my1.save()
|
|
|
|
my2 = MyObject.objects.create(data='bar', parent=my1)
|
|
|
|
parents = MyObject.objects.filter(parent=F('id'))
|
|
|
|
children = MyObject.objects.filter(parent__in=parents).exclude(parent=F('id'))
|
|
|
|
self.assertEqual(list(parents), [my1])
|
|
|
|
# Evaluating the children query (which has parents as part of it) does
|
|
|
|
# not change results for the parents query.
|
|
|
|
self.assertEqual(list(children), [my2])
|
|
|
|
self.assertEqual(list(parents), [my1])
|
2013-03-21 06:38:23 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-03-21 06:38:23 +08:00
|
|
|
class Ticket20101Tests(TestCase):
|
|
|
|
def test_ticket_20101(self):
|
|
|
|
"""
|
|
|
|
Tests QuerySet ORed combining in exclude subquery case.
|
|
|
|
"""
|
|
|
|
t = Tag.objects.create(name='foo')
|
|
|
|
a1 = Annotation.objects.create(tag=t, name='a1')
|
|
|
|
a2 = Annotation.objects.create(tag=t, name='a2')
|
|
|
|
a3 = Annotation.objects.create(tag=t, name='a3')
|
|
|
|
n = Note.objects.create(note='foo', misc='bar')
|
|
|
|
qs1 = Note.objects.exclude(annotation__in=[a1, a2])
|
|
|
|
qs2 = Note.objects.filter(annotation__in=[a3])
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn(n, qs1)
|
|
|
|
self.assertNotIn(n, qs2)
|
|
|
|
self.assertIn(n, (qs1 | qs2))
|
2013-03-25 04:59:44 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class EmptyStringPromotionTests(SimpleTestCase):
|
2013-03-25 04:59:44 +08:00
|
|
|
def test_empty_string_promotion(self):
|
|
|
|
qs = RelatedObject.objects.filter(single__name='')
|
|
|
|
if connection.features.interprets_empty_strings_as_nulls:
|
|
|
|
self.assertIn('LEFT OUTER JOIN', str(qs.query))
|
|
|
|
else:
|
|
|
|
self.assertNotIn('LEFT OUTER JOIN', str(qs.query))
|
2013-05-20 19:24:48 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-05-20 19:24:48 +08:00
|
|
|
class ValuesSubqueryTests(TestCase):
|
|
|
|
def test_values_in_subquery(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
# If a values() queryset is used, then the given values
|
2013-05-20 19:24:48 +08:00
|
|
|
# will be used instead of forcing use of the relation's field.
|
|
|
|
o1 = Order.objects.create(id=-2)
|
|
|
|
o2 = Order.objects.create(id=-1)
|
|
|
|
oi1 = OrderItem.objects.create(order=o1, status=0)
|
|
|
|
oi1.status = oi1.pk
|
|
|
|
oi1.save()
|
|
|
|
OrderItem.objects.create(order=o2, status=0)
|
|
|
|
|
|
|
|
# The query below should match o1 as it has related order_item
|
|
|
|
# with id == status.
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(Order.objects.filter(items__in=OrderItem.objects.values_list('status')), [o1])
|
2013-05-27 16:29:43 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-05-27 16:29:43 +08:00
|
|
|
class DoubleInSubqueryTests(TestCase):
|
|
|
|
def test_double_subquery_in(self):
|
|
|
|
lfa1 = LeafA.objects.create(data='foo')
|
|
|
|
lfa2 = LeafA.objects.create(data='bar')
|
|
|
|
lfb1 = LeafB.objects.create(data='lfb1')
|
|
|
|
lfb2 = LeafB.objects.create(data='lfb2')
|
|
|
|
Join.objects.create(a=lfa1, b=lfb1)
|
|
|
|
Join.objects.create(a=lfa2, b=lfb2)
|
|
|
|
leaf_as = LeafA.objects.filter(data='foo').values_list('pk', flat=True)
|
|
|
|
joins = Join.objects.filter(a__in=leaf_as).values_list('b__id', flat=True)
|
|
|
|
qs = LeafB.objects.filter(pk__in=joins)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [lfb1])
|
2013-06-01 05:26:16 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class Ticket18785Tests(SimpleTestCase):
|
2013-06-01 05:26:16 +08:00
|
|
|
def test_ticket_18785(self):
|
|
|
|
# Test join trimming from ticket18785
|
|
|
|
qs = Item.objects.exclude(
|
|
|
|
note__isnull=False
|
|
|
|
).filter(
|
|
|
|
name='something', creator__extra__isnull=True
|
|
|
|
).order_by()
|
2013-06-01 18:31:25 +08:00
|
|
|
self.assertEqual(1, str(qs.query).count('INNER JOIN'))
|
|
|
|
self.assertEqual(0, str(qs.query).count('OUTER JOIN'))
|
2013-07-23 18:40:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Ticket20788Tests(TestCase):
|
|
|
|
def test_ticket_20788(self):
|
|
|
|
Paragraph.objects.create()
|
|
|
|
paragraph = Paragraph.objects.create()
|
|
|
|
page = paragraph.page.create()
|
|
|
|
chapter = Chapter.objects.create(paragraph=paragraph)
|
|
|
|
Book.objects.create(chapter=chapter)
|
|
|
|
|
|
|
|
paragraph2 = Paragraph.objects.create()
|
|
|
|
Page.objects.create()
|
|
|
|
chapter2 = Chapter.objects.create(paragraph=paragraph2)
|
|
|
|
book2 = Book.objects.create(chapter=chapter2)
|
|
|
|
|
2016-09-10 17:36:27 +08:00
|
|
|
sentences_not_in_pub = Book.objects.exclude(chapter__paragraph__page=page)
|
|
|
|
self.assertSequenceEqual(sentences_not_in_pub, [book2])
|
2013-07-26 18:02:32 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-08-19 21:24:45 +08:00
|
|
|
class Ticket12807Tests(TestCase):
|
|
|
|
def test_ticket_12807(self):
|
|
|
|
p1 = Paragraph.objects.create()
|
|
|
|
p2 = Paragraph.objects.create()
|
|
|
|
# The ORed condition below should have no effect on the query - the
|
|
|
|
# ~Q(pk__in=[]) will always be True.
|
|
|
|
qs = Paragraph.objects.filter((Q(pk=p2.pk) | ~Q(pk__in=[])) & Q(pk=p1.pk))
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [p1])
|
2013-08-19 21:24:45 +08:00
|
|
|
|
|
|
|
|
2013-07-26 18:02:32 +08:00
|
|
|
class RelatedLookupTypeTests(TestCase):
|
2014-06-14 15:24:19 +08:00
|
|
|
error = 'Cannot query "%s": Must be "%s" instance.'
|
|
|
|
|
2014-10-19 05:30:58 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.oa = ObjectA.objects.create(name="oa")
|
|
|
|
cls.poa = ProxyObjectA.objects.get(name="oa")
|
|
|
|
cls.coa = ChildObjectA.objects.create(name="coa")
|
|
|
|
cls.wrong_type = Order.objects.create(id=cls.oa.pk)
|
|
|
|
cls.ob = ObjectB.objects.create(name="ob", objecta=cls.oa, num=1)
|
|
|
|
ProxyObjectB.objects.create(name="pob", objecta=cls.oa, num=2)
|
|
|
|
cls.pob = ProxyObjectB.objects.all()
|
|
|
|
ObjectC.objects.create(childobjecta=cls.coa)
|
2014-06-14 15:24:19 +08:00
|
|
|
|
2013-07-26 18:02:32 +08:00
|
|
|
def test_wrong_type_lookup(self):
|
2014-06-14 15:24:19 +08:00
|
|
|
"""
|
|
|
|
A ValueError is raised when the incorrect object type is passed to a
|
|
|
|
query lookup.
|
|
|
|
"""
|
|
|
|
# Passing incorrect object type
|
2016-02-04 19:10:50 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, self.error % (self.wrong_type, ObjectA._meta.object_name)):
|
2014-06-14 15:24:19 +08:00
|
|
|
ObjectB.objects.get(objecta=self.wrong_type)
|
|
|
|
|
2016-02-04 19:10:50 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, self.error % (self.wrong_type, ObjectA._meta.object_name)):
|
2014-06-14 15:24:19 +08:00
|
|
|
ObjectB.objects.filter(objecta__in=[self.wrong_type])
|
|
|
|
|
2016-02-04 19:10:50 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, self.error % (self.wrong_type, ObjectA._meta.object_name)):
|
2014-06-14 15:24:19 +08:00
|
|
|
ObjectB.objects.filter(objecta=self.wrong_type)
|
|
|
|
|
2016-02-04 19:10:50 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, self.error % (self.wrong_type, ObjectB._meta.object_name)):
|
2014-06-14 15:24:19 +08:00
|
|
|
ObjectA.objects.filter(objectb__in=[self.wrong_type, self.ob])
|
|
|
|
|
|
|
|
# Passing an object of the class on which query is done.
|
2016-02-04 19:10:50 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, self.error % (self.ob, ObjectA._meta.object_name)):
|
2014-06-14 15:24:19 +08:00
|
|
|
ObjectB.objects.filter(objecta__in=[self.poa, self.ob])
|
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, self.error % (self.ob, ChildObjectA._meta.object_name)):
|
2014-06-14 15:24:19 +08:00
|
|
|
ObjectC.objects.exclude(childobjecta__in=[self.coa, self.ob])
|
|
|
|
|
|
|
|
def test_wrong_backward_lookup(self):
|
|
|
|
"""
|
|
|
|
A ValueError is raised when the incorrect object type is passed to a
|
|
|
|
query lookup for backward relations.
|
|
|
|
"""
|
2016-02-04 19:10:50 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, self.error % (self.oa, ObjectB._meta.object_name)):
|
2014-06-14 15:24:19 +08:00
|
|
|
ObjectA.objects.filter(objectb__in=[self.oa, self.ob])
|
|
|
|
|
2016-02-04 19:10:50 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, self.error % (self.oa, ObjectB._meta.object_name)):
|
2014-06-14 15:24:19 +08:00
|
|
|
ObjectA.objects.exclude(objectb=self.oa)
|
|
|
|
|
2016-02-04 19:10:50 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, self.error % (self.wrong_type, ObjectB._meta.object_name)):
|
2014-06-14 15:24:19 +08:00
|
|
|
ObjectA.objects.get(objectb=self.wrong_type)
|
|
|
|
|
|
|
|
def test_correct_lookup(self):
|
|
|
|
"""
|
|
|
|
When passing proxy model objects, child objects, or parent objects,
|
|
|
|
lookups work fine.
|
|
|
|
"""
|
2017-12-29 04:07:29 +08:00
|
|
|
out_a = ['<ObjectA: oa>']
|
2014-06-14 15:24:19 +08:00
|
|
|
out_b = ['<ObjectB: ob>', '<ObjectB: pob>']
|
|
|
|
out_c = ['<ObjectC: >']
|
|
|
|
|
|
|
|
# proxy model objects
|
|
|
|
self.assertQuerysetEqual(ObjectB.objects.filter(objecta=self.poa).order_by('name'), out_b)
|
|
|
|
self.assertQuerysetEqual(ObjectA.objects.filter(objectb__in=self.pob).order_by('pk'), out_a * 2)
|
|
|
|
|
|
|
|
# child objects
|
|
|
|
self.assertQuerysetEqual(ObjectB.objects.filter(objecta__in=[self.coa]), [])
|
|
|
|
self.assertQuerysetEqual(ObjectB.objects.filter(objecta__in=[self.poa, self.coa]).order_by('name'), out_b)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
ObjectB.objects.filter(objecta__in=iter([self.poa, self.coa])).order_by('name'),
|
|
|
|
out_b
|
|
|
|
)
|
2014-06-14 15:24:19 +08:00
|
|
|
|
|
|
|
# parent objects
|
|
|
|
self.assertQuerysetEqual(ObjectC.objects.exclude(childobjecta=self.oa), out_c)
|
2013-08-20 15:32:18 +08:00
|
|
|
|
2015-08-18 05:36:08 +08:00
|
|
|
# QuerySet related object type checking shouldn't issue queries
|
|
|
|
# (the querysets aren't evaluated here, hence zero queries) (#23266).
|
2014-08-10 20:53:37 +08:00
|
|
|
with self.assertNumQueries(0):
|
|
|
|
ObjectB.objects.filter(objecta__in=ObjectA.objects.all())
|
|
|
|
|
2014-09-02 21:30:53 +08:00
|
|
|
def test_values_queryset_lookup(self):
|
|
|
|
"""
|
|
|
|
#23396 - Ensure ValueQuerySets are not checked for compatibility with the lookup field
|
|
|
|
"""
|
2015-07-20 18:35:24 +08:00
|
|
|
# Make sure the num and objecta field values match.
|
|
|
|
ob = ObjectB.objects.get(name='ob')
|
|
|
|
ob.num = ob.objecta.pk
|
|
|
|
ob.save()
|
|
|
|
pob = ObjectB.objects.get(name='pob')
|
|
|
|
pob.num = pob.objecta.pk
|
|
|
|
pob.save()
|
2014-09-02 21:30:53 +08:00
|
|
|
self.assertQuerysetEqual(ObjectB.objects.filter(
|
2015-07-20 18:35:24 +08:00
|
|
|
objecta__in=ObjectB.objects.all().values_list('num')
|
2014-09-02 21:30:53 +08:00
|
|
|
).order_by('pk'), ['<ObjectB: ob>', '<ObjectB: pob>'])
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-08-20 15:32:18 +08:00
|
|
|
class Ticket14056Tests(TestCase):
|
|
|
|
def test_ticket_14056(self):
|
|
|
|
s1 = SharedConnection.objects.create(data='s1')
|
|
|
|
s2 = SharedConnection.objects.create(data='s2')
|
|
|
|
s3 = SharedConnection.objects.create(data='s3')
|
|
|
|
PointerA.objects.create(connection=s2)
|
2013-08-20 16:33:44 +08:00
|
|
|
expected_ordering = (
|
|
|
|
[s1, s3, s2] if connection.features.nulls_order_largest
|
|
|
|
else [s2, s1, s3]
|
|
|
|
)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(SharedConnection.objects.order_by('-pointera__connection', 'pk'), expected_ordering)
|
2013-08-22 15:42:10 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-08-22 15:42:10 +08:00
|
|
|
class Ticket20955Tests(TestCase):
|
|
|
|
def test_ticket_20955(self):
|
|
|
|
jack = Staff.objects.create(name='jackstaff')
|
|
|
|
jackstaff = StaffUser.objects.create(staff=jack)
|
|
|
|
jill = Staff.objects.create(name='jillstaff')
|
|
|
|
jillstaff = StaffUser.objects.create(staff=jill)
|
|
|
|
task = Task.objects.create(creator=jackstaff, owner=jillstaff, title="task")
|
|
|
|
task_get = Task.objects.get(pk=task.pk)
|
|
|
|
# Load data so that assertNumQueries doesn't complain about the get
|
|
|
|
# version's queries.
|
|
|
|
task_get.creator.staffuser.staff
|
|
|
|
task_get.owner.staffuser.staff
|
2014-12-01 15:28:01 +08:00
|
|
|
qs = Task.objects.select_related(
|
|
|
|
'creator__staffuser__staff', 'owner__staffuser__staff')
|
|
|
|
self.assertEqual(str(qs.query).count(' JOIN '), 6)
|
|
|
|
task_select_related = qs.get(pk=task.pk)
|
2013-08-22 15:42:10 +08:00
|
|
|
with self.assertNumQueries(0):
|
|
|
|
self.assertEqual(task_select_related.creator.staffuser.staff,
|
|
|
|
task_get.creator.staffuser.staff)
|
|
|
|
self.assertEqual(task_select_related.owner.staffuser.staff,
|
|
|
|
task_get.owner.staffuser.staff)
|
2013-10-01 16:07:41 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-10-01 16:07:41 +08:00
|
|
|
class Ticket21203Tests(TestCase):
|
|
|
|
def test_ticket_21203(self):
|
|
|
|
p = Ticket21203Parent.objects.create(parent_bool=True)
|
|
|
|
c = Ticket21203Child.objects.create(parent=p)
|
|
|
|
qs = Ticket21203Child.objects.select_related('parent').defer('parent__created')
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs, [c])
|
2013-10-01 16:07:41 +08:00
|
|
|
self.assertIs(qs[0].parent.parent_bool, True)
|
2013-09-25 00:37:55 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-09-25 00:37:55 +08:00
|
|
|
class ValuesJoinPromotionTests(TestCase):
|
|
|
|
def test_values_no_promotion_for_existing(self):
|
|
|
|
qs = Node.objects.filter(parent__parent__isnull=False)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn(' INNER JOIN ', str(qs.query))
|
2013-09-25 00:37:55 +08:00
|
|
|
qs = qs.values('parent__parent__id')
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn(' INNER JOIN ', str(qs.query))
|
2013-09-25 00:37:55 +08:00
|
|
|
# Make sure there is a left outer join without the filter.
|
|
|
|
qs = Node.objects.values('parent__parent__id')
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn(' LEFT OUTER JOIN ', str(qs.query))
|
2013-09-25 00:37:55 +08:00
|
|
|
|
|
|
|
def test_non_nullable_fk_not_promoted(self):
|
|
|
|
qs = ObjectB.objects.values('objecta__name')
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn(' INNER JOIN ', str(qs.query))
|
2013-11-05 00:04:57 +08:00
|
|
|
|
|
|
|
def test_ticket_21376(self):
|
|
|
|
a = ObjectA.objects.create()
|
|
|
|
ObjectC.objects.create(objecta=a)
|
|
|
|
qs = ObjectC.objects.filter(
|
|
|
|
Q(objecta=a) | Q(objectb__objecta=a),
|
|
|
|
)
|
|
|
|
qs = qs.filter(
|
|
|
|
Q(objectb=1) | Q(objecta=a),
|
|
|
|
)
|
|
|
|
self.assertEqual(qs.count(), 1)
|
|
|
|
tblname = connection.ops.quote_name(ObjectB._meta.db_table)
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIn(' LEFT OUTER JOIN %s' % tblname, str(qs.query))
|
2014-01-21 00:28:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ForeignKeyToBaseExcludeTests(TestCase):
|
|
|
|
def test_ticket_21787(self):
|
|
|
|
sc1 = SpecialCategory.objects.create(special_name='sc1', name='sc1')
|
|
|
|
sc2 = SpecialCategory.objects.create(special_name='sc2', name='sc2')
|
|
|
|
sc3 = SpecialCategory.objects.create(special_name='sc3', name='sc3')
|
|
|
|
c1 = CategoryItem.objects.create(category=sc1)
|
|
|
|
CategoryItem.objects.create(category=sc2)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(SpecialCategory.objects.exclude(categoryitem__id=c1.pk).order_by('name'), [sc2, sc3])
|
|
|
|
self.assertSequenceEqual(SpecialCategory.objects.filter(categoryitem__id=c1.pk), [sc1])
|
2014-01-29 03:36:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ReverseM2MCustomPkTests(TestCase):
|
|
|
|
def test_ticket_21879(self):
|
|
|
|
cpt1 = CustomPkTag.objects.create(id='cpt1', tag='cpt1')
|
|
|
|
cp1 = CustomPk.objects.create(name='cp1', extra='extra')
|
|
|
|
cp1.custompktag_set.add(cpt1)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(CustomPk.objects.filter(custompktag=cpt1), [cp1])
|
|
|
|
self.assertSequenceEqual(CustomPkTag.objects.filter(custom_pk=cp1), [cpt1])
|
2014-04-28 20:27:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Ticket22429Tests(TestCase):
|
|
|
|
def test_ticket_22429(self):
|
|
|
|
sc1 = School.objects.create()
|
|
|
|
st1 = Student.objects.create(school=sc1)
|
|
|
|
|
|
|
|
sc2 = School.objects.create()
|
|
|
|
st2 = Student.objects.create(school=sc2)
|
|
|
|
|
|
|
|
cr = Classroom.objects.create(school=sc1)
|
|
|
|
cr.students.add(st1)
|
|
|
|
|
|
|
|
queryset = Student.objects.filter(~Q(classroom__school=F('school')))
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(queryset, [st2])
|
2014-10-07 21:07:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Ticket23605Tests(TestCase):
|
|
|
|
def test_ticket_23605(self):
|
|
|
|
# Test filtering on a complicated q-object from ticket's report.
|
|
|
|
# The query structure is such that we have multiple nested subqueries.
|
|
|
|
# The original problem was that the inner queries weren't relabeled
|
|
|
|
# correctly.
|
2015-07-13 15:54:39 +08:00
|
|
|
# See also #24090.
|
2014-10-07 21:07:46 +08:00
|
|
|
a1 = Ticket23605A.objects.create()
|
|
|
|
a2 = Ticket23605A.objects.create()
|
|
|
|
c1 = Ticket23605C.objects.create(field_c0=10000.0)
|
|
|
|
Ticket23605B.objects.create(
|
|
|
|
field_b0=10000.0, field_b1=True,
|
|
|
|
modelc_fk=c1, modela_fk=a1)
|
|
|
|
complex_q = Q(pk__in=Ticket23605A.objects.filter(
|
|
|
|
Q(
|
|
|
|
# True for a1 as field_b0 = 10000, field_c0=10000
|
|
|
|
# False for a2 as no ticket23605b found
|
|
|
|
ticket23605b__field_b0__gte=1000000 /
|
|
|
|
F("ticket23605b__modelc_fk__field_c0")
|
|
|
|
) &
|
|
|
|
# True for a1 (field_b1=True)
|
2016-04-04 08:37:32 +08:00
|
|
|
Q(ticket23605b__field_b1=True) & ~Q(ticket23605b__pk__in=Ticket23605B.objects.filter(
|
2014-10-07 21:07:46 +08:00
|
|
|
~(
|
|
|
|
# Same filters as above commented filters, but
|
|
|
|
# double-negated (one for Q() above, one for
|
|
|
|
# parentheses). So, again a1 match, a2 not.
|
|
|
|
Q(field_b1=True) &
|
|
|
|
Q(field_b0__gte=1000000 / F("modelc_fk__field_c0"))
|
|
|
|
)
|
|
|
|
))).filter(ticket23605b__field_b1=True))
|
|
|
|
qs1 = Ticket23605A.objects.filter(complex_q)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs1, [a1])
|
2014-10-07 21:07:46 +08:00
|
|
|
qs2 = Ticket23605A.objects.exclude(complex_q)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(qs2, [a2])
|
2015-02-04 21:16:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestTicket24279(TestCase):
|
|
|
|
def test_ticket_24278(self):
|
|
|
|
School.objects.create()
|
|
|
|
qs = School.objects.filter(Q(pk__in=()) | Q())
|
|
|
|
self.assertQuerysetEqual(qs, [])
|
2015-02-02 19:48:30 +08:00
|
|
|
|
|
|
|
|
2018-11-27 03:05:02 +08:00
|
|
|
class TestInvalidValuesRelation(SimpleTestCase):
|
2015-02-02 19:48:30 +08:00
|
|
|
def test_invalid_values(self):
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = "invalid literal for int() with base 10: 'abc'"
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2015-02-02 19:48:30 +08:00
|
|
|
Annotation.objects.filter(tag='abc')
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2015-02-02 19:48:30 +08:00
|
|
|
Annotation.objects.filter(tag__in=[123, 'abc'])
|
2015-04-16 13:39:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestTicket24605(TestCase):
|
|
|
|
def test_ticket_24605(self):
|
|
|
|
"""
|
|
|
|
Subquery table names should be quoted.
|
|
|
|
"""
|
|
|
|
i1 = Individual.objects.create(alive=True)
|
|
|
|
RelatedIndividual.objects.create(related=i1)
|
|
|
|
i2 = Individual.objects.create(alive=False)
|
|
|
|
RelatedIndividual.objects.create(related=i2)
|
|
|
|
i3 = Individual.objects.create(alive=True)
|
|
|
|
i4 = Individual.objects.create(alive=False)
|
|
|
|
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(Individual.objects.filter(Q(alive=False), Q(related_individual__isnull=True)), [i4])
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
Individual.objects.exclude(Q(alive=False), Q(related_individual__isnull=True)).order_by('pk'),
|
|
|
|
[i1, i2, i3]
|
2015-04-16 13:39:31 +08:00
|
|
|
)
|
2015-07-17 14:26:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Ticket23622Tests(TestCase):
|
|
|
|
@skipUnlessDBFeature('can_distinct_on_fields')
|
|
|
|
def test_ticket_23622(self):
|
|
|
|
"""
|
|
|
|
Make sure __pk__in and __in work the same for related fields when
|
|
|
|
using a distinct on subquery.
|
|
|
|
"""
|
|
|
|
a1 = Ticket23605A.objects.create()
|
|
|
|
a2 = Ticket23605A.objects.create()
|
|
|
|
c1 = Ticket23605C.objects.create(field_c0=0.0)
|
|
|
|
Ticket23605B.objects.create(
|
|
|
|
modela_fk=a1, field_b0=123,
|
2016-04-24 02:19:49 +08:00
|
|
|
field_b1=True,
|
2015-07-17 14:26:43 +08:00
|
|
|
modelc_fk=c1,
|
|
|
|
)
|
|
|
|
Ticket23605B.objects.create(
|
|
|
|
modela_fk=a1, field_b0=23,
|
2016-04-24 02:19:49 +08:00
|
|
|
field_b1=True,
|
2015-07-17 14:26:43 +08:00
|
|
|
modelc_fk=c1,
|
|
|
|
)
|
|
|
|
Ticket23605B.objects.create(
|
|
|
|
modela_fk=a1, field_b0=234,
|
2016-04-24 02:19:49 +08:00
|
|
|
field_b1=True,
|
2015-07-17 14:26:43 +08:00
|
|
|
modelc_fk=c1,
|
|
|
|
)
|
|
|
|
Ticket23605B.objects.create(
|
|
|
|
modela_fk=a1, field_b0=12,
|
2016-04-24 02:19:49 +08:00
|
|
|
field_b1=True,
|
2015-07-17 14:26:43 +08:00
|
|
|
modelc_fk=c1,
|
|
|
|
)
|
|
|
|
Ticket23605B.objects.create(
|
|
|
|
modela_fk=a2, field_b0=567,
|
2016-04-24 02:19:49 +08:00
|
|
|
field_b1=True,
|
2015-07-17 14:26:43 +08:00
|
|
|
modelc_fk=c1,
|
|
|
|
)
|
|
|
|
Ticket23605B.objects.create(
|
|
|
|
modela_fk=a2, field_b0=76,
|
2016-04-24 02:19:49 +08:00
|
|
|
field_b1=True,
|
2015-07-17 14:26:43 +08:00
|
|
|
modelc_fk=c1,
|
|
|
|
)
|
|
|
|
Ticket23605B.objects.create(
|
|
|
|
modela_fk=a2, field_b0=7,
|
2016-04-24 02:19:49 +08:00
|
|
|
field_b1=True,
|
2015-07-17 14:26:43 +08:00
|
|
|
modelc_fk=c1,
|
|
|
|
)
|
|
|
|
Ticket23605B.objects.create(
|
|
|
|
modela_fk=a2, field_b0=56,
|
2016-04-24 02:19:49 +08:00
|
|
|
field_b1=True,
|
2015-07-17 14:26:43 +08:00
|
|
|
modelc_fk=c1,
|
|
|
|
)
|
|
|
|
qx = (
|
2016-04-04 08:37:32 +08:00
|
|
|
Q(ticket23605b__pk__in=Ticket23605B.objects.order_by('modela_fk', '-field_b1').distinct('modela_fk')) &
|
|
|
|
Q(ticket23605b__field_b0__gte=300)
|
2015-07-17 14:26:43 +08:00
|
|
|
)
|
|
|
|
qy = (
|
2016-04-04 08:37:32 +08:00
|
|
|
Q(ticket23605b__in=Ticket23605B.objects.order_by('modela_fk', '-field_b1').distinct('modela_fk')) &
|
|
|
|
Q(ticket23605b__field_b0__gte=300)
|
2015-07-17 14:26:43 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
set(Ticket23605A.objects.filter(qx).values_list('pk', flat=True)),
|
|
|
|
set(Ticket23605A.objects.filter(qy).values_list('pk', flat=True))
|
|
|
|
)
|
2016-09-10 17:36:27 +08:00
|
|
|
self.assertSequenceEqual(Ticket23605A.objects.filter(qx), [a2])
|