// ubuntu bionicに入っているmysqlのバージョンを確認し、dbを作る
$ mysql –version
mysql Ver 14.14 Distrib 5.7.31, for Linux (x86_64) using EditLine wrapper
$ mysql -u root -p
Enter password:
mysql> show databases;
mysql> create database myapp;
Query OK, 1 row affected (0.01 sec)
OK
それではテスト用のDjangoのappを作ります
$ mkdir test
$ cd test
$ django-admin startproject testapp
$ cd testapp
$ python manage.py startapp myapp
/myapp/settings.py の DATABASESがデータベースの設定
defaultではsqlite3になっている
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }
ここをmysqlに変更します。
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp', ] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'myapp', 'USER': 'root', 'PASSWORD': '*****', 'HOST': 'localhost', 'PORT': '3306', } }
myqpp/models.py
from django.db import models class Friend(models.Model): name = models.CharField(max_length=100) mail = models.EmailField(max_length=200) gender = models.BooleanField() age = models.IntegerField(default=0) birthday = models.DateField() def __str__(self): return '<Friend:id=' + str(self.id) + ', ' + self.name + '(' + str(self.age) + ')>'
$ python manage.py makemigrations testapp
Traceback (most recent call last):
File “/usr/local/lib/python3.8/dist-packages/django/db/backends/mysql/base.py”, line 16, in
import MySQLdb as Database
ModuleNotFoundError: No module named ‘MySQLdb’
The above exception was the direct cause of the following exception:
// 省略
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
$ sudo apt-get install python3.8-dev
$ sudo apt-get install -y libmysqlclient-dev
$ pip install mysqlclient
$ python manage.py makemigrations testapp
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial… OK
Applying auth.0001_initial… OK
Applying admin.0001_initial… OK
Applying admin.0002_logentry_remove_auto_add… OK
Applying admin.0003_logentry_add_action_flag_choices… OK
Applying contenttypes.0002_remove_content_type_name… OK
Applying auth.0002_alter_permission_name_max_length… OK
Applying auth.0003_alter_user_email_max_length… OK
Applying auth.0004_alter_user_username_opts… OK
Applying auth.0005_alter_user_last_login_null… OK
Applying auth.0006_require_contenttypes_0002… OK
Applying auth.0007_alter_validators_add_error_messages… OK
Applying auth.0008_alter_user_username_max_length… OK
Applying auth.0009_alter_user_last_name_max_length… OK
Applying auth.0010_alter_group_name_max_length… OK
Applying auth.0011_update_proxy_permissions… OK
Applying sessions.0001_initial… OK
mysql> show tables;
+—————————-+
| Tables_in_myapp |
+—————————-+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
| myapp_friend |
+—————————-+
11 rows in set (0.00 sec)
mysql> describe myapp_friend
-> ;
+———-+————–+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+———-+————–+——+—–+———+—————-+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| mail | varchar(200) | NO | | NULL | |
| gender | tinyint(1) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
| birthday | date | NO | | NULL | |
+———-+————–+——+—–+———+—————-+
6 rows in set (0.00 sec)
mysqlにマイグレーションできてることは確認できました。
auth_* のテーブルが結構あるけど、やりたい事としてはusersテーブルで、email, name, password, department, (created), (updated)を管理したい。
Djangoのuser管理がどうなってるか理解する必要があるな。