Adjusting the Plural of a Model in Django Admin

Recently I had the following model that held a list of countries:

class Country(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=50)
    code = models.CharField(max_length=50)

And my admin file was like this:

from django.contrib import admin
from .models import *

admin.site.register(Country)

Problem was in the Django Admin the incorrect plural was rendered:

Now as a person of style and class it is important to have the right plural. Grammar is important – or do we need to talk about Uncle Jack and his horse again?

The solution is simple! Implement the Meta class within the model and give verbose_name_plural a value:

class Country(models.Model):
    """ The countries we are operating in """
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=50)
    code = models.CharField(max_length=50)

    class Meta:
        verbose_name_plural = "countries"

And the Django Admin will fix its previous lack of education:

And now we are back to civilisation! Whew! that was a close one!

Documentation can be found in the Model Meta Options page.

And seriously people – this is what makes Django so great – things like this are representative of Django’s long history. You won’t see the newer frameworks with this sort of feature built in.

Tags:

Leave a Reply