dowjones123
dowjones123

Reputation: 3847

Dynamic verbose name of model in Django Admin

I have a HelpTicket model, and I want the main Django admin page to show the Verbose Name as HelpTickets (2 unclosed) where HelpTicket.objects.count(closed=False) = 2

Is there an easy way of dynamically over-riding the verbose name in this fashion?

enter image description here

Upvotes: 0

Views: 1730

Answers (2)

rptmat57
rptmat57

Reputation: 3787

Here is another option:

def update_verbose_name(model_class, name):
    model_class._meta.verbose_name_plural = name

to use it:

new_name = f"Help Tickets ({HelpTicket.objects.count(closed=False))} unclosed)"
update_verbose_name(HelpTicket, new_name)

in your case you could use it with a post_save and post_delete signal to keep it up to date.

Upvotes: 1

moonstruck
moonstruck

Reputation: 2784

You can use proxy model here.

class ShowHelpTicket(HelpTicket):
    class Meta:
        proxy = True
        verbose_name_plural = "Help Tickets ( " + str(HelpTicket.objects.count(closed=False)) + "unclosed )"

Here you'll find a nice tutorial. Using Proxy Models to Customize the Django Admin

Upvotes: 3

Related Questions