0π
So after trying a lot, this was a solution that worked:
Bar.objects.values("foo_id").annotate(Count("foo_id")).filter(pk__count__gt=1)
Not exactly sure why this worked and the other didnβt, but it essentially just gets the count of Bar
objects with the same foo_id
and makes sure there is more than 1.
If somebody would like to explain a potential reason why this works and the other did not, that would be appreciated.
1π
I had the same issue by import mistake. This is solution for my use case
# from django.db.models.sql.aggregates import Count # wrong import
from django.db.models import Count # correct one
0π
There can be multiple reasons for this error, Iβll try to tell the probable causes:
- Recently upgrading your django version may cause the problem, clear
migrations, rerun. - Moving from local server to production server can cause this problem
sometimes. - Your app name can cause the problem, if it starts with β__β.
- Try other simpler queries, if they work, try to change the query so
that you donβt use thenum_model
. -
also check if you can get the count of them ( the dirty way π ):
for foo in Foo.objects.all():
if foo.bar_set.count() < 2:
#do sth like : foo.bar_set.get() or temp = temp + 1
as of your short description of model (not your main code), cannot find other causes. Your query should work.