proper naming convention in Django

699 views Asked by At

I am using Django 1.11 and on that I have created an app country and inside country/models.py I have a model Country.

class Country(models.Model):
    name = models.CharField(max_length=100)
    code = models.CharField(max_length=50)
    pub_date = models.DateTimeField('date published')

and included the model in admin.py

from django.contrib import admin
from country.models import Country

# Register your models here.
admin.site.register(Country)

When I visited http://127.0.0.1:8000/admin, it shows admin template

The spelling of country has been pluralized to Countrys which is wrong.

What is proper naming convention to prevent such mistakes?

2

There are 2 answers

3
Astik Anand On BEST ANSWER

It is not the spelling mistake , It's django convention to show that there will be many records and to show that it adds an s at last after the Model Name.

0
KyleDing On

PART I : Countrys is your model name, you can set verbosename to define it.

class Country(models.Model):
    name = models.CharField(max_length=100)
    code = models.CharField(max_length=50)
    pub_date = models.DateTimeField('date published')
    class Meta:
        verbose_name = "country"
        verbose_name_plural = "countries"

PART II: COUNTRY is your app name, in Django 11.1, you can set app name in apps.py

# in yourapp/apps.py
from django.apps import AppConfig

class YourAppConfig(AppConfig):
    name = 'yourapp'
    verbose_name = 'Fancy Title'