I have model A with a fk to model B
Now I need to make a queryset that gets all B’s with less then 5 A’s linked to them.
Edit Maybe I should have mentioned this is a Django project and what I’m looking for is a Queryset reference thats efficient enough to be run quite a number of times
i’ve tried something like:
B.objects.select_related()
but I don’t understand how to limit this qs to only include the B’s with max 5 occurrences
Use annotations:
See: https://docs.djangoproject.com/en/dev/topics/db/aggregation/