はじめに
px-wing.hatenablog.com
お問い合わせテーブル
# |
contact |
お問い合わせ |
1 |
contact_name |
お名前 |
2 |
contact_mailaddress |
メールアドレス |
3 |
contact_subject |
題名 |
4 |
contact_message |
メッセージ |
5 |
contact_time |
お問い合わせ時間 |
6 |
contact_attachment_file |
添付ファイル |
7 |
contact_status |
お問い合わせステータス |
モデルを作成する
from django.db import models
# Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
class Contact(models.Model):
contact_name = models.CharField(max_length=255)
contact_mailaddress = models.EmailField(max_length=255)
contact_subject = models.CharField(max_length=255)
contact_message = models.TextField()
contact_time = models.DateTimeField()
contact_attachment_file = models.FileField()
contact_status = models.IntegerField(default=0)
# python manage.py makemigrations
Migrations for 'api':
api/migrations/0001_initial.py
- Create model Contact
- Create model Question
- Create model Choice
モデルをデータベースに反映させる
# python manage.py migrate
Operations to perform:
Apply all migrations: admin, api, auth, contenttypes, sessions
Running migrations:
Applying api.0001_initial... OK
テーブルの確認
mysql> desc api_contact;
+-------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| contact_name | varchar(255) | NO | | NULL | |
| contact_mailaddress | varchar(255) | NO | | NULL | |
| contact_subject | varchar(255) | NO | | NULL | |
| contact_message | longtext | NO | | NULL | |
| contact_time | datetime(6) | NO | | NULL | |
| contact_status | int | NO | | NULL | |
| contact_attachment_file | varchar(100) | NO | | NULL | |
+-------------------------+--------------+------+-----+---------+----------------+
8 rows in set (0.52 sec)
その他
- 誤ってmigrateでテーブルを作成してしまった場合、いらないテーブルを削除したい場合、下記のように記述することで不要なテーブルを削除できる。
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [('api', '0002_contact_contact_attachment_file')]
operations = [
migrations.DeleteModel('Contact'),
]