[Fixed]-Django dynamic name values in model overview on admin site

1👍

Introspecting the django admin code I found out that unfortunately the model class is not being passed to the template context – so you cannot easily query for object count. What is passed to the template for every model is:

model_dict = {
    'name': capfirst(model._meta.verbose_name_plural),
    'object_name': model._meta.object_name,
    'perms': perms,
}

What you could do is override AdminSite._build_app_dict to include the model class itself, override the default index template and the just query in the template using:

{{ model_class.objects.count }}

The default template is admin/index.html but can be configured on per admin site basis as you can see in the above mentioned class.

Leave a comment