2

I want to know if it is possible to add a counter to Django admin app home page,

example:

enter image description here

Can I create a @property in models.py or admin.py for this?

Thanks

2 Answers2

3

You can specialize the string type to add your desired dynamic behavior.

Here is a complete example:

from django.db import models

class VerboseName(str):
    def __init__(self, func):
        self.func = func

    def decode(self, encoding, erros):
        return self.func().decode(encoding, erros)

class UsedCoupons(models.Model):
    name = models.CharField(max_length=10)

    class Meta:
        verbose_name_plural = VerboseName(lambda: u"Used Coupons (%d)" % UsedCoupons.objects.all().count())
Fernando Macedo
  • 2,518
  • 1
  • 24
  • 25
  • Where is VerboseName class? – SaiNageswar S May 01 '19 at 05:52
  • Its displays ` at 0x000002A04F42EE18>`.. If I use it without the lambda function, as in directly setting the `verbose_name_plural` to `UsedCoupons.objects.all().count()`, I get UsedCoupons is not defined. Any hints? – Ronnie Aug 04 '19 at 05:14
0

I think you can't do it in Meta, since it is needs a dynamic value. Also modifying Meta would make the change in all places you use the model, not just the admin. I think the best idea would be to use something like this https://stackoverflow.com/a/6312798/1433029 or use django admin tools https://bitbucket.org/izi/django-admin-tools/

Community
  • 1
  • 1
JoseP
  • 611
  • 3
  • 6
  • I guess this should work, but I want to know if there is an easy way. I cant install the admin tools on the project. Maybe I will try to hack the admin template and load the counter in ajax. – Bruno Rocha - rochacbruno Jan 09 '13 at 21:33