0

I read different threads and tutorials on renaming and migrating a Django app (How to rename a Django app and migrate data from an app to the other for instance), I also tried to use the django package called django-rename-app but it didn't work at all.

I'm going to explain the way I renamed my app in my development environment and I would like to know if you expect troubles, as apparently everything is working just as it did before so I'm considering doing the same in production.

Here are the steps I took inside my root project directory and my virtualenv activated, using Django 3.2:

Create the destination app

django-admin startapp app2

Copy these files and directories

cp app1/*.py app2/

mkdir app2/templates

mkdir app2/static

cp app1/templates app2/templates

in this case I had a sub-folder named after the app so I renamed it like this

mv app2/templates/app1 app2/templates/app2

cp app1/static app2/static

Models.py

Change the related_name attributes of all ForeignKeys in my new models.py and update all their references inside my views, admin, form and templates

Make sure your apps won't imported by the same name

Look for the instanciation of an AppConfig class inside your app directory and if you find something like this:

from django.apps import AppConfig

class AnunciosConfig(AppConfig):
    name = 'app1'

that's where you need to also change your app name from app1 to app2

Activate the new app

Add my app2 to the list of the enabled apps of settings.py

Delete __pycache__

Find and delete or empty all the __pycache__ directories in app2

Migrations

this will not work until all the overlapping bits are corrected, so this is how I tested everything was ok (it will not be aware of URLs problem at this stage)

python manage.py makemigrations

python manage.py migrate

Cloning data

Now I used the Django shell to migrate data from app1 to the app2

python manage.py shell

import app1

from app2.models import *

So I will have to use "app1.ModelName" to refer to the old app and simply "ModelName" to address the new app2 instances

Then I use the Models and QuerySet API to just create new objects in app2 with for loops for each app1 instances, with a corresponding app2 object that is saved (this needs be written individually and depends on each situation)

Deactivate the old app

Now I remove or comment out the old app1 reference in settings.py so that it is disabled

Rename the directory

At this point I needed to rename the app1/ directory to something unknown to Django

mv app1 app1_

Deal with the new errors

It seems some more errors appeared here with URLS and Views, so I had to just update app1 references to app2 until they disappeared

Delete Database table

Enter MySQL (in my case), and disable FOREIGN_KEY_CHECKS

USE yourowndjangodatabase;

SHOW TABLES;

SET FOREIGN_KEY_CHECKS=0;

so now I can delete them like this

DROP TABLE app1__yourfirstmodel;

DROP TABLE app1__yoursecondmodel;

and so on until none is left, then set the FOREIGN_KEY_CHECKS back to 1 so that it is enabled

SET FOREIGN_KEY_CHECKS=1;

Like I saw here Can't drop table: A foreign key constraint fails

CONCLUSION

And basically that was it, it worked perfectly for my development environment. I have a perfect copy of my previous app1 and web application that is just using app2 now.

I improvised so I'm not sure this is the right procedure but it worked so I would like to ask if you see any problem or suspect there will be issues in production.

H3x
  • 47
  • 6
  • I found a problem: when I run "python manage collectstatic" the directory created inside /static/ is still named after app1. I suspect it may have something to do with the virtualenvironment cache like I read here https://stackoverflow.com/questions/8408046/how-to-change-the-name-of-a-django-app?noredirect=1&lq=1 – H3x Jul 04 '22 at 19:02
  • I deleted and recreated the python virtualenvm even used different names, I also deleted all the __pycache__ dirs again but there still is a reference to app1 somewhere – H3x Jul 04 '22 at 19:11
  • So I edited the database table django_content_type where there still was a reference to app1, the models very tables had been deleted previously, deleted all the __pycache__ and also deleted all migration files. Recreated the virtualenv, but still "collectstatic" creates a folder called like app1. I'm using "grep -r" on the project root directory and it doesn't come up. Any idea where this name is retrieved from? – H3x Jul 04 '22 at 19:25
  • I even tried to create a clone of the project, import the apps folders and activate them, then migrate. I still see "collectstatic" create an "app1" folder inside /static so I suspect the problem may be elsewhere, maybe some python cache I have no access to – H3x Jul 05 '22 at 09:59
  • By "clone" I mean a new django project that I imported my apps in (already renamed) – H3x Jul 06 '22 at 08:07

0 Answers0