Freely customize the model. Change the variable name and field name of the model of the Django / Django Rest Framework app ~ How to reflect the deployment on heroku Memo

Introduction

Django's models.py is linked to the database compared to other views.py, so once you mess with it, it seems to be very troublesome, but this time the solution has been settled, so be polite in your own way I tried to describe it in. If it's hard to understand, I'm sorry.

Verification environment

Python==3.6.1 Django==2.2.1 djangorestframework==3.11.0

Part 1: How to change the variable name of model

"Migration of models.py is completed! Oh, I found a typo in the variable name ... Rewrite and migrate (` $ python manage.py makemigratios → $ python manage.py migrate```), error ... what Even if I do it, an error ... What should I do? " Here's how to solve this.

1: Create a migration file

Type a command

$ python manage.py makemigrations --empty <app name>

Migrations for '<app name>':
  <app name>/migrations/0012_auto_20200610_0000.py

2: There is an empty migration file in `` ` / migrations```

#0012_auto_20200610_0000.py


from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('<app name>', '0011_auto_20200610_1111'),
    ]

    operations = [
    ]

3: Write variable rewrite instruction to migration file

(1) In case of one change

#0012_auto_20200610_0000.py


from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('<app name>', '0011_auto_20200610_1111'),
    ]

    operations = [
        migrations.RenameField(
            model_name='<The model name you want to change inside the app>',
            old_name='image1Discription', #Example: Variable name you want to change
            new_name='image1Description' #Example: Variable name after change
        )
    ]

(2) When there are two or more changes

#0012_auto_20200610_0000.py


from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('<app name>', '0011_auto_20200610_1111'),
    ]

    operations = [
        migrations.RenameField(
            model_name='<The model name you want to change inside the app>',
            old_name='image1Discription', #Example: Variable name you want to change
            new_name='image1Description' #Example: Variable name after change
        ),
        migrations.RenameField(
            model_name='<The model name you want to change inside the app>',
            old_name='image2Discription', #Example: Variable name you want to change
            new_name='image2Description' #Example: Variable name after change
        )
    ]

4: Local migrate test

Don't forget to specify the DB of settings.py locally.

$ python manage.py migrate
Running migrations:
  Applying realestatedb.0012_auto_20200610_0000... OK

You can now change your local db (such as postgre). Since models.py is related to the behavior of apps other than DB, even if you run it now, you will get a 503 error, but if you write changes to models.py in a later process, the error will be fixed.

5: Upload to heroku and do the same remotely

Don't forget to specify the DB of settings.py remotely.

$ git add .
$ git commit -m "db revise"
$ git push heroku master

(Wait)

$ heroku run python manage.py migrate
Running migrations:
  Applying realestatedb.0012_auto_20200610_0000... OK

With this, the remote DB is OK for the time being.

6: Change models.py, views.py serializers.py, etc. and deploy to heroku.

$ git add .
$ git commit -m "db revise"
$ git push heroku master

(Wait)

If you check the operation and it is OK, you are done. Just because you messed with models.py, the migration is already done, so you don't need to migrate further.

Part 2: How to change the data type of the model field

"Migration of models.py is complete! Oh, I thought it was this variable Int, but it's Datetime, not Int ... I made a mistake ... Rewrite and migrate (` $ python manage.py makemigratios → $ python In manage.py migrate```), an error ... an error no matter what ... what should I do? " Here's how to solve this.

1: Create a migration file

$ python manage.py makemigrations --empty realestatedb

Migrations for 'realestatedb':
  realestatedb/migrations/0012_auto_20200610_0000.py

2: There is an empty migration file in `` ` / migrations```

#0012_auto_20200610_0000.py


from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('<app name>', '0011_auto_20200610_1111'),
    ]

    operations = [
    ]

3: Write variable rewrite instruction to migration file

from django.db import migrations, models



 Please note that an error will occur if you do not add the import of ``` models` `` to.


#### **`#0012_auto_20200610_0000.py`**
```py

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('<app name>', '0011_auto_20200610_1111'),
    ]

    operations = [
        migrations.AlterField(
            model_name='<The model name you want to change inside the app>',
            name='FeePayDate', #Example: Variable name you want to change
            field=models.DateTimeField(blank=True, null=True), #Example: Data type name after change
        ),
    ]

4: Local migrate test

Don't forget to specify the DB of settings.py locally.

$ python manage.py migrate
Running migrations:
  Applying realestatedb.0012_auto_20200610_0000... OK

You can now change your local db (such as postgre). Since models.py is related to the behavior of apps other than DB, even if you run it now, you will get a 503 error, but if you write changes to models.py in a later process, the error will be fixed.

5: Upload to heroku and do the same remotely

Don't forget to specify the DB of settings.py remotely.

$ git add .
$ git commit -m "db field revise"
$ git push heroku master

(Wait)

$ heroku run python manage.py migrate
Running migrations:
  Applying realestatedb.0012_auto_20200610_0000... OK

With this, the remote DB is OK for the time being.

6: Change models.py, views.py serializers.py, etc. and deploy to heroku.

$ git add .
$ git commit -m "db field revise"
$ git push heroku master

(Wait)

If you check the operation and it is OK, you are done. Just because you messed with models.py, the migration is already done, so you don't need to migrate further.

Reference article

Rename table columns in Django3 https://qiita.com/holly0819/items/41f01096f59416b0d52b

Learning notes for the migrations feature in the Django framework (3) https://qiita.com/pumbaacave/items/8b6f8d96ddc7cc112827

Change column type with django migrations https://stackoverflow.com/questions/27385118/change-column-type-with-django-migrations

Recommended Posts

Freely customize the model. Change the variable name and field name of the model of the Django / Django Rest Framework app ~ How to reflect the deployment on heroku Memo
Heroku deployment of the first Django app that beginners are addicted to
Memo of deploying Django × Postgresql on Docker to Heroku
How to write custom validations in the Django REST Framework
How to return the data contained in django model in json format and map it on leaflet
[Django] Correct the plural form of the model name on the management screen
I summarized how to change the boot parameters of GRUB and GRUB2
How to deploy a Django app on heroku in just 5 minutes
How to deal with garbled characters in json of Django REST Framework
How to get the "name" of a field whose value is limited by the choice attribute in Django's model
Understand the benefits of the Django Rest Framework
How to check the version of Django
Deploy the Django app on Heroku [Part 2]
Deploy the Django app on Heroku [Part 1]
Change the order of PostgreSQL on Heroku
How to count the number of elements in Django and output to a template
A memo on how to overcome the difficult problem of capturing FX with AI