This is ** 2nd **, a memorandum of making a shogi game record management app using Django.
The working environment this time is as follows
Also, Django's directory structure looks like this:
- kifu_app_project/
- kifu_app_project/
- __init__.py
- setting.py
- urls.py
- wsgi.py
- kifu_app/
- migrations/
- __init__.py
- admin.py
- apps.py
- models.py
- tests.py
- views.py
- manage.py
- .gitignore
--Database settings
[Official document] 1 is easy to understand, so I think it is best to look at this and edit it.
There is settings.py in the inner kifu_app_project, so edit it.
By default, it connects to sqlite3
, so change it to connect to mysql
.
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Database name',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
}
}
Then enter the following command, but ...
$ python manage.py migrate
ModuleNotFound Error : No module named 'MySQLdb'
I get the error.
When I searched there, it was said that installing PyMySQL would solve it, so I will try it, but this time
$ python manage.py migrate
mysqlclient 1.3.13 or newer is required;
I got the error.
And when I looked into this error, I found the following blog. [Django: Error Resolution “raise ImproperlyConfigured (‘mysqlclient 1.3.13 or newer is required; ~) django.core.exceptions.ImproperlyConfigured: ~ ”] 2
Thanks,
Django's recommended MySQL (MariaDB) library is mysqlclient instead of PyMySQL
It seems that So, install mysqlclient with pip.
$ pip install mysqlclient
And when you run migrate again.
$ python manage.py migrate
Apply all migrations: admin ~
And it worked!
I think of a Model as a table design document / template. This time, create the following table.
--Information table --Enter game information --Large Class table ――Enter the major classification of battle type --Middle Class table ――Enter the middle classification of the battle type --Small Class table --Enter a sub-classification of battle type --Kifu table ――Put the hand you actually struck
First, create an Information table.
models.py
class Information(models.Model):
date = models.DateTimeField()
sente = models.CharField(max_length=50)
gote = models.CharField(max_length=50)
result = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(3)]) # `validatos`Validation with(0 or more and less than 3)
my_result = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(3)])
small_class = models.ForeignKey(SmallClass, on_delete=models.CASCADE) #Define relations
create_at = models.DateTimeField(auto_now_add=True) #Automatically add time
update_at = models.DateTimeField(auto_now=True) #Automatically update the time
(There are some Japanese variables, but please ignore them)
For the fields that can be used, I referred to the following page. [Summary of Django database model field list] 3
What I find particularly useful about Django is that it's fairly easy to create relationships between tables.
With ForeignKey (to, on_delete, ** options)
, you can set a many-to-one relationship (which model to relate to).
This makes it easier to retrieve relevant data, such as when retrieving queries at a later date.
Similarly, define other tables.
models.py
class LargeClass(models.Model):
name = models.CharField(max_length=10)
class MiddleClass(models.Model):
large_class = models.ForeignKey(LargeClass, on_delete=models.CASCADE)
name = models.CharField(max_length=10)
class SmallClass(models.Model):
middle_class = models.ForeignKey(MiddleClass, on_delete=models.CASCADE)
name = models.CharField(max_length=10)
class Information(models.Model):
#~ Omitted ~
class Kifu(models.Model):
information = models.ForeignKey(Information, on_delete=models.CASCADE)
number = models.IntegerField(validators=[MinValueValidator(0)])
te = models.CharField(max_length=20)
Due to the relationship (Foreign Key), if you do not write in this order, an error will occur.
After editing models.py, execute the following command to actually perform migration. Migration is to execute SQL statements, create tables, etc. based on Model.
$ python manage.py makemigrations <app name>
Migrations for '~'
~\migrations\0001_initial.py
- Create model ~
...~ Omitted ~
Note that this is the app name, not the project name.
Hopefully you'll see the statement Create model <table name>
.
Also, I think that a file called 0001_initial.py is created under kifu_app_project \ kifu_app \ migrations
.
This file defines tables and columns based on the Model you created.
Let's perform Migrate based on the Migration file created earlier and actually create a table.
$ python manage.py migrate
Applying kifu_app.0001_initial... OK
If you say "OK", you are successful!
After that, let's actually log in to mysql and check if the table is created!
[Django default admin site settings] 4