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
This is a basic Todo Application API using Django Rest Framework

Todo Application This is a basic Todo Application API using Django Rest Framework. Todo Section - User can View his previously added todo items, creat

Atharva Parkhe 1 Aug 09, 2022
Simple yet powerful and really extendable application for managing a blog within your Django Web site.

Django Blog Zinnia Simple yet powerful and really extendable application for managing a blog within your Django Web site. Zinnia has been made for pub

Julien Fache 2.1k Dec 24, 2022
A middleware to log the requests and responses using loguru.

Django Loguru The extension was based on another one and added some extra flavours. One of the biggest problems with the apps is the logging and that

Tiago Silva 9 Oct 11, 2022
Imparare Django ricreando un sito facsimile a quello Flask

SitoPBG-Django Imparare Django ricreando un sito facsimile a quello Flask Note di utilizzo Necessita la valorizzazione delle seguenti variabili di amb

Mario Nardi 1 Dec 08, 2021
A small and lightweight imageboard written with Django

Yuu A small and lightweight imageboard written with Django. What are the requirements? Python 3.7.x PostgreSQL 14.x Redis 5.x FFmpeg 4.x Why? I don't

mint.lgbt 1 Oct 30, 2021
Sistema administrador de contranas desarrollador en Django

Sistema Contrasenas Desarrolado en Django Proyecto sistema de administracion de contraseñas, de la experiencia educativa Programacion Segura Descripci

Ibrain Rodriguez Espinoza 1 Sep 24, 2022
Django CRUD REST API Generator

Django CRUD REST API Generator This is a simple tool that generates a Django REST API with the given models. Specs: Authentication, DRF generic views,

Mehmet Alp Sümer 57 Nov 24, 2022
Easy thumbnails for Django

Easy Thumbnails A powerful, yet easy to implement thumbnailing application for Django 1.11+ Below is a quick summary of usage. For more comprehensive

Chris Beaven 1.3k Dec 30, 2022
Store model history and view/revert changes from admin site.

django-simple-history django-simple-history stores Django model state on every create/update/delete. This app supports the following combinations of D

Jazzband 1.8k Jan 06, 2023
Django REST Client API

Django REST Client API Client data provider API.

Ulysses Monteiro 1 Nov 08, 2021
Developer-friendly asynchrony for Django

Django Channels Channels augments Django to bring WebSocket, long-poll HTTP, task offloading and other async support to your code, using familiar Djan

Django 5.5k Jan 06, 2023
Forgot password functionality build in Python / Django Rest Framework

Password Recover Recover password functionality with e-mail sender usign Django Email Backend How to start project. Create a folder in your machine Cr

alexandre Lopes 1 Nov 03, 2021
Ugly single sign-on for django projects only

django-usso Ugly single sign-on for django projects only. Do you have many django apps with different users? Do you want to use only one of those apps

Erwin Feser 1 Mar 01, 2022
A simple Django middleware for Duo V4 2-factor authentication.

django-duo-universal-auth A lightweight middleware application that adds a layer on top of any number of existing authentication backends, enabling 2F

Adam Angle 1 Jan 10, 2022
Django Girls Tutorial Workshop

Django Girls Tutorial Workshop A log of activities during the workshop. this is an H2 git remote add origin https://github.com/ahuimanu/django_girls_t

Jeffry Babb 1 Oct 27, 2021
Django's class-based generic views are awesome, let's have more of them.

Django Extra Views - The missing class-based generic views for Django Django-extra-views is a Django package which introduces additional class-based v

Andy Ingram 1.3k Jan 04, 2023
Backend with Django .

BackendCode - Cookies Documentation: https://docs.djangoproject.com/fr/3.2/intro/ By @tcotidiane33 & @yaya Models Premium class Pack(models.Model): n

just to do it 1 Jan 28, 2022
A Django app to initialize Sentry client for your Django applications

Dj_sentry This Django application intialize Sentry SDK to your Django application. How to install You can install this packaging by using: pip install

Gandi 1 Dec 09, 2021
Declarative model lifecycle hooks, an alternative to Signals.

Django Lifecycle Hooks This project provides a @hook decorator as well as a base model and mixin to add lifecycle hooks to your Django models. Django'

Robert Singer 1k Dec 31, 2022
A package to handle images in django

Django Image Tools Django Image Tools is a small app that will allow you to manage your project's images without worrying much about image sizes, how

The Bonsai Studio 42 Jun 02, 2022