Model method order

The Django coding style suggests a standard order of model attributes. Increasing standardisation and predictability of your code style helps other developers read your code.

The Django Coding Style suggests the following order of inner classes, methods and attributes:

  1. Field choice tuples
  2. Database fields
  3. Custom manager attributes
  4. class Meta
  5. def __str__()
  6. def save()
  7. def delete
  8. def get_absolute_url()
  9. Any custom methods

This standardisation is very useful when Python should be written to be read.

If we spot this issue in your GitHub pull request we give this advice:

models.pymaintainabilitylow
class CommentModel(models.Model):
+
    body = models.TextField()
+
    class Meta:
        ordering = ('body',)
-
 
-
    body = models.TextField()

The Django coding style suggests a standard order of model attributes. Increasing standardisation and predictability of your code style helps other developers read your code.

Read more
Protect your pull requests from over 40 types of common Django technical debts with our GitHub code review bot.

Configuring this check

Django Doctor will run this check by default. No configuration is needed but the check can be turned on/off using check code model-method-order in your pyproject.toml file.

Read more about configuring Django Doctor.