# models.py
from django.db import models
class Elephant(models.Model):
location = models.ForeignKey(Location, on_delete=models.CASCADE)
families = models.ForeignKey(Families, on_delete=models.CASCADE)
def add_families_and_locations_counts(elephants):
return elephants.annotate(
families_number=Count('families'),
location_number=Count('location')
)
# Run
output = add_families_and_locations_counts(elephants)
In the above, the counts are incorrect. How do I get the correct counts?