Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin: How can I override a model verbose_name?

I have two models (Country and State) and I've made only one ModelAdmin for Country with an TabularInline for State.

class StateInline(admin.TabularInline):
    model = State

class CountryAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {'fields': ['name']}),
    ]
    inlines = [StateInline]

admin.site.register(Country, CountryAdmin)

How can I override the verbose_name Meta property of Country model? I want to rename the form in menu to "Countries / States", instead of only "Countries"?

like image 944
jrvidotti Avatar asked Oct 28 '13 15:10

jrvidotti


People also ask

What is the use of Verbose_name in Django model?

verbose_name is a human-readable name for the field. If the verbose name isn't given, Django will automatically create it using the field's attribute name, converting underscores to spaces. This attribute in general changes the field name in admin interface.

How do I automatically register all models in Django admin?

To automate this process, we can programmatically fetch all the models in the project and register them with the admin interface. Open admin.py file and add this code to it. This will fetch all the models in all apps and registers them with the admin interface.

How to register model in admin in Django?

from django. contrib import admin # Register your models here. Register the models by copying the following text into the bottom of the file. This code imports the models and then calls admin.

What is list_ display in Django?

It provides a simple UI for creating, editing and deleting data defined with the Django ORM. In this article we are going to enable the admin user interface for a simple model and customize it from a simple list view to a more user friendly table like interface.


1 Answers

using Meta

in your model:

from django.utils.tranlation import gettext_lazy as _

class Country(models.Model):
    # your fields
    class Meta:
        verbose_name = _("Country / State")
        verbose_name_plural = _("Countries / States")
like image 61
Leandro Avatar answered Sep 21 '22 18:09

Leandro