GSOC 2023 Discussion: Moving models between apps

Hello everyone,
I am a Pre final student from India and want to contribute to a long standing issue of #24686 (Support for Moving a model between two Django apps) – Django as a part of GSOC 2023. This topic is created so that i can discuss , clear doubts and propose the best possible solution.
There was also some discussion related to moving models between apps but its still an unsolved problem.
Currently models can be moved between the apps using SeparateDatabaseAndState but even that might create problems as mentioned in a comment by @MarkusH .
After going through the discussion on the ticket, i have a question :

  • If we use SeparateDatabaseAndState to move models, can we turn on/off old_app ? as we have to apply different operations in both apps so there are dependencies.

Hi @DevilsAutumn, it’s great that you want to contribute!

Markus essentially answered that question in a later comment. In order to turn off the old app, you’ll have to edit the migration in the new app to make two changes:

  • Remove the dependency on the migration in the old app, and
  • Change the operation from a SeparateDatabaseAndState() which creates the model in new_app only in memory, to a regular CreateModel(); this is required for the whole set of migrations to work correctly on an empty database, when old_app is no longer available.

I should note, though, that while the issue is indeed long-standing, it seems that someone from Brasil has started working on it only a few weeks ago. Have you coordinated with them?

hi @shaib , thanks for the quick reply.
Actually i was doing research on this issue from about a month, trying to reproduce various edge cases with different approaches, but somehow i missed to assign it to myself.
Yes i saw that that someone from brazil started working on it and i’ll surely connect with him so that we can work together to find the best approach.

I have opened a draft PR for what was in my mind .
I have tested the code against some cases on a demo project and it was working fine (even for reverse migration). but there are some cases that need to be tested .
Adding more about the functionality , With this approach we have to move model without changing anything in the model definition. If we made changes to the moving model then those changes will not be detected with the MoveModel operation, running makemigration command again will detect those changes and new migration files will be created.
Only the fields which are referencing the moving model are handled during the MoveModel operation.
We can also modify rename_model() for moving models between apps but i don’t think that such big change will be preferred.
Also I’m still trying to figure out how can we create table for moved model on empty database when the old app is removed from INSTALLED_APPS .
Any feedback in appreciable.

Hi @shaib , It would really helpful if you could please provide a quick feedback so that i know if i’m on the right track or not. :slightly_smiling_face:

@shaib Based on the discussion we had on the PR I created a draft proposal "Allow moving a model between apps" -- proposal for Google Summer of Code 2023. · GitHub .
and would appreciate your input. Would you be able to take a look and provide some feedback?