#24686: Support for Moving a model between two Django apps
-------------------------------+-------------------------------------------
Reporter: Alex Rothberg | Owner: Durval Carvalho
Type: New feature | Status: assigned
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+-------------------------------------------
Comment (by Durval Carvalho):
I would like to ask for feedback on my approach. I have found what I
believe to be the best way to move a model from one app to another in
Django. It involves four separate operations in at least 3 different
migrations:
**1.** Rename table operation: renames the physical table name on the
database. This operation should be a SeparateDatabaseAndState operation
and only change the database, without changing any state.
**2.** Create table operation: moves the model code to the new app, and
creates a CreateModel only in state, with a SeparateDatabaseAndState
operation.
**3.** Update all foreign keys that point to the moved model.
**4.** Delete the old model table, but only in state, using a
SeparateDatabaseAndState operation.
I've implemented this logic in a new method called "generate_moved_models"
in MigrationAutodetector. I've discovered that the current implementation
only handles simple ForeignKeys and not more complex cases like
ManyToManyFields. This is because the current approach generates at least
one AlterField with the fields ManyToOneRel and ManyToManyField, which
cannot be serialized at the time of writing operations to the migration
files.
I'm still trying to figure out the best approach to tackle this issue. I'm
unsure whether extending the serializer_factory to support these types of
fields or creating a new operation (probably "MoveModel") similar to
RenameModel would be the better solution. I noticed that in the
RenameModel the operations related to column renaming in the M2M tables
are done in the database_forwards method using the _alter_many_to_many.
If anyone in the Django community has any suggestions or experience with
this issue, I would greatly appreciate any input. Thank you!
--
Ticket URL: <https://code.djangoproject.com/ticket/24686#comment:22>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/010701867ab0c462-7cf01273-6a81-4d27-b3e1-bcec281866d1-000000%40eu-central-1.amazonses.com.