| 1173 | |
| 1174 | @skipUnlessDBFeature('supports_subqueries_in_group_by') |
| 1175 | def test_aggregation_subquery_annotation_related_field(self): |
| 1176 | publisher = Publisher.objects.create(name=self.a9.name, num_awards=2) |
| 1177 | book = Book.objects.create( |
| 1178 | isbn='159059999', name='Test book.', pages=819, rating=2.5, |
| 1179 | price=Decimal('14.44'), contact=self.a9, publisher=publisher, |
| 1180 | pubdate=datetime.date(2019, 12, 6), |
| 1181 | ) |
| 1182 | book.authors.add(self.a5, self.a6, self.a7) |
| 1183 | books_qs = Book.objects.annotate( |
| 1184 | contact_publisher=Subquery( |
| 1185 | Publisher.objects.filter( |
| 1186 | pk=OuterRef('publisher'), |
| 1187 | name=OuterRef('contact__name'), |
| 1188 | ).values('name')[:1], |
| 1189 | ) |
| 1190 | ).filter( |
| 1191 | contact_publisher__isnull=False, |
| 1192 | ).annotate(count=Count('authors')) |
| 1193 | self.assertSequenceEqual(books_qs, [book]) |