Skip to main content

A Django app that extends the admin interface with group-by functionality

Project description

Django Admin Group-By

Django Admin Group-By adds SQL-style GROUP BY functionality to the Django admin, letting you easily group and summarize data with aggregations like counts, sums, and averages directly in the admin interface.

It works by adding a "Group by" filter in the admin sidebar, allowing you to select fields and instantly see summarized views of your data.

Key Features

  • Group Data Directly in Admin: Easily group by model fields to quickly identify patterns.
  • Built-in Aggregations: Perform counts, sums, averages and more including advanced custom aggregations.
  • Custom Calculations: Aggregate calculated fields with lambda functions that run after database queries.
  • Compatible: Integrates seamlessly with Django admin filters, search, and permissions.
  • Efficient: Performs aggregations server-side, suitable for large datasets.

Installation

Install the package directly from GitHub:

pip install git+https://github.com/numegil/django-admin-groupby.git

Add the app to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    # ...
    'django.contrib.admin',
    # ...
    'django_admin_groupby',  # Add this line
]

Example Usage

Here's an example demonstrating both basic grouping and advanced aggregations:

from django.contrib import admin
from django.db.models import Count, Sum, Avg, Q
from django_admin_groupby.admin import GroupByAdminMixin
from django_admin_groupby import PostProcess
from .models import Product

@admin.register(Product)
class ProductAdmin(GroupByAdminMixin, admin.ModelAdmin):
    list_display = ('name', 'category', 'price', 'in_stock')
    list_filter = ('category', 'in_stock')

    group_by_fields = ['category', 'in_stock']

    group_by_aggregates = {
        'id': {
            'count': Count('id', extra={'verbose_name': "Total Products"}),
            'in_stock_count': Count('id', filter=Q(in_stock=True),
                                    extra={'verbose_name': "In-Stock Products"}),
        },
        'price': {
            'avg': Avg('price', extra={'verbose_name': "Average Price"}),
            'sum': Sum('price', extra={'verbose_name': "Total Value"}),
            'expensive_items': Count('id', filter=Q(price__gte=100),
                                     extra={'verbose_name': "Expensive Items (>= $100)"}),
        },
        'profit_margin': {
            'total': PostProcess(
                lambda product: product.price - product.cost,
                verbose_name="Profit Margin",
                aggregate="avg"
            )
        }
    }

Demo

Screenshot 2025-05-27 at 9 23 08 PM

A demo project is included in the repository to illustrate usage:

git clone https://github.com/numegil/django-admin-groupby.git
cd django-admin-groupby

# (optional)
python -m venv venv
source venv/bin/activate

pip install -e .
cd example
python manage.py migrate
python manage.py generate_cats --count 100
python manage.py createsuperuser
python manage.py runserver

Access the demo at http://localhost:8000/admin/cats/cat/.

License

MIT License. Feel free to use and modify this code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_admin_groupby-1.0.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_admin_groupby-1.0.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file django_admin_groupby-1.0.0.tar.gz.

File metadata

  • Download URL: django_admin_groupby-1.0.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for django_admin_groupby-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2ba03207c919a5cf636e1a866dbeb5897dd1c877f0b935104da1c6bb4906a268
MD5 8192282d27408caee26e222d9ee4d6b9
BLAKE2b-256 bca645aa7b8b3acd3ed2461cd2289f86bf60a4641faf0df1f0e4b8589bced12c

See more details on using hashes here.

File details

Details for the file django_admin_groupby-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_groupby-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 082af264446e83a1aed7e12da7d7e77b5c55008765576e10c1a6a08e005c078e
MD5 7e521e29410c6179a1b26a56272f1260
BLAKE2b-256 6a3e1af844d95b774d1c7001599a3e9839ff7c1b55590ee586bfcea6e299923b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page