Login

Tag "aggregation"

Snippet List

Aggregation class "Count" with Case

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
Read More

Aggregation class "Sum" with Case

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'), )

  • annotate
  • aggregation
  • sum
Read More

Generating aggregate data across generic relations

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

  • django
  • gfk
  • aggregation
Read More

3 snippets posted so far.