[Django]-Django admin shows models name with extra "s" at the end

5๐Ÿ‘

โœ…

Why is django-admin adding an โ€˜sโ€™

There is a naming convention in Django the models are named in the singular. So your classes should be named:

class Comment(models.Model):
   ...

class Note(models.Model):
   ...

An instance of such a class represents a single comment, not a set of comments. Likewise consider the following

# doesn't really make sense
Comments.objects.get(...)

# reads much better
Comment.objects.get(...) #e.g. get from the comment objects

This is so much of a convention that Django admin expects for your classes to be named in the singular. For this reason, Django adds on an โ€˜sโ€™ as a standard way of pluralising your class names. (This will admittedly not work for all words and is very anglo-centric, but as an override, Django provides the verbose_name_plural meta-attribute).

How to fix it:

Rename your classes so they are in the singular. This will make your code a lot more readable to anyone who is used to reading django. It will also make your models named in a way consistent with other third-party packages. If your class names are not in English, or do not pluralise simply by appending an s, use verbose_name_plural e.g:

Class Cactus(models.Model):
    class Meta:  
        verbose_name_plural = 'Cacti'
๐Ÿ‘คtim-mccurrach

6๐Ÿ‘

Add a meta class to your model and set verbose_name_plural (https://docs.djangoproject.com/en/3.2/ref/models/options/#verbose-name-plural). Ideally name your classes as singular (in this case youโ€™d have class Comment(models.Model).

class Comments(models.Model):


    class Meta:
        
        verbose_name_plural = 'Comments'

Good read:
https://simpleisbetterthancomplex.com/tips/2018/02/10/django-tip-22-designing-better-models.html#:~:text=Always%20name%20your%20models%20using,not%20a%20collection%20of%20companies.

๐Ÿ‘คAMG

Leave a comment