[Django]-Django-filter and aggregate functions

8👍

You mean something like this:

from django.db.models import Avg


class ProductFilter(django_filters.FilterSet):
    ...

    @property
    def avg(self):
        qs = super(ProductFilter, self).qs

        return qs.aggregate(Avg('price'))['id__avg']

So you’re adding your own filter property and use it like this in your template:

{{ filter.avg }}

Leave a comment