django-generic-aggregation

annotate() and aggregate() for generically related data
Download

django-generic-aggregation Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Price:
  • FREE
  • Publisher Name:
  • Charles Leifer
  • Publisher web site:
  • http://www.charlesleifer.com/blog/

django-generic-aggregation Tags


django-generic-aggregation Description

annotate() and aggregate() for generically related data django-generic-aggregation provides annotate() and aggregate() for generically-related data.ExamplesYou want the most commented on blog entries:>>> from django.contrib.comments.models import Comment>>> from django.db.models import Count>>> from blog.models import BlogEntry>>> from generic_aggregation import generic_annotate>>> annotated = generic_annotate(BlogEntry.objects.all(), Comment.content_object, 'id', Count)>>> for entry in annotated:... print entry.title, entry.scoreThe most popular 5The second best 4Nobody commented 0You want to figure out which items are highest rated:from django.db.models import Sum, Avg# assume a Food model and a generic Rating modelapple = Food.objects.create(name='apple')# create some ratings on the foodRating.objects.create(content_object=apple, rating=3)Rating.objects.create(content_object=apple, rating=5)Rating.objects.create(content_object=apple, rating=7)>>> aggregate = generic_aggregate(Food.objects.all(), Rating.content_object, 'rating', Sum)>>> print aggregate15>>> aggregate = generic_aggregate(Food.objects.all(), Rating.content_object, 'rating', Avg)>>> print aggregate5Check the tests - there are more examples there. Tested with postgres & sqlite Requirements: · Python · Django


django-generic-aggregation Related Software