Skip to main content

Django Aggregate If: Condition aggregates for Django

Test Status Code Helth Latest PyPI version Number of PyPI downloads

Aggregate-if adds 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 model Offer like this one:

class Offer(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:

  1. How many offers exists in total;

  2. How many of them are OPEN, REVOKED or PAID;

  3. How much money was offered in total;

  4. How much money is in OPEN, REVOKED and PAID offers;

To get these informations, you could query:

from django.db.models import Count, Sum

Offer.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 queries were needed to retrieve the desired information.

With conditional aggregates you can get it all with only 1 query:

from django.db.models import Q
from aggregate_if import Count, Sum

Offer.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))
)

Installation

Aggregate-if works with Django 1.4, 1.5, 1.6 and 1.7.

To install it, simply:

$ pip install django-aggregate-if

Inspiration

There is a 5 years old ticket 11305 that 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 on ticket 11305, so when the new feature is available you can easily replace django-aggregate-if.

Limitations

Conditions involving joins with aliases are not supported yet. If you want to help adding this feature, you’re welcome to check the first issue.

Contributors

Changelog

0.5
  • Support for Django 1.7

0.4
  • Use tox to run tests.

  • Add support for Django 1.6.

  • Add support for Python3.

  • The only parameter 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.1
  • Fix quotation escaping.

  • Fix boolean casts on Postgres.

0.2
  • Fix postgres issue with LIKE conditions.

0.1
  • Initial release.

License

The MIT License.

Metadata

Release files for django-aggregate-if 0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-aggregate-if 0.5
File Size Uploaded Provenance
django-aggregate-if-0.5.tar.gz 5.1 kB Without provenance Details

Release files / django-aggregate-if-0.5.tar.gz

Download URL django-aggregate-if-0.5.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ae2f991c5faf7d48068b6a16ee7649ff333d77d06c8d14d143d5ba67420acf3c
BLAKE2b-256 checksum
How to use checksums
39b51eb6e244add6ed4caf09797bb6bd1a82b07bcfc940251ec98a9e6639ad7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for django-aggregate-if 0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page