Consider these pseudo classes:
class Foo(models.Model):
pass
class Bar(models.Model):
foo = models.ForeignKey(Foo)
I would like to filter via Foo's manager effectively to get a QuerySet that only holds Foo objects with atleast 2 Bar objects pointing towards it.
Use aggregation with Count
:
from django.db.models import Count
Foo.objects.annotate(bar_count=Count('bar')).filter(bar_count__gte=2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With