Opened 3 years ago

Last modified 2 years ago

#25765 new Cleanup/optimization

makemigrations and app_label are incompatible

Reported by: ograycode Owned by: nobody
Component: Documentation Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Repo steps:

  • Create a new app called "testapp"
  • Add one model to the models.py with a meta class and app_label which are different then your current one.
  • Run "manage.py makemigrations testapp"

On the third step, it will state that there are no changes. If you remove the custom app_label and run makemigrations again it will pick up on the new model. If you change app_label once again, it will see that as a deletion of the model.

I believe that it should either be explicitly documented, or that app_label should have no bearing on makemigrations.

Change History (1)

comment:1 Changed 3 years ago by Marten Kenbeek

Component: UncategorizedDocumentation
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

The behaviour is correct. By changing the app_label, you are effectively moving the model to a different app without changing the file location. If the new app label is the label of a valid Django app, you will be able to create migrations for that model using the explicitly defined app_label.

Django currently does not support moving a model from one application to another. That's why it sees the model as a new model when you remove the explicit app label, and sees it as a deleted model when you change the app label again. The new app label you define is not recognized as a valid app, so it is treated as an unmigrated app (or the model might be ignored by manage.py migrate - something to figure out as well).

We can expand the description of Meta.app_label and link to AppConfig.label to clarify that changing the app_label also changes the app in which the model lives according to Django. The django-admin page has a short section about the "app name" used by various commands. I suggest we change the terminology to app label to clarify its meaning and be consistent with the rest of the documentation. This section could also be a bit more elaborate.

Note: See TracTickets for help on using tickets.
Back to Top