Snippet List
Use it like below:
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),
)
- count
- annotate
- aggregation
Just use it like below:
from downloaded_file import SumCase
MyClass.objects.aggregate(
sum1=SumCase('salary', case='salary < 4', when=True),
sum1=SumCase('salary', case='type', when='director'),
)
http://github.com/coleifer/django-generic-aggregation
http://charlesleifer.com/blog/generating-aggregate-data-across-generic-relations/
Generate and calculate aggregations across generic foreign keys.
>>> from misc import generic_annotate, generic_aggregate
>>> from blog.models import Entry
>>> from tagging.models import TaggedItem
>>> from django.db.models import Count
>>> qs = generic_annotate(Entry.objects.all(), TaggedItem.object, 'id', Count)
>>> qs[0].score
5L
>>> qs[1].score
4L
>>> qs[1].tags
u'databases django many-to-many python'
>>> generic_aggregate(Entry.objects.all(), TaggedItem.object, 'id', Count)
106L # total number of times entries were tagged
3 snippets posted so far.