Django Naming Conventions

Today is a sort of cheatsheet for properly formatting a Django codebase. There are 2 main issues that I see newer devs getting wrong, the formatting of names and when to use singular vs plural (there may be others, but that will be enough for today!)

Any Python class should be PascalCase, functions and methods should snake_case, variables should be snake_case. An example:


class BlogPage(models.Model):
  a_long_title = models.CharField(max_length=255)

  def a_method_to_render_the_blog(self):
    # do something

def a_function_to_do_something():
  # do something

The second issue of plural vs singular is that most class definitions should be singular, especially model classes as this represents a table in SQL and instances represent many rows, so should be plural.

class BlogPages(models.Model):
  # ...

class BlogPage(models.Model):
  # ...