Add Chart.js visualizations to your Django admin using a mixin class

Overview

django-admincharts

Add Chart.js visualizations to your Django admin using a mixin class.

Example

django-admincharts example

from django.contrib import admin

from .models import BillingAccount
from admincharts.admin import AdminChartMixin
from admincharts.utils import months_between_dates


@admin.register(BillingAccount)
class BillingAccountAdmin(AdminChartMixin, admin.ModelAdmin):
    def get_list_chart_data(self, queryset):
        if not queryset:
            return {}

        # Cannot reorder the queryset at this point
        earliest = min([x.ctime for x in queryset])

        labels = []
        totals = []
        for b in months_between_dates(earliest, timezone.now()):
            labels.append(b.strftime("%b %Y"))
            totals.append(
                len(
                    [
                        x
                        for x in queryset
                        if x.ctime.year == b.year and x.ctime.month == b.month
                    ]
                )
            )

        return {
            "labels": labels,
            "datasets": [
                {"label": "New accounts", "data": totals, "backgroundColor": "#79aec8"},
            ],
        }

Installation

Install from pypi.org:

$ pip install django-admincharts

Add admincharts to your Django INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "admincharts",
]

Use the AdminChartMixin with an admin.ModelAdmin class to add a chart to the changelist view.

Options can be set directly on the class:

from django.contrib import admin
from admincharts.admin import AdminChartMixin

@admin.register(MyModel)
class MyModelAdmin(AdminChartMixin, admin.ModelAdmin):
    list_chart_type = "bar"
    list_chart_data = {}
    list_chart_options = {"aspectRatio": 6}
    list_chart_config = None  # Override the combined settings

Or by using the class methods which gives you access to the queryset being used for the current view:

class MyModelAdmin(AdminChartMixin, admin.ModelAdmin):
    def get_list_chart_queryset(self, result_list):
        ...

    def get_list_chart_type(self, queryset):
        ...

    def get_list_chart_data(self, queryset):
        ...

    def get_list_chart_options(self, queryset):
        ...

    def get_list_chart_config(self, queryset):
        ...

The type, data, and options are passed directly to Chart.js to render the chart. Look at the Chart.js docs to see what kinds of settings can be used.

By default, the objects in your chart will be the objects that are currently visible in your list view. This means that admin controls like search and list filter will update your chart, and you can use the Django pagination settings to control how many objects you want in your chart at a time. If you want, you can also sidestep the list queryset entirely by using overriding get_list_chart_queryset.

