Search code examples
djangodjango-migrationsdjango-apps

Why does Django create migrations in the parent model's app when they are inherited?


I have the following setup, an external dependency dep is installed and being used in my Django project and I'm extending one of it's models in my own apps, like the following:

from dep.models import ParentModel

class MyModel(ParentModel):
    # fields

Both mine and the dependency models are not abstract and by running makemigrations a new migration is created into the dep app, following it's current migration tree.

The problem is that I expect to have a new migration in my own app as it doesn't make sense to mess with the dependency's migration structure as it's gonna create all sorts of problems whenever it's updated.

Is there a way to go around this?

I've tried moving the migrations manually to my app but when I run makemigrations again, it gets deleted and created again the same way as I mentioned above.


Solution

  • I actually figured it out and the point is that the inherited model's Meta class has the app_label explicitly set which is also inherited by my model, forcing it to be a migration on the same app.

    To fix it, I just had to set the same attributes in my model and now the migration is created as I expected.

    Here's the example of the base model and mine:

    class ParentModel(models.Model):
        class Meta:
            app_label = "dep"
    
    
    class MyModel(ParentModel):
        class Meta:
            app_label = "my_app"