"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Move Django Models Between Apps Using Migrations?

How to Move Django Models Between Apps Using Migrations?

Published on 2024-11-08
Browse:570

How to Move Django Models Between Apps Using Migrations?

Moving Models Between Django Apps with Migrations

Organizing models into separate Django app structures can enhance project maintainability and structure. While previous attempts using South may have proved difficult, Django 1.7's migration system offers a seamless solution.

Procedure

Using migrations.SeparateDatabaseAndState allows you to concurrently modify the database and update the state of the model:

  1. Removing from the Old App:

    • Create an empty migration for the old app using makemigrations.
    • Define the SeparateDatabaseAndState operation in the migration to rename the table, delete the model from the old app's state, and add the model to the new app's state.
  2. Adding to the New App:

    • Copy the model file to the new app.
    • Create a migration for the new app using makemigrations.
    • Enclose the existing CreateModel operation in a SeparateDatabaseAndState operation, ensuring the migration depends on the old app's migration.
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3