[Answered ]-Querying Django object for number of One-to-Many related objects

2👍

Not sure about your schema but in my example a Car object has a Fk to User calles ‘user’:

User.objects.annotate(num_cars=Count(‘cars’)).filter(num_cars=1)
This gives you the users that have only one car.

http://docs.djangoproject.com/en/dev/topics/db/aggregation/#joins-and-aggregates

👤endre

Leave a comment