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
The Django Leaflet Admin List package provides an admin list view featured by the map and bounding box filter for the geo-based data of the GeoDjango.

The Django Leaflet Admin List package provides an admin list view featured by the map and bounding box filter for the geo-based data of the GeoDjango. It requires a django-leaflet package.

Vsevolod Novikov 33 Nov 11, 2022
django-quill-editor makes Quill.js easy to use on Django Forms and admin sites

django-quill-editor django-quill-editor makes Quill.js easy to use on Django Forms and admin sites No configuration required for static files! The ent

lhy 139 Dec 5, 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
📊📈 Serves up Pandas dataframes via the Django REST Framework for use in client-side (i.e. d3.js) visualizations and offline analysis (e.g. Excel)

Django REST Pandas Django REST Framework + pandas = A Model-driven Visualization API Django REST Pandas (DRP) provides a simple way to generate and se

wq framework 1.2k Jan 1, 2023
Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project

Django URL Shortener Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project Install this package to your Dja

Rishav Sinha 4 Nov 18, 2021
An app that allows you to add recipes from the dashboard made using DJango, JQuery, JScript and HTMl.

An app that allows you to add recipes from the dashboard. Then visitors filter based on different categories also each ingredient has a unique page with their related recipes.

Pablo Sagredo 1 Jan 31, 2022
Django application and library for importing and exporting data with admin integration.

django-import-export django-import-export is a Django application and library for importing and exporting data with included admin integration. Featur

null 2.6k Dec 26, 2022
Django admin CKEditor integration.

Django CKEditor NOTICE: django-ckeditor 5 has backward incompatible code moves against 4.5.1. File upload support has been moved to ckeditor_uploader.

null 2.2k Jan 2, 2023
Django admin CKEditor integration.

Django CKEditor NOTICE: django-ckeditor 5 has backward incompatible code moves against 4.5.1. File upload support has been moved to ckeditor_uploader.

null 2.2k Dec 31, 2022
📝 Sticky Notes in Django admin

django-admin-sticky-notes Share notes between superusers. Installation Install via pip: pip install django_admin_sticky_notes Put django_admin_sticky_

Dariusz Choruży 7 Oct 6, 2021
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
Add infinite scroll to any django app.

django-infinite-scroll Add infinite scroll to any django app. Features - Allows to add infinite scroll to any page.

Gustavo Teixeira 1 Dec 26, 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 4, 2023
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
Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.

Django-environ django-environ allows you to use Twelve-factor methodology to configure your Django application with environment variables. import envi

Daniele Faraglia 2.7k Jan 7, 2023
Rosetta is a Django application that eases the translation process of your Django projects

Rosetta Rosetta is a Django application that facilitates the translation process of your Django projects. Because it doesn't export any models, Rosett

Marco Bonetti 909 Dec 26, 2022
pytest-django allows you to test your Django project/applications with the pytest testing tool.

pytest-django allows you to test your Django project/applications with the pytest testing tool.

pytest-dev 1.1k Dec 14, 2022
django-dashing is a customisable, modular dashboard application framework for Django to visualize interesting data about your project. Inspired in the dashboard framework Dashing

django-dashing django-dashing is a customisable, modular dashboard application framework for Django to visualize interesting data about your project.

talPor Solutions 703 Dec 22, 2022
Django-Audiofield is a simple app that allows Audio files upload, management and conversion to different audio format (mp3, wav & ogg), which also makes it easy to play audio files into your Django application.

Django-Audiofield Description: Django Audio Management Tools Maintainer: Areski Contributors: list of contributors Django-Audiofield is a simple app t

Areski Belaid 167 Nov 10, 2022