Find more articles in our new Python Knowledge Base

Importing django.db.models.fields

In Django, models are defined in django.db.models.fields. However, for convenience they are imported into django.db.models. Django’s standard convention is to use from django.db import models and refer to fields as models<some>Field. To improve readability and maintainability of your code, change your import statement and model definition.

Anti-pattern

from django.db.models import fields

class Person(models.Model):
    first_name = fields.CharField(max_length=30)
    last_name = fields.CharField(max_length=30)

Best practice

Stick to standard conventions and use from django.db import models instead.

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)