[Fixed]-Django – Group by column and where on a different column

1👍

Lovely to hear that you got your stuff working, but this answer is 1 explanation short of a reasoning behind this magic. I remembered that this works, but if someone comes up with an explanation WHY it works, then that is the real correct answer. But yeah, if django.db.models.Count doesn’t work as expected, you can replace it with a combination of Sum(Case(When(field='value'), then=1), default=0, output_field=models.IntegerField()) (in Django >= 1.9).

The original comment that solved the problem:

First of all have to state the obvious: are you sure you have any
closed or open ticket requests created in the last month? That would
be the most obvious cause. But if you already double- and
triple-checked that, then I have a vague memory of having a similar
problem somewhere, but can’t remember why. But I fixed it by changing
Count() to Sum(Case(When(set__the__condition=’set_the_value’),
then=1), default=0, output_field=models.IntegerField())).

Leave a comment