Comments
  • Only the objects in current list page is shown.

    Only the objects in current list page is shown.

    If I have a model with 150 objects, only first 100 of them are included in chart. And I browse to next page, following 50 records are included. Is this by design?

    If not, using .queryset instead of .result_list on line 51 of admincharts/admin.py fixes the issue for me.

    enhancement question 
    opened by byenilmez 5
  • Update poetry.lock (black and django)

    Update poetry.lock (black and django)

    The following dependencies have been updated by deps:

    • poetry.lock was updated (including 2 direct and 6 transitive dependencies)
      • black was updated from 22.6.0 to 22.10.0
      • django was updated from 4.0.6 to 4.1.3
    opened by deps-dropseed[bot] 0
  • Update poetry.lock (django)

    Update poetry.lock (django)

    The following dependencies have been updated by deps:

    • poetry.lock was updated (including 1 direct and 1 transitive dependencies)
      • django was updated from 4.0.5 to 4.0.6
    opened by deps-dropseed[bot] 0
  • Release version 0.4.1

    Release version 0.4.1

    These commits are new since version 0.4.0:

    • a198bcb Update poetry.lock (django) (#35)
    • 62ed9d5 Update chart.js in package.json from 3.8.0 to 3.8.2 (#34)
    • 4106593 Update poetry.lock (black and django) (#32)

    To release the new version:

    • [ ] Label this PR (ex. release: major)
    • [ ] Update the changelog on this branch (optional)
    • [ ] Merge this PR (commit message should start with Release version {version})
    release: patch 
    opened by github-actions[bot] 0
  • Update poetry.lock (black and django)

    Update poetry.lock (black and django)

    The following dependencies have been updated by deps:

    • poetry.lock was updated (including 2 direct and 1 transitive dependencies)
      • black was updated from 22.3.0 to 22.6.0
      • django was updated from 4.0.4 to 4.0.5
    opened by deps-dropseed[bot] 0
  • Release version 0.4.0

    Release version 0.4.0

    These commits are new since version 0.3.1:

    • 9412c18 Update poetry.lock (1 transitive dependencies) (#28)
    • 77c614f Update chart.js in package.json from 3.7.1 to 3.8.0 (#29)

    To release the new version:

    • [ ] Label this PR (ex. release: major)
    • [ ] Update the changelog on this branch (optional)
    • [ ] Merge this PR (commit message should start with Release version {version})
    release: minor 
    opened by github-actions[bot] 0
  • Release version 0.3.1

    Release version 0.3.1

    These commits are new since version 0.3.0:

    • e756c64 Fix error on pages without charts

    To release the new version:

    • [ ] Label this PR (ex. release: major)
    • [ ] Update the changelog on this branch (optional)
    • [ ] Merge this PR (commit message should start with Release version {version})
    release: patch 
    opened by github-actions[bot] 0
  • Release version 0.3.0

    Release version 0.3.0

    These commits are new since version 0.2.1:

    • 3f1d46c Change get_list_chart_queryset arg to changelist (#26)
    • d10a12c Update poetry.lock (black and django) (#23)

    To release the new version:

    • [ ] Label this PR (ex. release: major)
    • [ ] Update the changelog on this branch (optional)
    • [ ] Merge this PR (commit message should start with Release version {version})
    release: minor 
    opened by github-actions[bot] 0
  • Update poetry.lock (black and django)

    Update poetry.lock (black and django)

    The following dependencies have been updated by deps:

    • poetry.lock was updated (including 2 direct and 5 transitive dependencies)
      • black was updated from 22.1.0 to 22.3.0
      • django was updated from 4.0.3 to 4.0.4
    opened by deps-dropseed[bot] 0
  • Update poetry.lock (django)

    Update poetry.lock (django)

    The following dependencies have been updated by deps:

    • poetry.lock was updated (including 1 direct and 5 transitive dependencies)
      • django was updated from 3.2.11 to 3.2.12
    opened by deps-dropseed[bot] 0
  • Release version 0.2.1

    Release version 0.2.1

    These commits are new since version 0.2.0:

    • 91b69d5 Use nextrelease v2
    • dbebffb Format with black
    • 87bb343 Fix redirect context_data attribute error
    • 0830052 Remove Python 3.7 and test Django 4.0
    • 4a573dd Update poetry.lock (django) (#22)
    • 438024e Update chart.js in package.json from 3.7.0 to 3.7.1 (#21)
    • 2b35cea Update poetry.lock (django) (#18)
    • 7d5305b Update black in pyproject.toml from ^21.6b0 to ^22.1.0 (#19)

    To release the new version:

    • [ ] Label this PR (ex. release: major)
    • [ ] Update the changelog on this branch (optional)
    • [ ] Merge this PR (commit message should start with Release version {version})
    release: patch 
    opened by github-actions[bot] 0
  • Update poetry.lock (black and django)

    Update poetry.lock (black and django)

    The following dependencies have been updated by deps:

    • poetry.lock was updated (including 2 direct and 3 transitive dependencies)
      • black was updated from 22.10.0 to 22.12.0
      • django was updated from 4.1.3 to 4.1.4
    opened by deps-dropseed[bot] 0
  • Release version <next>

    Release version

    These commits are new since version 0.4.1:

    • dc0e315 Update poetry.lock (black and django) (#36)
    • a18f3ed Update chart.js in package.json from 3.8.2 to 3.9.1 (#37)

    To release the new version:

    • [ ] Label this PR (ex. release: major)
    • [ ] Update the changelog on this branch (optional)
    • [ ] Merge this PR (commit message should start with Release version {version})
    opened by github-actions[bot] 0
Releases(v0.4.1)
Owner
Dropseed
Building software to make you more effective.
Dropseed
Automatic class scheduler for Texas A&M written with Python+Django and React+Typescript

Rev Registration Description Rev Registration is an automatic class scheduler for Texas A&M, aimed at easing the process of course registration by gen

Aggie Coding Club 21 Nov 15, 2022
Simple alternative to Doodle polls and scheduling (Python 3, Django 3, JavaScript)

What is jawanndenn? jawanndenn is a simple web application to schedule meetings and run polls, a libre alternative to Doodle. It is using the followin

Sebastian Pipping 169 Jan 06, 2023
A Redis cache backend for django

Redis Django Cache Backend A Redis cache backend for Django Docs can be found at http://django-redis-cache.readthedocs.org/en/latest/. Changelog 3.0.0

Sean Bleier 1k Dec 15, 2022
Intellicards-backend - A Django project bootstrapped with django-admin startproject mysite

Intellicards-backend - A Django project bootstrapped with django-admin startproject mysite

Fabrizio Torrico 2 Jan 13, 2022
Django datatables and widgets, both AJAX and traditional. Display-only ModelForms.

Django datatables and widgets, both AJAX and traditional. Display-only ModelForms. ModelForms / inline formsets with AJAX submit and validation. Works with Django templates.

Dmitriy Sintsov 132 Dec 14, 2022
A BitField extension for Django Models

django-bitfield Provides a BitField like class (using a BigIntegerField) for your Django models. (If you're upgrading from a version before 1.2 the AP

DISQUS 361 Dec 22, 2022
Visual DSL framework for django

Preface Processes change more often than technic. Domain Rules are situational and may differ from customer to customer. With diverse code and frequen

Dmitry Kuksinsky 165 Jan 08, 2023
Per object permissions for Django

django-guardian django-guardian is an implementation of per object permissions [1] on top of Django's authorization backend Documentation Online docum

3.3k Jan 04, 2023
A better and faster multiple selection widget with suggestions

django-searchable-select A better and faster multiple selection widget with suggestions for Django This project is looking for maintainers! Please ope

Andrew Dunai 105 Oct 22, 2022
A calendaring app for Django. It is now stable, Please feel free to use it now. Active development has been taken over by bartekgorny.

Django-schedule A calendaring/scheduling application, featuring: one-time and recurring events calendar exceptions (occurrences changed or cancelled)

Tony Hauber 814 Dec 26, 2022
Inject an ID into every log message from a Django request. ASGI compatible, integrates with Sentry, and works with Celery

Django GUID Now with ASGI support! Django GUID attaches a unique correlation ID/request ID to all your log outputs for every request. In other words,

snok 300 Dec 29, 2022
A starter template for building a backend with Django and django-rest-framework using docker with PostgreSQL as the primary DB.

Django-Rest-Template! This is a basic starter template for a backend project with Django as the server and PostgreSQL as the database. About the templ

Akshat Sharma 11 Dec 06, 2022
Thumbnails for Django

Thumbnails for Django. Features at a glance Support for Django 2.2, 3.0 and 3.1 following the Django supported versions policy Python 3 support Storag

Jazzband 1.6k Jan 03, 2023
Build reusable components in Django without writing a single line of Python.

Build reusable components in Django without writing a single line of Python. {% #quote %} {% quote_photo src="/project-hail-mary.jpg" %} {% #quot

Mitchel Cabuloy 277 Jan 02, 2023
Yummy Django API, it's the exclusive API used for the e-yummy-ke vue web app

Yummy Django API, it's the exclusive API used for the e-yummy-ke vue web app

Am.Chris_KE 1 Feb 14, 2022
Probably the best abstract model / admin for your tree based stuff.

django-treenode Probably the best abstract model / admin for your tree based stuff. Features Fast - get ancestors, children, descendants, parent, root

Fabio Caccamo 360 Jan 05, 2023
AUES Student Management System Developed for laboratory works №9 Purpose using Python (Django).

AUES Student Management System (L M S ) AUES Student Management System Developed for laboratory works №9 Purpose using Python (Django). I've created t

ANAS NABIL 2 Dec 06, 2021
Extensions for using Rich with Django.

django-rich Extensions for using Rich with Django. Requirements Python 3.6 to 3.10 supported. Django 2.2 to 4.0 supported. Are your tests slow? Check

Adam Johnson 88 Dec 26, 2022
Redia Cache implementation in django.

django-redis Recipe APP Simple Recipe app which shows different kinds off recipe to the user. Why Cache ? Accessing data from cache is much faster tha

Avinash Alanjkar 1 Sep 21, 2022
Reusable, generic mixins for Django

django-braces Mixins for Django's class-based views. Documentation Read The Docs Installation Install from PyPI with pip: pip install django-braces Bu

Brack3t 1.9k Jan 05, 2023