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
TinyApp - A Python (Django) Full Stack Application for shortening URLs

TinyApp A Python (Django) Full Stack Application for shortening URLs. How to sta

Li Deng 1 Jan 23, 2022
A simple porfolio with Django, Bootstrap and Sqlite3

Django Portofolio Example this is a basic portfolio in dark mode Installation git clone https://github.com/FaztWeb/django-portfolio-simple.git cd djan

Fazt Web 16 Sep 26, 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
Dockerizing Django with Postgres, Gunicorn, Nginx and Certbot. A fully Django starter project.

Dockerizing Django with Postgres, Gunicorn, Nginx and Certbot πŸš€ Features A Django stater project with fully basic requirements for a production-ready

8 Jun 27, 2022
Automatic caching and invalidation for Django models through the ORM.

Cache Machine Cache Machine provides automatic caching and invalidation for Django models through the ORM. For full docs, see https://cache-machine.re

846 Nov 26, 2022
Integarting Celery with Django to asynchronous tasks πŸ“ƒ

Integrating πŸ”— Celery with Django via Redis server ,To-Do asynchronously πŸ‘€task without stopping the main-flow πŸ“ƒ of Django-project . It increase your speed πŸš€ and user experience 🀡 of website

Rushi Patel 4 Jul 15, 2022
A visual indicator of what environment/system you're using in django

A visual indicator of what environment/system you're using in django

Mark Walker 4 Nov 26, 2022
Blog focused on skills enhancement and knowledge sharing. Tech Stack's: Vue.js, Django and Django-Ninja

Blog focused on skills enhancement and knowledge sharing. Tech Stack's: Vue.js, Django and Django-Ninja

Wanderson Fontes 2 Sep 21, 2022
A Django app that allows visitors to interact with your site as a guest user without requiring registration.

django-guest-user A Django app that allows visitors to interact with your site as a guest user without requiring registration. Largely inspired by dja

Julian Wachholz 21 Dec 17, 2022
This is a repository for collecting global custom management extensions for the Django Framework.

Django Extensions Django Extensions is a collection of custom extensions for the Django Framework. Getting Started The easiest way to figure out what

Django Extensions 6k Dec 26, 2022
Django channels basic chat

Django channels basic chat

Dennis Ivy 41 Dec 24, 2022
Helps working with singletons - things like global settings that you want to edit from the admin site.

Django Solo +---------------------------+ | | | | | \ | Django Solo helps

Sylvain ToΓ© 726 Jan 08, 2023
A helper for organizing Django project settings by relying on well established programming patterns.

django-configurations django-configurations eases Django project configuration by relying on the composability of Python classes. It extends the notio

Jazzband 953 Dec 29, 2022
Store events and publish to Kafka

Create an event from Django ORM object model, store the event into the database and also publish it into Kafka cluster.

Diag 6 Nov 30, 2022
Displaying objects on maps in the Django views and administration site.

DjangoAdminGeomap library The free, open-source DjangoAdminGeomap library is designed to display objects on the map in the Django views and admin site

Vitaly Bogomolov 31 Dec 28, 2022
A Minimalistic Modern Django Boilerplate

A Minimalistic Modern Django Boilerplate This boilerplate is mainly for educational purposes. It is meant to be cloned as a starter code for future tu

Jonathan Adly 21 Nov 02, 2022
A set of functions related with Django

django-extra-tools Table of contents Installation Quick start Template filters parse_datetime parse_date parse_time parse_duration Aggregation First L

Tomasz Jakub Rup 3 Mar 04, 2020
mirage ~ β™ͺ extended django admin or manage.py command.

mirage ~ β™ͺ extended django admin or manage.py command. ⬇️ Installation Installing Mirage with Pipenv is recommended. pipenv install -d mirage-django-l

Shota Shimazu 6 Feb 14, 2022
Bootstrap 4 integration with Django.

django-bootstrap 4 Bootstrap 4 integration for Django. Goal The goal of this project is to seamlessly blend Django and Bootstrap 4. Requirements Pytho

Zostera B.V. 980 Dec 29, 2022
E-Commerce Platform

Shuup Shuup is an Open Source E-Commerce Platform based on Django and Python. https://shuup.com/ Copyright Copyright (c) 2012-2021 by Shuup Commerce I

Shuup 2k Jan 07, 2023