I want to annotate a count field in my queryset with a subquery filter:
My code:
module_attempts = Subquery(
ModuleProgressAttempt.objects.filter(
module_progress__module__id=OuterRef("pk")
).only("pk")
)
real_instances = VirtualClassRoomModule.objects.filter(
id__in=[vc.id for vc in vc_classrooms]
).annotate(
attendees_count=Count(module_attempts),
)
Here module_progress__module__id is the id of the current VirtualClassRoomModule of the annotation iteration. The count is basically the length of the ModuleProgressAttempt filtered queryset. Currently the count is always 1 , eventhough the
ModuleProgressAttempt.objects.filter(
module_progress__module__id=<Current-module-id>
)
returns more than one object.