Chart.js integration for Django admin models
Project description
django-admincharts
Add Chart.js visualizations to your Django admin using a mixin class.
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
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-admincharts-0.2.1.tar.gz
.
File metadata
- Download URL: django-admincharts-0.2.1.tar.gz
- Upload date:
- Size: 72.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.2 Linux/5.11.0-1028-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e22f741958558dce4fb820b509cf8b99171e3c20243e3b1286989c276b1a374d |
|
MD5 | ea7d7dbd392594a6d6bf253066cd527d |
|
BLAKE2b-256 | 031d37569df99c7337ec64f5f779fbffea8b6352e50b5e86c95a45e54acb6a29 |
File details
Details for the file django_admincharts-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: django_admincharts-0.2.1-py3-none-any.whl
- Upload date:
- Size: 73.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.2 Linux/5.11.0-1028-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7eb9ff7e96265b3d4d0f1691ad02b08aa2ed11f59c17a1cf44f9f5049aa753e7 |
|
MD5 | 227ce037a6934658a795e328b4fd977b |
|
BLAKE2b-256 | 5504c5c78e5ac40f0467dee646d52ce19be36ed48dc79db818427c07a2a80a40 |