Changing an app name in Django

Post #201 written by Khodok in Django


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:

  1. Saving all the data
  2. Removing the users app
  3. Creating accounts app
  4. Adding the data from old users app
  5. Making the migrations linked to that
  6. Making the rest of the migrations
  7. Adding the data from before to the all the respective apps
  8. Done
  1. Changing stuff
  2. Trying
  3. Noticing that there’s an error, thank God Django gives good error messages
  4. Using the error messages to detect and erase the error
  5. 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

  1. Rename the folder of the app you want to rename.
  2. 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).
  3. 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
    UPDATE django_content_type SET app_label='<NewAppName>' WHERE app_label='<OldAppName>'

    And in my case was
    UPDATE django_content_type SET app_label='accounts' WHERE app_label='users';
  4. Then you have to rename the models with the new app name before the underscore (common Database stuff)
    The code for this step is
    ALTER TABLE <oldAppName>_modelName RENAME TO <newAppName>_modelName

    And in my case was
    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;
  5. Then you have to update the related migrations
    The code for this step is
    UPDATE django_migrations SET app='<NewAppName>' WHERE app='<OldAppName>'

    And in my case was
    UPDATE django_migrations SET app='accounts' WHERE app='users';
  6. If your's Meta Class has app_name, you have to rename it
  7. 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)
  8. Now if you want to rename models you have to do this
    The code for this step is
    UPDATE django_content_type SET name='<newModelName>' where name='<oldModelName>' AND app_label='<OldAppName>'

    And in my case wasn’t necessary.

Please Log in to leave a comment.

No comments yet.