| 600 | return [Authenticated()] |
| 601 | |
| 602 | def get_queryset(self): |
| 603 | # Annotate both counts at the SQL level so the serializer methods |
| 604 | # can read them from the object rather than issuing a COUNT per row. |
| 605 | # `distinct=True` is required when multiple reverse-FK annotations |
| 606 | # share the same queryset to avoid row-multiplication artifacts. |
| 607 | # m3u_accounts is still prefetched for the nested serializer data. |
| 608 | return ( |
| 609 | ChannelGroup.objects |
| 610 | .annotate( |
| 611 | channel_count=Count('channels', distinct=True), |
| 612 | m3u_account_count=Count('m3u_accounts', distinct=True), |
| 613 | ) |
| 614 | .prefetch_related('m3u_accounts') |
| 615 | .all() |
| 616 | ) |
| 617 | |
| 618 | def list(self, request, *args, **kwargs): |
| 619 | queryset = self.filter_queryset(self.get_queryset()) |