| 1528 | def test_detect_is_fetched_with_to_attr_multiple(self): |
| 1529 | self.book1.authors.add(self.author11, self.author12) |
| 1530 | self.book2.authors.add(self.author21) |
| 1531 | |
| 1532 | with self.assertNumQueries(3): |
| 1533 | books = Book.objects.filter( |
| 1534 | title__in=['book1', 'book2'], |
| 1535 | authors__name__startswith='Author', |
| 1536 | ).prefetch_related( |
| 1537 | Prefetch( |
| 1538 | 'first_time_authors', |
| 1539 | Author.objects.prefetch_related( |
| 1540 | Prefetch( |
| 1541 | 'addresses', |
| 1542 | AuthorAddress.objects.filter(address='Happy place'), |
| 1543 | to_attr='happy_place', |
| 1544 | ) |
| 1545 | ), |
| 1546 | to_attr='first_authors', |
| 1547 | ), |
| 1548 | ) |
| 1549 | book1_1, book1_2, book2 = list(books) |
| 1550 | |
| 1551 | with self.assertNumQueries(0): |
| 1552 | self.assertEqual(book1_1.first_authors, [self.author11, self.author12]) |
| 1553 | self.assertEqual(book1_2.first_authors, [self.author11, self.author12]) |
| 1554 | self.assertEqual(book2.first_authors, [self.author21]) |
| 1555 | |
| 1556 | self.assertEqual(book1_1.first_authors[0].happy_place, [self.author1_address1]) |
| 1557 | self.assertEqual(book1_1.first_authors[1].happy_place, []) |
| 1558 | self.assertEqual(book1_2.first_authors[0].happy_place, [self.author1_address1]) |
| 1559 | self.assertEqual(book1_2.first_authors[1].happy_place, []) |
| 1560 | self.assertEqual(book2.first_authors[0].happy_place, [self.author2_address1]) |
| 1561 | |