Add a counter to Django admin home

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

Upvotes: 2

Views: 1287

Answers (2)

Fernando Macedo
Fernando Macedo

Reputation: 2528

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())

Upvotes: 3

JoseP
JoseP

Reputation: 641

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/

Upvotes: 0

Related Questions