Skip to content

django-unordered-body-content-in-model (DJ012)#

Derived from the flake8-django linter.

What it does#

Checks for the order of Model's inner classes, methods, and fields as per the Django Style Guide.

Why is this bad?#

The Django Style Guide specifies that the order of Model inner classes, attributes and methods should be as follows:

  1. All database fields
  2. Custom manager attributes
  3. class Meta
  4. def __str__()
  5. def save()
  6. def get_absolute_url()
  7. Any custom methods

Examples#

from django.db import models


class StrBeforeFieldModel(models.Model):
    class Meta:
        verbose_name = "test"
        verbose_name_plural = "tests"

    def __str__(self):
        return "foobar"

    first_name = models.CharField(max_length=32)
    last_name = models.CharField(max_length=40)

Use instead:

from django.db import models


class StrBeforeFieldModel(models.Model):
    first_name = models.CharField(max_length=32)
    last_name = models.CharField(max_length=40)

    class Meta:
        verbose_name = "test"
        verbose_name_plural = "tests"

    def __str__(self):
        return "foobar"