Dynamic Charts
Learn to add dynamic charts in your Django admin interface.
We'll cover the following...
Dynamic charts
The charts in the previous lesson look nice, but it would be nicer to implement real dynamic charts and not hardcoded ones.
When rendering the change_list.html
page, Django uses a method called
changelist_view()
. You can override this method in your AuthorAdmin
class to implement your business logic. As an example, you will display the number of authors updated by date in the AuthorAdmin
class defined inside sample_app/admin.py
.
def changelist_view(self, request, extra_context=None):
# Aggregate new authors per day
chart_data = (
Author.objects.annotate(date=TruncDay("updatedDate"))
.values("date")
.annotate(y=Count("id"))
.order_by("-date")
)
# Serialize and attach the chart data to the template context
as_json = json.dumps(list(chart_data), cls=DjangoJSONEncoder)
print("Json %s"%as_json)
extra_context = extra_context or {"chart_data": as_json}
# Call the superclass changelist_view to render the page
return super().changelist_view(request, extra_context=extra_context)
When querying your data, then you can convert them as JSON and add them as extra_context
in your template. ...