moving models between apps, fast and easy

I created an SO post because they have the best UI, but I’m hoping to get feedback from the Django experts here.

It seems like a great shortcut, yet I haven’t come across anyone else suggesting it in my web searching. Am I missing some obvious problem with this approach? (other than a minute of downtime)

While this will work in a lot of cases, there are probably as many cases where this doesn’t go far enough.

To be more specific, this:

  1. Update all references in the code to the previous locations. (Including migration files.)

may not be such a simple operation.

You may have code or libraries involved with permissions and content type management storing metadata for objects using natural keys. You may have other data stored in models that use the app label for dynamic model reference.

Again, I’m not saying it won’t work - and how you’ve described it here is certainly accurate. All I’m saying is that there are many situations where it’s not as simple as you’re making it sound.

It’s not always going to be as easy as a global “search and replace” of your app name string in your source code - and that’s why it really can’t be recommended as a general one-size-fits-all approach. And that is why the referenced blog identifies multiple methods and why some of those methods can be described as “onerous” - because they need to be.

The only way to identify a method that is going to work will be with knowledge of the application and the data being managed by it.

1 Like

UPDATE: Just remembered - I’d also have to:

5.5) Run a script to update app for all rows in the in django_migrations table (easy part), and to renumber all migrations to form a single sequence (hard part), as well as modifying all migration files to match new sequence.

I appreciate you iterating edge cases. Fortunately, I’m pretty familiar with our project, and I believe I can identify the few places where app name plays a role.

As for the limitations of search-and-replace, I agree - fortunately, I’m a big fan of brute force, and greatly enjoy spending hours manually making a thousand similar modifications with careful attention to context while enjoy a video on the other screen, especially over a weekend so I can get the PR merged first thing Monday morning.

Given the need to re-sequence migrations, combined with having already been wanting to squash our huge migration stack, lead me to this new and improved recipe:

V2 - Full Reset

  1. Move models to new app. (Let’s call it “newapp”.)
  2. Delete previous migrations.
  3. Update code to reflect new model locations.
  4. Run makemigrations to generate single migration in new app with all models.
  5. Turn site off.
  6. Run script to rename my tables from prevapp_model to newapp_model.
  7. Run script to update django_content_type.app_label.
  8. Run script to replace contents of django_migrations with single row indicating migration newapp-0001 has already run.
  9. Deploy new version.
  10. Turn site back on.

PS - I now regret starting this thread with a link to SO instead of duplicating the content here. The thread would be far more coherent. My bad.