Rename an existing Django application

What is this?

I usually develop with Django, and sometimes I think of it.

`It's working fine now, but I don't like the application name. ``

If you just change the folder name as a trial, an unnecessary table will be created and the existing data cannot be used.

What to do when you want to change the application name while using the data in the existing table as it is

I think there are various methods, but I have summarized them as one of them.

Conclusion

stack overflow/How to change the name of a Django app? As written here.

  1. Rename the folder
  2. Change AppConfig, change ʻINSTALLED_APPS` in settings
  3. Rename the old table to match the new application name
  4. Rewrite django_content_type and django_migrations

I was able to change the application name while keeping the existing code and records in 4 steps.

Why do you need the above steps?

I will briefly describe why changing the folder name does not work as expected.

Django's migrations are managed in the django_migrations table. (I'm using Sequel Pro) ↓django_migrations image.png

The app column is INSTALLED_APPS in settings.py, The name column contains the filename in migrations for each application.

Also, Model is managed in the django_content_type table. ↓django_content_type image.png

The app_label column is also INSTALLED_APPS in settings.py, The model column contains the model name of models.py in each application.

Even if you rename the folder, if you keep it as it is, Django will recognize it as a new and completely different application created.

By following steps 3 and 4 above, you'll need to get Django to recognize your renamed application as being native.

Below is the process of moving the hand. If you are interested, please read it.


Premise

Suppose you want to rename your blogs app to vlogs app.

Create a blogs app.

1: Create a Django project

$ django-admin startproject my-site

2: Create a blogs app

$ python manage.py startapp blogs

3: Create PostModel

blogs/models.py

from django.db import models


class Post(models.Model):
    title = models.CharField(max_length=255)

4: Run migration my-site/settings.py

INSTALLED_APPS = [
    'blogs.apps.BlogsConfig',
    ...
]
$ python manage.py migrate

This will create a table like this. image.png

From here change blogs to vlogs.

1. Rename the folder

I will introduce what happens if you just change the folder name as a trial.

/ blogs / vlogs. In this state

$ python manage.py migrate

Then of course ModuleNotFoundError: No module named 'blogs' It will be.

Then next.

2. Change AppConfig, change ʻINSTALLED_APPS` in settings

You have to edit ʻINSTALLED_APPS` as you got angry at 1.

vlogs/apps.py

from django.apps import AppConfig


class VlogsConfig(AppConfig):
    name = 'vlogs'

settings.py


INSTALLED_APPS = [
    'vlogs.apps.VlogsConfig',
    ...
]

Django should now recognize the vlogs app. Run show migrations

$ python manage.py showmigrations
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
sessions
 [X] 0001_initial
vlogs
 [ ] 0001_initial

You are recognized. migrate.

$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, vlogs
Running migrations:
  Applying vlogs.0001_initial... OK

It works fine like this, image.png You will end up with vlogs_post and blogs_post. The ideal is to have only vlogs_post. Now change blogs_post to vlogs_post.

3. Rename the old table to match the new application name

I want the table created from PostModel to become vlogs_post by renaming blogs app to vlogs app. Change the table name.

mysql> ALTER TABLE blogs_post RENAME TO vlogs_post;

4. Rewrite django_content_type and django_migrations

In order to have applications that were previously treated as blogs be treated as vlogs in the future Rewrite django_content_type and django_migrations.

mysql> UPDATE django_content_type SET app_label='vlogs' WHERE app_label='blogs'
mysql> UPDATE django_migrations SET app='vlogs' WHERE app='blogs'

result

Now, let's check if there is no problem. As a confirmation method

1: Nothing happens when you run migrate without making any changes to the Model

$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, vlogs
Running migrations:
  No migrations to apply.

image.png It doesn't seem to be a problem.

2: A migration will be created from the continuation when the model is changed. Add a content column to the Post model. vlogs/models.py


from django.db import models


class Post(models.Model):
    title = models.CharField(max_length=255)
    content = models.CharField(max_length=255, null=True)

$ python manage.py makemigrations
Migrations for 'vlogs':
  vlogs/migrations/0002_post_content.py
    - Add field content to post

$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, vlogs
Running migrations:
  Applying vlogs.0002_post_content... OK

image.png

In this way, 0002_post_content.py was created as a continuation under vlogs / migrations, and the content column was added without any problem.

Summary

I thought it was a good subject to somehow understand what Django's migration looks like and how it works. I also touched on django_content_type, which I don't usually pay attention to.

It's GitHub.

Thank you very much.

reference

Recommended Posts

Rename an existing Django application
Browse an existing external database with Django
[Django] Rename the project
Procedure for creating an application with Django with Pycharm ~ Preparation ~
Try running a Django application on an nginx unit
Deploy an existing app with docker + pyenv-virtualenv + uwsgi + django
It's too easy to use an existing database with Django
Create an API with Django
Rename table columns in Django3
Web application creation with Django
WEB application development using Django [Django startup]
Launch notes for existing Django applications
Load Django modules with an interpreter
WEB application development using Django [Application addition]
Measure Django application coverage with Coverage.py
Register your Django application in your project
Django beginners tried building an environment
Deploy a Django application with Docker
Create new application use python, django
Twitter posting application made with Django
Build a web application with Django
Note: Send an email with Django
How to build an application from the cloud using the Django web framework