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.
stack overflow/How to change the name of a Django app? As written here.
I was able to change the application name while keeping the existing code and records in 4 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
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
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.
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.
From here change blogs
to vlogs
.
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.
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,
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
.
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;
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'
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.
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
In this way, 0002_post_content.py
was created as a continuation under vlogs / migrations
, and the content column was added without any problem.
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.
Recommended Posts