Change the pub_data of the Question model of the polls app to pub_date.
Create an empty migration file on the command line by referring to the Official Document.
$ python3 manage.py makemigration --empty [app name]
Then, a migration file will be created in the polls folder directly under the application.
migrations/0002_auto_20200128_0044.py
# Generated by Django 3.0.2 on 2020-01-28 00:44
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('[app name]', '0001_initial'),
]
operations = [
]
Rewrite this as follows.
migrations/0002_auto_20200128_0044.py
# Generated by Django 3.0.2 on 2020-01-28 00:44
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('[app name]', '0001_initial'),
]
operations = [
migrations.RenameField(
model_name='question', #Model name
old_name='pub_data', #Change before
new_name='pub_date' #After change
)
]
If you rewrite the RenameField, you can add or delete columns and indexes in the same way. For more information, go to Official Documents.
Finally, run the following command on the command line to apply your changes.
$ python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
Applying polls.0002_auto_20200128_0044... OK
Let's check it on the SQL side.
mysql> DESC polls_question;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| question_text | varchar(200) | NO | | NULL | |
| pub_date | datetime(6) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
It's strange: relaxed:
Modify files such as model.py.
model.py
class Question(models.Model):
question_text = models.CharField(max_length=200)
γ# pub_data -> pub_date
pub_data = models.DateTimeField('data published')
def __str__(self):
return self.question_text
model.py
class Question(models.Model):
question_text = models.CharField(max_length=200)
# pub_data -> pub_date
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
Why is the function name that changes the column name RenameField instead of RenameColumn?
If anyone knows another good way, please let me know.
Recommended Posts