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
Django Serverless Cron - Run cron jobs easily in a serverless environment

Django Serverless Cron - Run cron jobs easily in a serverless environment

Paul Onteri 41 Dec 16, 2022
:couple: Multi-user accounts for Django projects

django-organizations Summary Groups and multi-user account management Author Ben Lopatin (http://benlopatin.com / https://wellfire.co) Status Separate

Ben Lopatin 1.1k Jan 01, 2023
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
Django Audit is a simple Django app that tracks and logs requests to your application.

django-audit Django Audit is a simple Django app that tracks and logs requests to your application. Quick Start Install django-audit pip install dj-au

Oluwafemi Tairu 6 Dec 01, 2022
Django API that scrapes and provides the last news of the city of Carlos Casares by semantic way (RDF format).

"Casares News" API Api that scrapes and provides the last news of the city of Carlos Casares by semantic way (RDF format). Usage Consume the articles

Andrés Milla 6 May 12, 2022
A pluggable Django application for integrating PayPal Payments Standard or Payments Pro

Django PayPal Django PayPal is a pluggable application that integrates with PayPal Payments Standard and Payments Pro. See https://django-paypal.readt

Luke Plant 672 Dec 22, 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.

2.2k Jan 02, 2023
Basic Form Web Development using Python, Django and CSS

thebookrain Basic Form Web Development using Python, Django and CSS This is a basic project that contains two forms - borrow and donate. The form data

Ananya Dhulipala 1 Nov 27, 2021
Add Chart.js visualizations to your Django admin using a mixin class

django-admincharts Add Chart.js visualizations to your Django admin using a mixin class. Example from django.contrib import admin from .models import

Dropseed 22 Nov 22, 2022
A Django web application that allows you to be in the loop about everything happening in your neighborhood.

A Django web application that allows you to be in the loop about everything happening in your neighborhood. From contact information of different handyman to meeting announcements or even alerts.

Kennedy Ngugi Mwaura 3 Dec 11, 2022
Django Email Sender

Email-Sender Django Email Sender Installation 1.clone Repository & Install Packages git clone https://github.com/telman03/Email-Sender.git pip install

Telman Gadimov 0 Dec 26, 2021
Cached file system for online resources in Python

Minato Cache & file system for online resources in Python Features Minato enables you to: Download & cache online recsources minato supports the follo

Yasuhiro Yamaguchi 10 Jan 04, 2023
Generate generic activity streams from the actions on your site. Users can follow any actors' activities for personalized streams.

Django Activity Stream What is Django Activity Stream? Django Activity Stream is a way of creating activities generated by the actions on your site. I

Justin Quick 2.1k Dec 29, 2022
Django-powered application about blockchain (bitcoin)

Django-powered application about blockchain (bitcoin)

Igor Izvekov 0 Jun 23, 2022
This is a template tag project for django to calculate in templates , enjoy it

Calculator-Template-Django this is a template tag project for django to calculate in templates , enjoy it Get Started : 1 - Download Source Code 2 - M

1 Feb 01, 2022
✋ Auto logout a user after specific time in Django

django-auto-logout Auto logout a user after specific time in Django. Works with Python 🐍 ≥ 3.7, Django 🌐 ≥ 3.0. ✔️ Installation pip install django-a

Georgy Bazhukov 21 Dec 26, 2022
A Django/Python web app that functions as a digital diary

My Django Diary Full-stack web application that functions as a digital diary using Django, Python, SQLite, HTML & CSS. Things I learned during this pr

1 Sep 30, 2022
simple project management tool for educational purposes

Taskcamp This software is used for educational and demonstrative purposes. It's a simple project management tool powered by Django Framework Install B

Ilia Dmitriev 6 Nov 08, 2022
Django Persistent Filters is a Python package which provide a django middleware that take care to persist the querystring in the browser cookies.

Django Persistent Filters Django Persistent Filters is a Python package which provide a django middleware that take care to persist the querystring in

Lorenzo Prodon 2 Aug 05, 2022
Django Course Project - TextCorrector

Django-TextUtils Django Course Project A tool for analyzing text data in Django backend. It is a project where you can do some of the things with you

1 Oct 29, 2021