Changing a Django app name
Disclaimer: This post is for anyone who wants to read it, but I’ll use some technical examples and words, so if you don’t understand something, just don’t mind it.
Update log
What changed
- In the code the accounts app is called accounts instead of users.
- The path to anything related to accounts is now /accounts/ instead of /users/ (which was annoying because the default pages by allauth were accounts and overridden ones were users).
What’s new
- Nothing.
Friction log
I was so annoyed…
Having my accounts app called users was so frustrating to me, and I really thought it’d be close to impossible to change it (especially this app because the app for accounts must be the first thing in a Django generated Database, it ISN’T possible to add a custom user app AFTER the project was launched, or to be exact, it should be possible (it’s computer science, c’mon…), but not a good practice at all and should be avoided at all costs, it can cause problems (or maybe the Django devs just want to scare people because they like a good laugh, Idk lol)), so I thought renaming it would cause the same problems as creating it after the first migrations, you know, kinda like renaming a file tracked by git, it’s counted as a removed old name file and a new renamed file, except this time it would actually be bad and not just a little bit confusing.
What did I do about it?
For a long time I was just “eh, it’s not that bad, I’ll get over it”, but it wasn’t the case, and since I thought I couldn’t do anything about it, I just didn’t search for a solution, I had a few ideas myself:
- Saving all the data
- Removing the users app
- Creating accounts app
- Adding the data from old users app
- Making the migrations linked to that
- Making the rest of the migrations
- Adding the data from before to the all the respective apps
- Done
- Changing stuff
- Trying
- Noticing that there’s an error, thank God Django gives good error messages
- Using the error messages to detect and erase the error
- goto: 2. “Trying”
Would probably work after a day or two
And well, a few other dumb ideas, but the one I chose was searching the web because I knew for a fact that someone once had the same problem, and I found the StackOverflow post linked to this post, I’ll explain the steps here again though (just because that way next time it happens to me I’ll know where to find the answer).
A-New-Hope Way
- Rename the folder of the app you want to rename.
- Change its name everywhere it’s referenced (you can use, in most Code Editors, Ctrl+Shift+F, more commonly called “search everywhere” function, or you can use the “Show / Find References” in some editors too, note that something can technically be a reference but if you don’t use a good Python interpreter it won’t find everything, that’s where search everywhere comes in handy).
- Edit the database content using an SQL Editor (I often use DB Browser for SQLite to edit SQLite DBs, but you could also use Beekeeper Studio or modelDBA for other DB engines (not SQLite at the moment, the dev is currently working on adding it though), or of course any other DB editor you want)
The code for this step is
SQL 1
UPDATE django_content_type SET app_label='<NewAppName>' WHERE app_label='<OldAppName>'
And in my case was
SQL 1
UPDATE django_content_type SET app_label='accounts' WHERE app_label='users';
- Then you have to rename the models with the new app name before the underscore (common Database stuff)
The code for this step is
SQL 1
ALTER TABLE <oldAppName>_modelName RENAME TO <newAppName>_modelName
And in my case was
SQL 1 2 3
ALTER TABLE users_customuser RENAME TO accounts_customuser; ALTER TABLE users_customuser_groups RENAME TO accounts_customuser_groups; ALTER TABLE users_customuser_user_permissions RENAME TO accounts_customuser_user_permissions;
- Then you have to update the related migrations
The code for this step is
SQL 1
UPDATE django_migrations SET app='<NewAppName>' WHERE app='<OldAppName>'
And in my case was
SQL 1
UPDATE django_migrations SET app='accounts' WHERE app='users';
- If your
models.py's
Meta Class hasapp_name
, you have to rename it - If you have static or templates folder for the app you’re renaming, make sure to also rename them the same way (else Django will be confused by trying
new_app/static/old_app
for example) - Now if you want to rename models you have to do this
The code for this step is
SQL 1
UPDATE django_content_type SET name='<newModelName>' where name='<oldModelName>' AND app_label='<OldAppName>'
And in my case wasn’t necessary.