Package: django-aggregate-if
Django Aggregate If: Condition aggregates for DjangoAggregate-ifadds conditional aggregates to Django.Conditional aggregates can help you reduce the ammount of queries to obtain
aggregated information, like statistics for example.Imagine you have a modelOfferlike this one:classOffer(models.Model):sponsor=models.ForeignKey(User)price=models.DecimalField(max_digits=9,decimal_places=2)status=models.CharField(max_length=30)expire_at=models.DateField(null=True,blank=True)created_at=models.DateTimeField(auto_now_add=True)updated_at=models.DateTimeField(auto_now=True)OPEN="OPEN"REVOKED="REVOKED"PAID="PAID"Let’s say you want to know:How many offers exists in total;How many of them are OPEN, REVOKED or PAID;How much money was offered in total;How much money is in OPEN, REVOKED and PAID offers;To get these informations, you could query:fromdjango.db.modelsimportCount,SumOffer.objects.count()Offer.objects.filter(status=Offer.OPEN).aggregate(Count('pk'))Offer.objects.filter(status=Offer.REVOKED).aggregate(Count('pk'))Offer.objects.filter(status=Offer.PAID).aggregate(Count('pk'))Offer.objects.aggregate(Sum('price'))Offer.objects.filter(status=Offer.OPEN).aggregate(Sum('price'))Offer.objects.filter(status=Offer.REVOKED).aggregate(Sum('price'))Offer.objects.filter(status=Offer.PAID).aggregate(Sum('price'))In this case,8 querieswere needed to retrieve the desired information.With conditional aggregates you can get it all with only1 query:fromdjango.db.modelsimportQfromaggregate_ifimportCount,SumOffer.objects.aggregate(pk__count=Count('pk'),pk__open__count=Count('pk',only=Q(status=Offer.OPEN)),pk__revoked__count=Count('pk',only=Q(status=Offer.REVOKED)),pk__paid__count=Count('pk',only=Q(status=Offer.PAID)),pk__sum=Sum('price'),pk__open__sum=Sum('price',only=Q(status=Offer.OPEN)),pk__revoked__sum=Sum('price'),only=Q(status=Offer.REVOKED)),pk__paid__sum=Sum('price'),only=Q(status=Offer.PAID)))InstallationAggregate-ifworks with Django 1.4, 1.5, 1.6 and 1.7.To install it, simply:$pipinstalldjango-aggregate-ifInspirationThere is a 5 years oldticket 11305that will (hopefully) implement this feature into
Django 1.8.Using Django 1.6, I still wanted to avoid creating custom queries for very simple
conditional aggregations. So I’ve cherry picked those ideas and others from the
internet and built this library.This library uses the same API and tests proposed onticket 11305, so when the
new feature is available you can easily replacedjango-aggregate-if.LimitationsConditions involving joins with aliases are not supported yet. If you want to
help adding this feature, you’re welcome to check thefirst issue.ContributorsHenrique BastosIuri de SilvioHampus StjernhavBradley MartsbergerMarkus Bertheauend0Scott SextonMaulertrbsChangelog0.5Support for Django 1.70.4Use tox to run tests.Add support for Django 1.6.Add support for Python3.Theonlyparameter now freely supports joins independent of the main query.Adds support for alias relabeling permitting excludes and updates with aggregates filtered on remote foreign key relations.0.3.1Fix quotation escaping.Fix boolean casts on Postgres.0.2Fix postgres issue with LIKE conditions.0.1Initial release.LicenseThe MIT License.