Importing models.py into migrations

It's better to use apps.get_model, which guarantees the Model's fields will reflect the fields in the database even if models.py is vastly out of step with the database.

The fields in Django's models.py must agree with the schema in the database. When Django performs database read or write operations it uses the fields in your models.py to determine the name of the database fields to SELECT and INSERT.

If models.py includes fields that are not yet in the database schema then the database will throw an error.

If our GitHub code review bot spots this issue in your pull request it gives this advice:

django-doctorbotsuggested changes just now
tasks.py
1
+
def forwards(apps, schema_editor):
2
+
    MyModel.objects.get(...)

It's better to use apps.get_model, which guarantees the Model's fields will reflect the fields in the database even if models.py is vastly out of step with the database.

Read more
Suggested changes
-
    MyModel.objects.get(...)
+
    apps.get_model("core", "MyModel").objects.get(...)
Commit suggestion
Update tasks.py

Instantly check if you have this issue for free

    Works with tools you use

    Read about how it works.