djangosnippets.org: Latest snippets tagged with 'annotate'https://djangosnippets.org/tags/annotate/2010-07-06T20:14:01.730892-05:00Aggregation class "Count" with Case
2010-07-06T20:14:01.730892-05:00marinhohttps://djangosnippets.org/snippets/2100/<p>Use it like below:</p>
<pre><code>totals = MyClass.aggregate(
is_enabled_yes=CountCase('is_enabled', when=True),
is_enabled_no=CountCase('is_enabled', when=False),
count_if=CountCase('id', case="another_field in ('a','b')", when=True),
)
</code></pre>
Freely redistributableAggregation class "Sum" with Case
2010-07-06T20:09:40.550892-05:00marinhohttps://djangosnippets.org/snippets/2099/<p>Just use it like below:</p>
<pre><code>from downloaded_file import SumCase
MyClass.objects.aggregate(
sum1=SumCase('salary', case='salary < 4', when=True),
sum1=SumCase('salary', case='type', when='director'),
)
</code></pre>
Freely redistributableAnnotate queryset with comment count
2008-10-01T03:58:26.980760-05:00fivethreeohttps://djangosnippets.org/snippets/1101/<p>Code example</p>
<pre><code>queryset = Event.objects.all().select_related('user', 'category')
queryset = comments_extra_count(queryset)
</code></pre>
Freely redistributable