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, changelist):
...
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.
To ignore pagination but still respect search/filter,
you can override the get_list_chart_queryset method to return the full queryset:
class MyModelAdmin(AdminChartMixin, admin.ModelAdmin):
def get_list_chart_queryset(self, changelist):
return changelist.queryset
And if you want, you can also sidestep the list queryset entirely by using overriding get_list_chart_queryset with your own query:
class MyModelAdmin(AdminChartMixin, admin.ModelAdmin):
def get_list_chart_queryset(self, changelist):
return MyModel.objects.all()
Metadata
Release files for django-admincharts 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-admincharts-0.4.1.tar.gz | 73.6 kB | Without provenance | Details for django-admincharts-0.4.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_admincharts-0.4.1-py3-none-any.whl | Python 3 | none | any | Details for django_admincharts-0.4.1-py3-none-any.whl |
Total release size: 147.5 kB
Release files / django-admincharts-0.4.1.tar.gz
| Download URL | django-admincharts-0.4.1.tar.gz |
|---|---|
| Size | 73.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
27b9d00be1df4ef47c28b514a7ad705094b29d9017e3cf847baf261ab54081eb
|
|
BLAKE2b-256 checksum How to use checksums |
14d17f4bafc466cf0a5989c407b331064d8e74701ae238221429fe440e9dfb22
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.14 CPython/3.10.5 Linux/5.15.0-1014-azure
|
Release files / django_admincharts-0.4.1-py3-none-any.whl
| Download URL | django_admincharts-0.4.1-py3-none-any.whl |
|---|---|
| Size | 73.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
79cdd184eae4ebb3f1be0e009a69cb8f70345c9b02a5beffacb167cb760178a4
|
|
BLAKE2b-256 checksum How to use checksums |
1bbfb1b167d9cde7c053429cee7b590db0b24bcb52407950353d9f16e5d2754f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.14 CPython/3.10.5 Linux/5.15.0-1014-azure
|
Security for django-admincharts 0.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.