I have a quite common use-case in which i need to count some rows and annotate the result to the base model grouped by the modelinformation itself.
MyModel.objects
.filter(name='xyz')
.values('id', 'name')
.annotate(count=Count('relation__nested_relation', distinct=True))
Now i do not want to count every object in the nested relation, but need to count the objects in a special timespan.
MyModel.objects
.filter(name='xyz', relation__nested_relation__time__gt=mytime)
.values('id', 'name')
.annotate(count=Count('relation__nested_relation', distinct=True))
At this point every instance of MyModel
not containing a record for the nested_relation
will be dropped. Instead i want to have every MyModel
record but with count=0
annotated.
In SQL i would append the filter for the nested_relation
in the ON clause of the join. But it seems like django does not support the modification of the on clause. Any other ideas how i can filter my count statement? My database backend is PostgreSQL and Count(When(relation__nested_relation__time__gt=mytime, then=1), distinct=True)
raises an SQL syntax exception.