How to move a model between two Django apps Django 1.7
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Moving a model from one Django app to another is mostly a migration-planning problem, not a copy-paste problem. In Django 1.7, the migration system existed, but it did not have many of the convenience tools available in later versions. The safest strategy is to preserve the existing database table, move the Python import path carefully, and keep the migration history explicit.
Decide Whether the Database Table Should Move
In many projects, you do not actually want to rename the database table at all. You only want the model class to live in a different app. That distinction matters because a Python move is much cheaper than a database-table move.
If the existing table is already populated, the practical default is:
- move the model code to the new app
- point the new model at the old table name
- adjust foreign keys and imports
- create migrations that reflect the state change without destroying data
That avoids risky bulk data copying when the table itself can stay where it is.
Start With a Concrete Example
Assume the model starts in old_app/models.py:
In a fresh Django 1.7 project, the default table name would be something like old_app_invoice.
Move the Model Code Into the New App
Create the same model in new_app/models.py, but pin the database table to the old table name:
This is the key move. The class now lives in new_app, but it still reads and writes the existing table. That means existing rows are preserved.
Update References Before Running Migrations
Every place that imports the model must now use the new app path.
Examples:
Also check:
- admin registrations
- foreign key declarations
- signals
- forms
- serializers
- raw imports inside management commands or tests
If you miss these, the app can boot with a split view of the same concept.
Handle Foreign Keys Carefully
If other models point at the old class, update them to point at the new app label.
After changing the relation target, create migrations for those dependent apps too. In Django 1.7, migration order matters more than people expect, so review dependencies manually if necessary.
Migration Strategy in Django 1.7
Because Django 1.7 is older, the safest approach is usually explicit state control rather than hoping the autodetector understands your intent.
Typical workflow:
If Django tries to create a brand-new table for new_app.Invoice, that is a sign the migration state does not match your intended preserved-table strategy. In older Django versions, you may need to edit the generated migration so it reflects the model state you want without dropping or recreating the table incorrectly.
When a Physical Table Rename Is Required
Sometimes you do want the database table name to match the new app label. That is a separate operation and should be treated separately from the Python move.
The rough sequence is:
- move the model class
- temporarily keep
db_tablepointed at the old table - once code is stable, rename the database table with SQL or a migration step
- update
db_tableor remove it if the default name is now correct
Keeping those steps separate reduces the blast radius.
Verify the Move in the Shell
Before declaring success, verify that the new model reads existing data:
If the count matches the original data, the move preserved the table binding correctly.
Common Pitfalls
- Moving the model class without preserving the original table name.
- Letting Django generate a fresh table when the goal was only to move the Python model.
- Updating the model location but forgetting foreign keys and imports in other apps.
- Trying to combine Python refactor, table rename, and data migration into one risky step.
- Assuming the Django 1.7 migration autodetector fully understands model moves across apps.
Summary
- In Django 1.7, moving a model between apps is safest when the existing table is preserved first.
- Use
Meta.db_tablein the new app to point at the original table. - Update all imports and relation targets to the new app path.
- Treat table renaming as a separate step from model relocation.
- Verify the result in the shell before considering the migration complete.
Related reading
- How to Multi-thread an Operation Within a Loop in Python
- How to normalize a NumPy array to within a certain range?
- How to obtain a Thread id in Python?
- How to obtain a Thread id in Python?
- How to obtain information gain from a scikit-learn DecisionTreeClassifier?
- How to open a file using the open with statement
- How to optimize MAPE code in Python?
- How to output a comma delimited list in jinja python template?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.