tests.py 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. from __future__ import unicode_literals
  2. from django.core.exceptions import ObjectDoesNotExist
  3. from django.contrib.contenttypes.models import ContentType
  4. from django.db import connection
  5. from django.db.models import Prefetch
  6. from django.db.models.query import get_prefetcher
  7. from django.test import TestCase, override_settings
  8. from django.utils import six
  9. from django.utils.encoding import force_text
  10. from .models import (Author, Bio, Book, Reader, Qualification, Teacher, Department,
  11. TaggedItem, Bookmark, AuthorAddress, FavoriteAuthors, AuthorWithAge,
  12. BookWithYear, BookReview, Person, House, Room, Employee, Comment,
  13. LessonEntry, WordEntry, Author2)
  14. class PrefetchRelatedTests(TestCase):
  15. def setUp(self):
  16. self.book1 = Book.objects.create(title="Poems")
  17. self.book2 = Book.objects.create(title="Jane Eyre")
  18. self.book3 = Book.objects.create(title="Wuthering Heights")
  19. self.book4 = Book.objects.create(title="Sense and Sensibility")
  20. self.author1 = Author.objects.create(name="Charlotte",
  21. first_book=self.book1)
  22. self.author2 = Author.objects.create(name="Anne",
  23. first_book=self.book1)
  24. self.author3 = Author.objects.create(name="Emily",
  25. first_book=self.book1)
  26. self.author4 = Author.objects.create(name="Jane",
  27. first_book=self.book4)
  28. self.book1.authors.add(self.author1, self.author2, self.author3)
  29. self.book2.authors.add(self.author1)
  30. self.book3.authors.add(self.author3)
  31. self.book4.authors.add(self.author4)
  32. self.reader1 = Reader.objects.create(name="Amy")
  33. self.reader2 = Reader.objects.create(name="Belinda")
  34. self.reader1.books_read.add(self.book1, self.book4)
  35. self.reader2.books_read.add(self.book2, self.book4)
  36. def test_m2m_forward(self):
  37. with self.assertNumQueries(2):
  38. lists = [list(b.authors.all()) for b in Book.objects.prefetch_related('authors')]
  39. normal_lists = [list(b.authors.all()) for b in Book.objects.all()]
  40. self.assertEqual(lists, normal_lists)
  41. def test_m2m_reverse(self):
  42. with self.assertNumQueries(2):
  43. lists = [list(a.books.all()) for a in Author.objects.prefetch_related('books')]
  44. normal_lists = [list(a.books.all()) for a in Author.objects.all()]
  45. self.assertEqual(lists, normal_lists)
  46. def test_foreignkey_forward(self):
  47. with self.assertNumQueries(2):
  48. books = [a.first_book for a in Author.objects.prefetch_related('first_book')]
  49. normal_books = [a.first_book for a in Author.objects.all()]
  50. self.assertEqual(books, normal_books)
  51. def test_foreignkey_reverse(self):
  52. with self.assertNumQueries(2):
  53. [list(b.first_time_authors.all())
  54. for b in Book.objects.prefetch_related('first_time_authors')]
  55. self.assertQuerysetEqual(self.book2.authors.all(), ["<Author: Charlotte>"])
  56. def test_onetoone_reverse_no_match(self):
  57. # Regression for #17439
  58. with self.assertNumQueries(2):
  59. book = Book.objects.prefetch_related('bookwithyear').all()[0]
  60. with self.assertNumQueries(0):
  61. with self.assertRaises(BookWithYear.DoesNotExist):
  62. book.bookwithyear
  63. def test_survives_clone(self):
  64. with self.assertNumQueries(2):
  65. [list(b.first_time_authors.all())
  66. for b in Book.objects.prefetch_related('first_time_authors').exclude(id=1000)]
  67. def test_len(self):
  68. with self.assertNumQueries(2):
  69. qs = Book.objects.prefetch_related('first_time_authors')
  70. len(qs)
  71. [list(b.first_time_authors.all()) for b in qs]
  72. def test_bool(self):
  73. with self.assertNumQueries(2):
  74. qs = Book.objects.prefetch_related('first_time_authors')
  75. bool(qs)
  76. [list(b.first_time_authors.all()) for b in qs]
  77. def test_count(self):
  78. with self.assertNumQueries(2):
  79. qs = Book.objects.prefetch_related('first_time_authors')
  80. [b.first_time_authors.count() for b in qs]
  81. def test_exists(self):
  82. with self.assertNumQueries(2):
  83. qs = Book.objects.prefetch_related('first_time_authors')
  84. [b.first_time_authors.exists() for b in qs]
  85. def test_in_and_prefetch_related(self):
  86. """
  87. Regression test for #20242 - QuerySet "in" didn't work the first time
  88. when using prefetch_related. This was fixed by the removal of chunked
  89. reads from QuerySet iteration in
  90. 70679243d1786e03557c28929f9762a119e3ac14.
  91. """
  92. qs = Book.objects.prefetch_related('first_time_authors')
  93. self.assertTrue(qs[0] in qs)
  94. def test_clear(self):
  95. """
  96. Test that we can clear the behavior by calling prefetch_related()
  97. """
  98. with self.assertNumQueries(5):
  99. with_prefetch = Author.objects.prefetch_related('books')
  100. without_prefetch = with_prefetch.prefetch_related(None)
  101. [list(a.books.all()) for a in without_prefetch]
  102. def test_m2m_then_m2m(self):
  103. """
  104. Test we can follow a m2m and another m2m
  105. """
  106. with self.assertNumQueries(3):
  107. qs = Author.objects.prefetch_related('books__read_by')
  108. lists = [[[six.text_type(r) for r in b.read_by.all()]
  109. for b in a.books.all()]
  110. for a in qs]
  111. self.assertEqual(lists,
  112. [
  113. [["Amy"], ["Belinda"]], # Charlotte - Poems, Jane Eyre
  114. [["Amy"]], # Anne - Poems
  115. [["Amy"], []], # Emily - Poems, Wuthering Heights
  116. [["Amy", "Belinda"]], # Jane - Sense and Sense
  117. ])
  118. def test_overriding_prefetch(self):
  119. with self.assertNumQueries(3):
  120. qs = Author.objects.prefetch_related('books', 'books__read_by')
  121. lists = [[[six.text_type(r) for r in b.read_by.all()]
  122. for b in a.books.all()]
  123. for a in qs]
  124. self.assertEqual(lists,
  125. [
  126. [["Amy"], ["Belinda"]], # Charlotte - Poems, Jane Eyre
  127. [["Amy"]], # Anne - Poems
  128. [["Amy"], []], # Emily - Poems, Wuthering Heights
  129. [["Amy", "Belinda"]], # Jane - Sense and Sense
  130. ])
  131. with self.assertNumQueries(3):
  132. qs = Author.objects.prefetch_related('books__read_by', 'books')
  133. lists = [[[six.text_type(r) for r in b.read_by.all()]
  134. for b in a.books.all()]
  135. for a in qs]
  136. self.assertEqual(lists,
  137. [
  138. [["Amy"], ["Belinda"]], # Charlotte - Poems, Jane Eyre
  139. [["Amy"]], # Anne - Poems
  140. [["Amy"], []], # Emily - Poems, Wuthering Heights
  141. [["Amy", "Belinda"]], # Jane - Sense and Sense
  142. ])
  143. def test_get(self):
  144. """
  145. Test that objects retrieved with .get() get the prefetch behavior.
  146. """
  147. # Need a double
  148. with self.assertNumQueries(3):
  149. author = Author.objects.prefetch_related('books__read_by').get(name="Charlotte")
  150. lists = [[six.text_type(r) for r in b.read_by.all()]
  151. for b in author.books.all()]
  152. self.assertEqual(lists, [["Amy"], ["Belinda"]]) # Poems, Jane Eyre
  153. def test_foreign_key_then_m2m(self):
  154. """
  155. Test we can follow an m2m relation after a relation like ForeignKey
  156. that doesn't have many objects
  157. """
  158. with self.assertNumQueries(2):
  159. qs = Author.objects.select_related('first_book').prefetch_related('first_book__read_by')
  160. lists = [[six.text_type(r) for r in a.first_book.read_by.all()]
  161. for a in qs]
  162. self.assertEqual(lists, [["Amy"],
  163. ["Amy"],
  164. ["Amy"],
  165. ["Amy", "Belinda"]])
  166. def test_reverse_one_to_one_then_m2m(self):
  167. """
  168. Test that we can follow a m2m relation after going through
  169. the select_related reverse of an o2o.
  170. """
  171. qs = Author.objects.prefetch_related('bio__books').select_related('bio')
  172. with self.assertNumQueries(1):
  173. list(qs.all())
  174. Bio.objects.create(author=self.author1)
  175. with self.assertNumQueries(2):
  176. list(qs.all())
  177. def test_attribute_error(self):
  178. qs = Reader.objects.all().prefetch_related('books_read__xyz')
  179. with self.assertRaises(AttributeError) as cm:
  180. list(qs)
  181. self.assertTrue('prefetch_related' in str(cm.exception))
  182. def test_invalid_final_lookup(self):
  183. qs = Book.objects.prefetch_related('authors__name')
  184. with self.assertRaises(ValueError) as cm:
  185. list(qs)
  186. self.assertTrue('prefetch_related' in str(cm.exception))
  187. self.assertTrue("name" in str(cm.exception))
  188. class CustomPrefetchTests(TestCase):
  189. @classmethod
  190. def traverse_qs(cls, obj_iter, path):
  191. """
  192. Helper method that returns a list containing a list of the objects in the
  193. obj_iter. Then for each object in the obj_iter, the path will be
  194. recursively travelled and the found objects are added to the return value.
  195. """
  196. ret_val = []
  197. if hasattr(obj_iter, 'all'):
  198. obj_iter = obj_iter.all()
  199. try:
  200. iter(obj_iter)
  201. except TypeError:
  202. obj_iter = [obj_iter]
  203. for obj in obj_iter:
  204. rel_objs = []
  205. for part in path:
  206. if not part:
  207. continue
  208. try:
  209. related = getattr(obj, part[0])
  210. except ObjectDoesNotExist:
  211. continue
  212. if related is not None:
  213. rel_objs.extend(cls.traverse_qs(related, [part[1:]]))
  214. ret_val.append((obj, rel_objs))
  215. return ret_val
  216. def setUp(self):
  217. self.person1 = Person.objects.create(name="Joe")
  218. self.person2 = Person.objects.create(name="Mary")
  219. # Set main_room for each house before creating the next one for
  220. # databases where supports_nullable_unique_constraints is False.
  221. self.house1 = House.objects.create(name='House 1', address="123 Main St", owner=self.person1)
  222. self.room1_1 = Room.objects.create(name="Dining room", house=self.house1)
  223. self.room1_2 = Room.objects.create(name="Lounge", house=self.house1)
  224. self.room1_3 = Room.objects.create(name="Kitchen", house=self.house1)
  225. self.house1.main_room = self.room1_1
  226. self.house1.save()
  227. self.person1.houses.add(self.house1)
  228. self.house2 = House.objects.create(name='House 2', address="45 Side St", owner=self.person1)
  229. self.room2_1 = Room.objects.create(name="Dining room", house=self.house2)
  230. self.room2_2 = Room.objects.create(name="Lounge", house=self.house2)
  231. self.room2_3 = Room.objects.create(name="Kitchen", house=self.house2)
  232. self.house2.main_room = self.room2_1
  233. self.house2.save()
  234. self.person1.houses.add(self.house2)
  235. self.house3 = House.objects.create(name='House 3', address="6 Downing St", owner=self.person2)
  236. self.room3_1 = Room.objects.create(name="Dining room", house=self.house3)
  237. self.room3_2 = Room.objects.create(name="Lounge", house=self.house3)
  238. self.room3_3 = Room.objects.create(name="Kitchen", house=self.house3)
  239. self.house3.main_room = self.room3_1
  240. self.house3.save()
  241. self.person2.houses.add(self.house3)
  242. self.house4 = House.objects.create(name='house 4', address="7 Regents St", owner=self.person2)
  243. self.room4_1 = Room.objects.create(name="Dining room", house=self.house4)
  244. self.room4_2 = Room.objects.create(name="Lounge", house=self.house4)
  245. self.room4_3 = Room.objects.create(name="Kitchen", house=self.house4)
  246. self.house4.main_room = self.room4_1
  247. self.house4.save()
  248. self.person2.houses.add(self.house4)
  249. def test_traverse_qs(self):
  250. qs = Person.objects.prefetch_related('houses')
  251. related_objs_normal = [list(p.houses.all()) for p in qs],
  252. related_objs_from_traverse = [[inner[0] for inner in o[1]]
  253. for o in self.traverse_qs(qs, [['houses']])]
  254. self.assertEqual(related_objs_normal, (related_objs_from_traverse,))
  255. def test_ambiguous(self):
  256. # Ambiguous: Lookup was already seen with a different queryset.
  257. with self.assertRaises(ValueError):
  258. self.traverse_qs(
  259. Person.objects.prefetch_related('houses__rooms', Prefetch('houses', queryset=House.objects.all())),
  260. [['houses', 'rooms']]
  261. )
  262. # Ambiguous: Lookup houses_lst doesn't yet exist when performing houses_lst__rooms.
  263. with self.assertRaises(AttributeError):
  264. self.traverse_qs(
  265. Person.objects.prefetch_related('houses_lst__rooms', Prefetch('houses', queryset=House.objects.all(), to_attr='houses_lst')),
  266. [['houses', 'rooms']]
  267. )
  268. # Not ambiguous.
  269. self.traverse_qs(
  270. Person.objects.prefetch_related('houses__rooms', 'houses'),
  271. [['houses', 'rooms']]
  272. )
  273. self.traverse_qs(
  274. Person.objects.prefetch_related('houses__rooms', Prefetch('houses', queryset=House.objects.all(), to_attr='houses_lst')),
  275. [['houses', 'rooms']]
  276. )
  277. def test_m2m(self):
  278. # Control lookups.
  279. with self.assertNumQueries(2):
  280. lst1 = self.traverse_qs(
  281. Person.objects.prefetch_related('houses'),
  282. [['houses']]
  283. )
  284. # Test lookups.
  285. with self.assertNumQueries(2):
  286. lst2 = self.traverse_qs(
  287. Person.objects.prefetch_related(Prefetch('houses')),
  288. [['houses']]
  289. )
  290. self.assertEqual(lst1, lst2)
  291. with self.assertNumQueries(2):
  292. lst2 = self.traverse_qs(
  293. Person.objects.prefetch_related(Prefetch('houses', to_attr='houses_lst')),
  294. [['houses_lst']]
  295. )
  296. self.assertEqual(lst1, lst2)
  297. def test_reverse_m2m(self):
  298. # Control lookups.
  299. with self.assertNumQueries(2):
  300. lst1 = self.traverse_qs(
  301. House.objects.prefetch_related('occupants'),
  302. [['occupants']]
  303. )
  304. # Test lookups.
  305. with self.assertNumQueries(2):
  306. lst2 = self.traverse_qs(
  307. House.objects.prefetch_related(Prefetch('occupants')),
  308. [['occupants']]
  309. )
  310. self.assertEqual(lst1, lst2)
  311. with self.assertNumQueries(2):
  312. lst2 = self.traverse_qs(
  313. House.objects.prefetch_related(Prefetch('occupants', to_attr='occupants_lst')),
  314. [['occupants_lst']]
  315. )
  316. self.assertEqual(lst1, lst2)
  317. def test_m2m_through_fk(self):
  318. # Control lookups.
  319. with self.assertNumQueries(3):
  320. lst1 = self.traverse_qs(
  321. Room.objects.prefetch_related('house__occupants'),
  322. [['house', 'occupants']]
  323. )
  324. # Test lookups.
  325. with self.assertNumQueries(3):
  326. lst2 = self.traverse_qs(
  327. Room.objects.prefetch_related(Prefetch('house__occupants')),
  328. [['house', 'occupants']]
  329. )
  330. self.assertEqual(lst1, lst2)
  331. with self.assertNumQueries(3):
  332. lst2 = self.traverse_qs(
  333. Room.objects.prefetch_related(Prefetch('house__occupants', to_attr='occupants_lst')),
  334. [['house', 'occupants_lst']]
  335. )
  336. self.assertEqual(lst1, lst2)
  337. def test_m2m_through_gfk(self):
  338. TaggedItem.objects.create(tag="houses", content_object=self.house1)
  339. TaggedItem.objects.create(tag="houses", content_object=self.house2)
  340. # Control lookups.
  341. with self.assertNumQueries(3):
  342. lst1 = self.traverse_qs(
  343. TaggedItem.objects.filter(tag='houses').prefetch_related('content_object__rooms'),
  344. [['content_object', 'rooms']]
  345. )
  346. # Test lookups.
  347. with self.assertNumQueries(3):
  348. lst2 = self.traverse_qs(
  349. TaggedItem.objects.prefetch_related(
  350. Prefetch('content_object'),
  351. Prefetch('content_object__rooms', to_attr='rooms_lst')
  352. ),
  353. [['content_object', 'rooms_lst']]
  354. )
  355. self.assertEqual(lst1, lst2)
  356. def test_o2m_through_m2m(self):
  357. # Control lookups.
  358. with self.assertNumQueries(3):
  359. lst1 = self.traverse_qs(
  360. Person.objects.prefetch_related('houses', 'houses__rooms'),
  361. [['houses', 'rooms']]
  362. )
  363. # Test lookups.
  364. with self.assertNumQueries(3):
  365. lst2 = self.traverse_qs(
  366. Person.objects.prefetch_related(Prefetch('houses'), 'houses__rooms'),
  367. [['houses', 'rooms']]
  368. )
  369. self.assertEqual(lst1, lst2)
  370. with self.assertNumQueries(3):
  371. lst2 = self.traverse_qs(
  372. Person.objects.prefetch_related(Prefetch('houses'), Prefetch('houses__rooms')),
  373. [['houses', 'rooms']]
  374. )
  375. self.assertEqual(lst1, lst2)
  376. with self.assertNumQueries(3):
  377. lst2 = self.traverse_qs(
  378. Person.objects.prefetch_related(Prefetch('houses', to_attr='houses_lst'), 'houses_lst__rooms'),
  379. [['houses_lst', 'rooms']]
  380. )
  381. self.assertEqual(lst1, lst2)
  382. with self.assertNumQueries(3):
  383. lst2 = self.traverse_qs(
  384. Person.objects.prefetch_related(
  385. Prefetch('houses', to_attr='houses_lst'),
  386. Prefetch('houses_lst__rooms', to_attr='rooms_lst')
  387. ),
  388. [['houses_lst', 'rooms_lst']]
  389. )
  390. self.assertEqual(lst1, lst2)
  391. def test_generic_rel(self):
  392. bookmark = Bookmark.objects.create(url='http://www.djangoproject.com/')
  393. TaggedItem.objects.create(content_object=bookmark, tag='django')
  394. TaggedItem.objects.create(content_object=bookmark, favorite=bookmark, tag='python')
  395. # Control lookups.
  396. with self.assertNumQueries(4):
  397. lst1 = self.traverse_qs(
  398. Bookmark.objects.prefetch_related('tags', 'tags__content_object', 'favorite_tags'),
  399. [['tags', 'content_object'], ['favorite_tags']]
  400. )
  401. # Test lookups.
  402. with self.assertNumQueries(4):
  403. lst2 = self.traverse_qs(
  404. Bookmark.objects.prefetch_related(
  405. Prefetch('tags', to_attr='tags_lst'),
  406. Prefetch('tags_lst__content_object'),
  407. Prefetch('favorite_tags'),
  408. ),
  409. [['tags_lst', 'content_object'], ['favorite_tags']]
  410. )
  411. self.assertEqual(lst1, lst2)
  412. def test_traverse_single_item_property(self):
  413. # Control lookups.
  414. with self.assertNumQueries(5):
  415. lst1 = self.traverse_qs(
  416. Person.objects.prefetch_related(
  417. 'houses__rooms',
  418. 'primary_house__occupants__houses',
  419. ),
  420. [['primary_house', 'occupants', 'houses']]
  421. )
  422. # Test lookups.
  423. with self.assertNumQueries(5):
  424. lst2 = self.traverse_qs(
  425. Person.objects.prefetch_related(
  426. 'houses__rooms',
  427. Prefetch('primary_house__occupants', to_attr='occupants_lst'),
  428. 'primary_house__occupants_lst__houses',
  429. ),
  430. [['primary_house', 'occupants_lst', 'houses']]
  431. )
  432. self.assertEqual(lst1, lst2)
  433. def test_traverse_multiple_items_property(self):
  434. # Control lookups.
  435. with self.assertNumQueries(4):
  436. lst1 = self.traverse_qs(
  437. Person.objects.prefetch_related(
  438. 'houses',
  439. 'all_houses__occupants__houses',
  440. ),
  441. [['all_houses', 'occupants', 'houses']]
  442. )
  443. # Test lookups.
  444. with self.assertNumQueries(4):
  445. lst2 = self.traverse_qs(
  446. Person.objects.prefetch_related(
  447. 'houses',
  448. Prefetch('all_houses__occupants', to_attr='occupants_lst'),
  449. 'all_houses__occupants_lst__houses',
  450. ),
  451. [['all_houses', 'occupants_lst', 'houses']]
  452. )
  453. self.assertEqual(lst1, lst2)
  454. def test_custom_qs(self):
  455. # Test basic.
  456. with self.assertNumQueries(2):
  457. lst1 = list(Person.objects.prefetch_related('houses'))
  458. with self.assertNumQueries(2):
  459. lst2 = list(Person.objects.prefetch_related(
  460. Prefetch('houses', queryset=House.objects.all(), to_attr='houses_lst')))
  461. self.assertEqual(
  462. self.traverse_qs(lst1, [['houses']]),
  463. self.traverse_qs(lst2, [['houses_lst']])
  464. )
  465. # Test queryset filtering.
  466. with self.assertNumQueries(2):
  467. lst2 = list(Person.objects.prefetch_related(
  468. Prefetch('houses', queryset=House.objects.filter(pk__in=[self.house1.pk, self.house3.pk]), to_attr='houses_lst')))
  469. self.assertEqual(len(lst2[0].houses_lst), 1)
  470. self.assertEqual(lst2[0].houses_lst[0], self.house1)
  471. self.assertEqual(len(lst2[1].houses_lst), 1)
  472. self.assertEqual(lst2[1].houses_lst[0], self.house3)
  473. # Test flattened.
  474. with self.assertNumQueries(3):
  475. lst1 = list(Person.objects.prefetch_related('houses__rooms'))
  476. with self.assertNumQueries(3):
  477. lst2 = list(Person.objects.prefetch_related(
  478. Prefetch('houses__rooms', queryset=Room.objects.all(), to_attr='rooms_lst')))
  479. self.assertEqual(
  480. self.traverse_qs(lst1, [['houses', 'rooms']]),
  481. self.traverse_qs(lst2, [['houses', 'rooms_lst']])
  482. )
  483. # Test inner select_related.
  484. with self.assertNumQueries(3):
  485. lst1 = list(Person.objects.prefetch_related('houses__owner'))
  486. with self.assertNumQueries(2):
  487. lst2 = list(Person.objects.prefetch_related(
  488. Prefetch('houses', queryset=House.objects.select_related('owner'))))
  489. self.assertEqual(
  490. self.traverse_qs(lst1, [['houses', 'owner']]),
  491. self.traverse_qs(lst2, [['houses', 'owner']])
  492. )
  493. # Test inner prefetch.
  494. inner_rooms_qs = Room.objects.filter(pk__in=[self.room1_1.pk, self.room1_2.pk])
  495. houses_qs_prf = House.objects.prefetch_related(
  496. Prefetch('rooms', queryset=inner_rooms_qs, to_attr='rooms_lst'))
  497. with self.assertNumQueries(4):
  498. lst2 = list(Person.objects.prefetch_related(
  499. Prefetch('houses', queryset=houses_qs_prf.filter(pk=self.house1.pk), to_attr='houses_lst'),
  500. Prefetch('houses_lst__rooms_lst__main_room_of')
  501. ))
  502. self.assertEqual(len(lst2[0].houses_lst[0].rooms_lst), 2)
  503. self.assertEqual(lst2[0].houses_lst[0].rooms_lst[0], self.room1_1)
  504. self.assertEqual(lst2[0].houses_lst[0].rooms_lst[1], self.room1_2)
  505. self.assertEqual(lst2[0].houses_lst[0].rooms_lst[0].main_room_of, self.house1)
  506. self.assertEqual(len(lst2[1].houses_lst), 0)
  507. # Test ReverseSingleRelatedObjectDescriptor.
  508. houses = House.objects.select_related('owner')
  509. with self.assertNumQueries(6):
  510. rooms = Room.objects.all().prefetch_related('house')
  511. lst1 = self.traverse_qs(rooms, [['house', 'owner']])
  512. with self.assertNumQueries(2):
  513. rooms = Room.objects.all().prefetch_related(Prefetch('house', queryset=houses.all()))
  514. lst2 = self.traverse_qs(rooms, [['house', 'owner']])
  515. self.assertEqual(lst1, lst2)
  516. with self.assertNumQueries(2):
  517. houses = House.objects.select_related('owner')
  518. rooms = Room.objects.all().prefetch_related(Prefetch('house', queryset=houses.all(), to_attr='house_attr'))
  519. lst2 = self.traverse_qs(rooms, [['house_attr', 'owner']])
  520. self.assertEqual(lst1, lst2)
  521. room = Room.objects.all().prefetch_related(Prefetch('house', queryset=houses.filter(address='DoesNotExist'))).first()
  522. with self.assertRaises(ObjectDoesNotExist):
  523. getattr(room, 'house')
  524. room = Room.objects.all().prefetch_related(Prefetch('house', queryset=houses.filter(address='DoesNotExist'), to_attr='house_attr')).first()
  525. self.assertIsNone(room.house_attr)
  526. rooms = Room.objects.all().prefetch_related(Prefetch('house', queryset=House.objects.only('name')))
  527. with self.assertNumQueries(2):
  528. getattr(rooms.first().house, 'name')
  529. with self.assertNumQueries(3):
  530. getattr(rooms.first().house, 'address')
  531. # Test SingleRelatedObjectDescriptor.
  532. houses = House.objects.select_related('owner')
  533. with self.assertNumQueries(6):
  534. rooms = Room.objects.all().prefetch_related('main_room_of')
  535. lst1 = self.traverse_qs(rooms, [['main_room_of', 'owner']])
  536. with self.assertNumQueries(2):
  537. rooms = Room.objects.all().prefetch_related(Prefetch('main_room_of', queryset=houses.all()))
  538. lst2 = self.traverse_qs(rooms, [['main_room_of', 'owner']])
  539. self.assertEqual(lst1, lst2)
  540. with self.assertNumQueries(2):
  541. rooms = list(Room.objects.all().prefetch_related(Prefetch('main_room_of', queryset=houses.all(), to_attr='main_room_of_attr')))
  542. lst2 = self.traverse_qs(rooms, [['main_room_of_attr', 'owner']])
  543. self.assertEqual(lst1, lst2)
  544. room = Room.objects.filter(main_room_of__isnull=False).prefetch_related(Prefetch('main_room_of', queryset=houses.filter(address='DoesNotExist'))).first()
  545. with self.assertRaises(ObjectDoesNotExist):
  546. getattr(room, 'main_room_of')
  547. room = Room.objects.filter(main_room_of__isnull=False).prefetch_related(Prefetch('main_room_of', queryset=houses.filter(address='DoesNotExist'), to_attr='main_room_of_attr')).first()
  548. self.assertIsNone(room.main_room_of_attr)
  549. class DefaultManagerTests(TestCase):
  550. def setUp(self):
  551. self.qual1 = Qualification.objects.create(name="BA")
  552. self.qual2 = Qualification.objects.create(name="BSci")
  553. self.qual3 = Qualification.objects.create(name="MA")
  554. self.qual4 = Qualification.objects.create(name="PhD")
  555. self.teacher1 = Teacher.objects.create(name="Mr Cleese")
  556. self.teacher2 = Teacher.objects.create(name="Mr Idle")
  557. self.teacher3 = Teacher.objects.create(name="Mr Chapman")
  558. self.teacher1.qualifications.add(self.qual1, self.qual2, self.qual3, self.qual4)
  559. self.teacher2.qualifications.add(self.qual1)
  560. self.teacher3.qualifications.add(self.qual2)
  561. self.dept1 = Department.objects.create(name="English")
  562. self.dept2 = Department.objects.create(name="Physics")
  563. self.dept1.teachers.add(self.teacher1, self.teacher2)
  564. self.dept2.teachers.add(self.teacher1, self.teacher3)
  565. def test_m2m_then_m2m(self):
  566. with self.assertNumQueries(3):
  567. # When we prefetch the teachers, and force the query, we don't want
  568. # the default manager on teachers to immediately get all the related
  569. # qualifications, since this will do one query per teacher.
  570. qs = Department.objects.prefetch_related('teachers')
  571. depts = "".join(["%s department: %s\n" %
  572. (dept.name, ", ".join(six.text_type(t) for t in dept.teachers.all()))
  573. for dept in qs])
  574. self.assertEqual(depts,
  575. "English department: Mr Cleese (BA, BSci, MA, PhD), Mr Idle (BA)\n"
  576. "Physics department: Mr Cleese (BA, BSci, MA, PhD), Mr Chapman (BSci)\n")
  577. class GenericRelationTests(TestCase):
  578. def setUp(self):
  579. book1 = Book.objects.create(title="Winnie the Pooh")
  580. book2 = Book.objects.create(title="Do you like green eggs and spam?")
  581. book3 = Book.objects.create(title="Three Men In A Boat")
  582. reader1 = Reader.objects.create(name="me")
  583. reader2 = Reader.objects.create(name="you")
  584. reader3 = Reader.objects.create(name="someone")
  585. book1.read_by.add(reader1, reader2)
  586. book2.read_by.add(reader2)
  587. book3.read_by.add(reader3)
  588. self.book1, self.book2, self.book3 = book1, book2, book3
  589. self.reader1, self.reader2, self.reader3 = reader1, reader2, reader3
  590. def test_prefetch_GFK(self):
  591. TaggedItem.objects.create(tag="awesome", content_object=self.book1)
  592. TaggedItem.objects.create(tag="great", content_object=self.reader1)
  593. TaggedItem.objects.create(tag="outstanding", content_object=self.book2)
  594. TaggedItem.objects.create(tag="amazing", content_object=self.reader3)
  595. # 1 for TaggedItem table, 1 for Book table, 1 for Reader table
  596. with self.assertNumQueries(3):
  597. qs = TaggedItem.objects.prefetch_related('content_object')
  598. list(qs)
  599. def test_prefetch_GFK_nonint_pk(self):
  600. Comment.objects.create(comment="awesome", content_object=self.book1)
  601. # 1 for Comment table, 1 for Book table
  602. with self.assertNumQueries(2):
  603. qs = Comment.objects.prefetch_related('content_object')
  604. [c.content_object for c in qs]
  605. def test_traverse_GFK(self):
  606. """
  607. Test that we can traverse a 'content_object' with prefetch_related() and
  608. get to related objects on the other side (assuming it is suitably
  609. filtered)
  610. """
  611. TaggedItem.objects.create(tag="awesome", content_object=self.book1)
  612. TaggedItem.objects.create(tag="awesome", content_object=self.book2)
  613. TaggedItem.objects.create(tag="awesome", content_object=self.book3)
  614. TaggedItem.objects.create(tag="awesome", content_object=self.reader1)
  615. TaggedItem.objects.create(tag="awesome", content_object=self.reader2)
  616. ct = ContentType.objects.get_for_model(Book)
  617. # We get 3 queries - 1 for main query, 1 for content_objects since they
  618. # all use the same table, and 1 for the 'read_by' relation.
  619. with self.assertNumQueries(3):
  620. # If we limit to books, we know that they will have 'read_by'
  621. # attributes, so the following makes sense:
  622. qs = TaggedItem.objects.filter(content_type=ct, tag='awesome').prefetch_related('content_object__read_by')
  623. readers_of_awesome_books = {r.name for tag in qs
  624. for r in tag.content_object.read_by.all()}
  625. self.assertEqual(readers_of_awesome_books, {"me", "you", "someone"})
  626. def test_nullable_GFK(self):
  627. TaggedItem.objects.create(tag="awesome", content_object=self.book1,
  628. created_by=self.reader1)
  629. TaggedItem.objects.create(tag="great", content_object=self.book2)
  630. TaggedItem.objects.create(tag="rubbish", content_object=self.book3)
  631. with self.assertNumQueries(2):
  632. result = [t.created_by for t in TaggedItem.objects.prefetch_related('created_by')]
  633. self.assertEqual(result,
  634. [t.created_by for t in TaggedItem.objects.all()])
  635. def test_generic_relation(self):
  636. bookmark = Bookmark.objects.create(url='http://www.djangoproject.com/')
  637. TaggedItem.objects.create(content_object=bookmark, tag='django')
  638. TaggedItem.objects.create(content_object=bookmark, tag='python')
  639. with self.assertNumQueries(2):
  640. tags = [t.tag for b in Bookmark.objects.prefetch_related('tags')
  641. for t in b.tags.all()]
  642. self.assertEqual(sorted(tags), ["django", "python"])
  643. def test_charfield_GFK(self):
  644. b = Bookmark.objects.create(url='http://www.djangoproject.com/')
  645. TaggedItem.objects.create(content_object=b, tag='django')
  646. TaggedItem.objects.create(content_object=b, favorite=b, tag='python')
  647. with self.assertNumQueries(3):
  648. bookmark = Bookmark.objects.filter(pk=b.pk).prefetch_related('tags', 'favorite_tags')[0]
  649. self.assertEqual(sorted([i.tag for i in bookmark.tags.all()]), ["django", "python"])
  650. self.assertEqual([i.tag for i in bookmark.favorite_tags.all()], ["python"])
  651. class MultiTableInheritanceTest(TestCase):
  652. def setUp(self):
  653. self.book1 = BookWithYear.objects.create(
  654. title="Poems", published_year=2010)
  655. self.book2 = BookWithYear.objects.create(
  656. title="More poems", published_year=2011)
  657. self.author1 = AuthorWithAge.objects.create(
  658. name='Jane', first_book=self.book1, age=50)
  659. self.author2 = AuthorWithAge.objects.create(
  660. name='Tom', first_book=self.book1, age=49)
  661. self.author3 = AuthorWithAge.objects.create(
  662. name='Robert', first_book=self.book2, age=48)
  663. self.authorAddress = AuthorAddress.objects.create(
  664. author=self.author1, address='SomeStreet 1')
  665. self.book2.aged_authors.add(self.author2, self.author3)
  666. self.br1 = BookReview.objects.create(
  667. book=self.book1, notes="review book1")
  668. self.br2 = BookReview.objects.create(
  669. book=self.book2, notes="review book2")
  670. def test_foreignkey(self):
  671. with self.assertNumQueries(2):
  672. qs = AuthorWithAge.objects.prefetch_related('addresses')
  673. addresses = [[six.text_type(address) for address in obj.addresses.all()]
  674. for obj in qs]
  675. self.assertEqual(addresses, [[six.text_type(self.authorAddress)], [], []])
  676. def test_foreignkey_to_inherited(self):
  677. with self.assertNumQueries(2):
  678. qs = BookReview.objects.prefetch_related('book')
  679. titles = [obj.book.title for obj in qs]
  680. self.assertEqual(titles, ["Poems", "More poems"])
  681. def test_m2m_to_inheriting_model(self):
  682. qs = AuthorWithAge.objects.prefetch_related('books_with_year')
  683. with self.assertNumQueries(2):
  684. lst = [[six.text_type(book) for book in author.books_with_year.all()]
  685. for author in qs]
  686. qs = AuthorWithAge.objects.all()
  687. lst2 = [[six.text_type(book) for book in author.books_with_year.all()]
  688. for author in qs]
  689. self.assertEqual(lst, lst2)
  690. qs = BookWithYear.objects.prefetch_related('aged_authors')
  691. with self.assertNumQueries(2):
  692. lst = [[six.text_type(author) for author in book.aged_authors.all()]
  693. for book in qs]
  694. qs = BookWithYear.objects.all()
  695. lst2 = [[six.text_type(author) for author in book.aged_authors.all()]
  696. for book in qs]
  697. self.assertEqual(lst, lst2)
  698. def test_parent_link_prefetch(self):
  699. with self.assertNumQueries(2):
  700. [a.author for a in AuthorWithAge.objects.prefetch_related('author')]
  701. @override_settings(DEBUG=True)
  702. def test_child_link_prefetch(self):
  703. with self.assertNumQueries(2):
  704. l = [a.authorwithage for a in Author.objects.prefetch_related('authorwithage')]
  705. # Regression for #18090: the prefetching query must include an IN clause.
  706. # Note that on Oracle the table name is upper case in the generated SQL,
  707. # thus the .lower() call.
  708. self.assertIn('authorwithage', connection.queries[-1]['sql'].lower())
  709. self.assertIn(' IN ', connection.queries[-1]['sql'])
  710. self.assertEqual(l, [a.authorwithage for a in Author.objects.all()])
  711. class ForeignKeyToFieldTest(TestCase):
  712. def setUp(self):
  713. self.book = Book.objects.create(title="Poems")
  714. self.author1 = Author.objects.create(name='Jane', first_book=self.book)
  715. self.author2 = Author.objects.create(name='Tom', first_book=self.book)
  716. self.author3 = Author.objects.create(name='Robert', first_book=self.book)
  717. self.authorAddress = AuthorAddress.objects.create(
  718. author=self.author1, address='SomeStreet 1'
  719. )
  720. FavoriteAuthors.objects.create(author=self.author1,
  721. likes_author=self.author2)
  722. FavoriteAuthors.objects.create(author=self.author2,
  723. likes_author=self.author3)
  724. FavoriteAuthors.objects.create(author=self.author3,
  725. likes_author=self.author1)
  726. def test_foreignkey(self):
  727. with self.assertNumQueries(2):
  728. qs = Author.objects.prefetch_related('addresses')
  729. addresses = [[six.text_type(address) for address in obj.addresses.all()]
  730. for obj in qs]
  731. self.assertEqual(addresses, [[six.text_type(self.authorAddress)], [], []])
  732. def test_m2m(self):
  733. with self.assertNumQueries(3):
  734. qs = Author.objects.all().prefetch_related('favorite_authors', 'favors_me')
  735. favorites = [(
  736. [six.text_type(i_like) for i_like in author.favorite_authors.all()],
  737. [six.text_type(likes_me) for likes_me in author.favors_me.all()]
  738. ) for author in qs]
  739. self.assertEqual(
  740. favorites,
  741. [
  742. ([six.text_type(self.author2)], [six.text_type(self.author3)]),
  743. ([six.text_type(self.author3)], [six.text_type(self.author1)]),
  744. ([six.text_type(self.author1)], [six.text_type(self.author2)])
  745. ]
  746. )
  747. class LookupOrderingTest(TestCase):
  748. """
  749. Test cases that demonstrate that ordering of lookups is important, and
  750. ensure it is preserved.
  751. """
  752. def setUp(self):
  753. self.person1 = Person.objects.create(name="Joe")
  754. self.person2 = Person.objects.create(name="Mary")
  755. # Set main_room for each house before creating the next one for
  756. # databases where supports_nullable_unique_constraints is False.
  757. self.house1 = House.objects.create(address="123 Main St")
  758. self.room1_1 = Room.objects.create(name="Dining room", house=self.house1)
  759. self.room1_2 = Room.objects.create(name="Lounge", house=self.house1)
  760. self.room1_3 = Room.objects.create(name="Kitchen", house=self.house1)
  761. self.house1.main_room = self.room1_1
  762. self.house1.save()
  763. self.person1.houses.add(self.house1)
  764. self.house2 = House.objects.create(address="45 Side St")
  765. self.room2_1 = Room.objects.create(name="Dining room", house=self.house2)
  766. self.room2_2 = Room.objects.create(name="Lounge", house=self.house2)
  767. self.house2.main_room = self.room2_1
  768. self.house2.save()
  769. self.person1.houses.add(self.house2)
  770. self.house3 = House.objects.create(address="6 Downing St")
  771. self.room3_1 = Room.objects.create(name="Dining room", house=self.house3)
  772. self.room3_2 = Room.objects.create(name="Lounge", house=self.house3)
  773. self.room3_3 = Room.objects.create(name="Kitchen", house=self.house3)
  774. self.house3.main_room = self.room3_1
  775. self.house3.save()
  776. self.person2.houses.add(self.house3)
  777. self.house4 = House.objects.create(address="7 Regents St")
  778. self.room4_1 = Room.objects.create(name="Dining room", house=self.house4)
  779. self.room4_2 = Room.objects.create(name="Lounge", house=self.house4)
  780. self.house4.main_room = self.room4_1
  781. self.house4.save()
  782. self.person2.houses.add(self.house4)
  783. def test_order(self):
  784. with self.assertNumQueries(4):
  785. # The following two queries must be done in the same order as written,
  786. # otherwise 'primary_house' will cause non-prefetched lookups
  787. qs = Person.objects.prefetch_related('houses__rooms',
  788. 'primary_house__occupants')
  789. [list(p.primary_house.occupants.all()) for p in qs]
  790. class NullableTest(TestCase):
  791. def setUp(self):
  792. boss = Employee.objects.create(name="Peter")
  793. Employee.objects.create(name="Joe", boss=boss)
  794. Employee.objects.create(name="Angela", boss=boss)
  795. def test_traverse_nullable(self):
  796. # Because we use select_related() for 'boss', it doesn't need to be
  797. # prefetched, but we can still traverse it although it contains some nulls
  798. with self.assertNumQueries(2):
  799. qs = Employee.objects.select_related('boss').prefetch_related('boss__serfs')
  800. co_serfs = [list(e.boss.serfs.all()) if e.boss is not None else []
  801. for e in qs]
  802. qs2 = Employee.objects.select_related('boss')
  803. co_serfs2 = [list(e.boss.serfs.all()) if e.boss is not None else [] for e in qs2]
  804. self.assertEqual(co_serfs, co_serfs2)
  805. def test_prefetch_nullable(self):
  806. # One for main employee, one for boss, one for serfs
  807. with self.assertNumQueries(3):
  808. qs = Employee.objects.prefetch_related('boss__serfs')
  809. co_serfs = [list(e.boss.serfs.all()) if e.boss is not None else []
  810. for e in qs]
  811. qs2 = Employee.objects.all()
  812. co_serfs2 = [list(e.boss.serfs.all()) if e.boss is not None else [] for e in qs2]
  813. self.assertEqual(co_serfs, co_serfs2)
  814. def test_in_bulk(self):
  815. """
  816. In-bulk does correctly prefetch objects by not using .iterator()
  817. directly.
  818. """
  819. boss1 = Employee.objects.create(name="Peter")
  820. boss2 = Employee.objects.create(name="Jack")
  821. with self.assertNumQueries(2):
  822. # Check that prefetch is done and it does not cause any errors.
  823. bulk = Employee.objects.prefetch_related('serfs').in_bulk([boss1.pk, boss2.pk])
  824. for b in bulk.values():
  825. list(b.serfs.all())
  826. class MultiDbTests(TestCase):
  827. multi_db = True
  828. def test_using_is_honored_m2m(self):
  829. B = Book.objects.using('other')
  830. A = Author.objects.using('other')
  831. book1 = B.create(title="Poems")
  832. book2 = B.create(title="Jane Eyre")
  833. book3 = B.create(title="Wuthering Heights")
  834. book4 = B.create(title="Sense and Sensibility")
  835. author1 = A.create(name="Charlotte", first_book=book1)
  836. author2 = A.create(name="Anne", first_book=book1)
  837. author3 = A.create(name="Emily", first_book=book1)
  838. author4 = A.create(name="Jane", first_book=book4)
  839. book1.authors.add(author1, author2, author3)
  840. book2.authors.add(author1)
  841. book3.authors.add(author3)
  842. book4.authors.add(author4)
  843. # Forward
  844. qs1 = B.prefetch_related('authors')
  845. with self.assertNumQueries(2, using='other'):
  846. books = "".join(["%s (%s)\n" %
  847. (book.title, ", ".join(a.name for a in book.authors.all()))
  848. for book in qs1])
  849. self.assertEqual(books,
  850. "Poems (Charlotte, Anne, Emily)\n"
  851. "Jane Eyre (Charlotte)\n"
  852. "Wuthering Heights (Emily)\n"
  853. "Sense and Sensibility (Jane)\n")
  854. # Reverse
  855. qs2 = A.prefetch_related('books')
  856. with self.assertNumQueries(2, using='other'):
  857. authors = "".join(["%s: %s\n" %
  858. (author.name, ", ".join(b.title for b in author.books.all()))
  859. for author in qs2])
  860. self.assertEqual(authors,
  861. "Charlotte: Poems, Jane Eyre\n"
  862. "Anne: Poems\n"
  863. "Emily: Poems, Wuthering Heights\n"
  864. "Jane: Sense and Sensibility\n")
  865. def test_using_is_honored_fkey(self):
  866. B = Book.objects.using('other')
  867. A = Author.objects.using('other')
  868. book1 = B.create(title="Poems")
  869. book2 = B.create(title="Sense and Sensibility")
  870. A.create(name="Charlotte Bronte", first_book=book1)
  871. A.create(name="Jane Austen", first_book=book2)
  872. # Forward
  873. with self.assertNumQueries(2, using='other'):
  874. books = ", ".join(a.first_book.title for a in A.prefetch_related('first_book'))
  875. self.assertEqual("Poems, Sense and Sensibility", books)
  876. # Reverse
  877. with self.assertNumQueries(2, using='other'):
  878. books = "".join("%s (%s)\n" %
  879. (b.title, ", ".join(a.name for a in b.first_time_authors.all()))
  880. for b in B.prefetch_related('first_time_authors'))
  881. self.assertEqual(books,
  882. "Poems (Charlotte Bronte)\n"
  883. "Sense and Sensibility (Jane Austen)\n")
  884. def test_using_is_honored_inheritance(self):
  885. B = BookWithYear.objects.using('other')
  886. A = AuthorWithAge.objects.using('other')
  887. book1 = B.create(title="Poems", published_year=2010)
  888. B.create(title="More poems", published_year=2011)
  889. A.create(name='Jane', first_book=book1, age=50)
  890. A.create(name='Tom', first_book=book1, age=49)
  891. # parent link
  892. with self.assertNumQueries(2, using='other'):
  893. authors = ", ".join(a.author.name for a in A.prefetch_related('author'))
  894. self.assertEqual(authors, "Jane, Tom")
  895. # child link
  896. with self.assertNumQueries(2, using='other'):
  897. ages = ", ".join(str(a.authorwithage.age) for a in A.prefetch_related('authorwithage'))
  898. self.assertEqual(ages, "50, 49")
  899. def test_using_is_honored_custom_qs(self):
  900. B = Book.objects.using('other')
  901. A = Author.objects.using('other')
  902. book1 = B.create(title="Poems")
  903. book2 = B.create(title="Sense and Sensibility")
  904. A.create(name="Charlotte Bronte", first_book=book1)
  905. A.create(name="Jane Austen", first_book=book2)
  906. # Implicit hinting
  907. with self.assertNumQueries(2, using='other'):
  908. prefetch = Prefetch('first_time_authors', queryset=Author.objects.all())
  909. books = "".join("%s (%s)\n" %
  910. (b.title, ", ".join(a.name for a in b.first_time_authors.all()))
  911. for b in B.prefetch_related(prefetch))
  912. self.assertEqual(books,
  913. "Poems (Charlotte Bronte)\n"
  914. "Sense and Sensibility (Jane Austen)\n")
  915. # Explicit using on the same db.
  916. with self.assertNumQueries(2, using='other'):
  917. prefetch = Prefetch('first_time_authors', queryset=Author.objects.using('other'))
  918. books = "".join("%s (%s)\n" %
  919. (b.title, ", ".join(a.name for a in b.first_time_authors.all()))
  920. for b in B.prefetch_related(prefetch))
  921. self.assertEqual(books,
  922. "Poems (Charlotte Bronte)\n"
  923. "Sense and Sensibility (Jane Austen)\n")
  924. # Explicit using on a different db.
  925. with self.assertNumQueries(1, using='default'), self.assertNumQueries(1, using='other'):
  926. prefetch = Prefetch('first_time_authors', queryset=Author.objects.using('default'))
  927. books = "".join("%s (%s)\n" %
  928. (b.title, ", ".join(a.name for a in b.first_time_authors.all()))
  929. for b in B.prefetch_related(prefetch))
  930. self.assertEqual(books,
  931. "Poems ()\n"
  932. "Sense and Sensibility ()\n")
  933. class Ticket19607Tests(TestCase):
  934. def setUp(self):
  935. for id, name1, name2 in [
  936. (1, 'einfach', 'simple'),
  937. (2, 'schwierig', 'difficult'),
  938. ]:
  939. LessonEntry.objects.create(id=id, name1=name1, name2=name2)
  940. for id, lesson_entry_id, name in [
  941. (1, 1, 'einfach'),
  942. (2, 1, 'simple'),
  943. (3, 2, 'schwierig'),
  944. (4, 2, 'difficult'),
  945. ]:
  946. WordEntry.objects.create(id=id, lesson_entry_id=lesson_entry_id, name=name)
  947. def test_bug(self):
  948. list(WordEntry.objects.prefetch_related('lesson_entry', 'lesson_entry__wordentry_set'))
  949. class Ticket21410Tests(TestCase):
  950. def setUp(self):
  951. self.book1 = Book.objects.create(title="Poems")
  952. self.book2 = Book.objects.create(title="Jane Eyre")
  953. self.book3 = Book.objects.create(title="Wuthering Heights")
  954. self.book4 = Book.objects.create(title="Sense and Sensibility")
  955. self.author1 = Author2.objects.create(name="Charlotte",
  956. first_book=self.book1)
  957. self.author2 = Author2.objects.create(name="Anne",
  958. first_book=self.book1)
  959. self.author3 = Author2.objects.create(name="Emily",
  960. first_book=self.book1)
  961. self.author4 = Author2.objects.create(name="Jane",
  962. first_book=self.book4)
  963. self.author1.favorite_books.add(self.book1, self.book2, self.book3)
  964. self.author2.favorite_books.add(self.book1)
  965. self.author3.favorite_books.add(self.book2)
  966. self.author4.favorite_books.add(self.book3)
  967. def test_bug(self):
  968. list(Author2.objects.prefetch_related('first_book', 'favorite_books'))
  969. class Ticket21760Tests(TestCase):
  970. def setUp(self):
  971. self.rooms = []
  972. for _ in range(3):
  973. house = House.objects.create()
  974. for _ in range(3):
  975. self.rooms.append(Room.objects.create(house=house))
  976. # Set main_room for each house before creating the next one for
  977. # databases where supports_nullable_unique_constraints is False.
  978. house.main_room = self.rooms[-3]
  979. house.save()
  980. def test_bug(self):
  981. prefetcher = get_prefetcher(self.rooms[0], 'house')[0]
  982. queryset = prefetcher.get_prefetch_queryset(list(Room.objects.all()))[0]
  983. self.assertNotIn(' JOIN ', force_text(queryset.query))