1👍
✅
You don’t need to annotate this, you can .prefetch_related(…)
[Django-doc] this with:
annotated_parent_set = Parent.objects.prefetch_related('child_set')
or if you want to use .brothers
and .sisters
, then you can work with two Prefetch
objects [Django-doc]:
from django.db.models import Prefetch
annotated_parent_set = Parent.objects.prefetch_related(
Prefetch('child_set', Child.objects.filter(status=MALE), to_attr='brothers'),
Prefetch('child_set', Child.objects.filter(status=FEMALE), to_attr='sisters')
)
Source:stackexchange.com