C:\Users\520\mysite>python manage.py startapp oto
C:\Users\520\mysite>
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp.apps.MyappConfig',
"oto",
]
# oto.models.py
from django.db import models
# Create your models here.
class Author(models.Model):
name = models.CharField(max_length=11, verbose_name="姓名")
class Wife(models.Model):
name = models.CharField(max_length=11, verbose_name="姓名")
author = models.OneToOneField(Author, on_delete=models.CASCADE)
C:\Users\520\mysite>python manage.py makemigrations
Migrations for 'oto':
oto\migrations\0001_initial.py
- Create model Author
- Create model Wife
C:\Users\520\mysite>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, myapp, oto, sessions
Running migrations:
Applying oto.0001_initial... OK
C:\Users\520\mysite>
Microsoft Windows [版本 10.0.19042.985]
(c) Microsoft Corporation。保留所有权利。
C:\Users\520>cd..
C:\Users>cd..
C:\>cd program files\mysql\mysql server 8.0\bin
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql -uroot -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 237
Server version: 8.0.25 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysite;
Database changed
mysql> show tables;
+----------------------------+
| Tables_in_mysite |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| author |
| book |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
| oto_author |
| oto_wife |
+----------------------------+
14 rows in set (0.00 sec)
mysql> desc oto_wife;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | bigint | NO | PRI | NULL | auto_increment |
| name | varchar(11) | NO | | NULL | |
| author_id | bigint | NO | UNI | NULL | |
+-----------+-------------+------+-----+---------+----------------+
3 rows in set (0.02 sec)
mysql> desc oto_author;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | bigint | NO | PRI | NULL | auto_increment |
| name | varchar(11) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql>
Django模型类的名字,在数据库里是“应用名_模型类名小写”
Django模型类类属性名里的映射属性名,在数据库里是“关联模型类名_id”
C:\Users\520\mysite>python manage.py shell
Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from oto.models import *
>>> a1 = Author.objects.create(name="wang")
>>> w1 = Wife.objects.create(name="wangfuren", author=a1)
>>> a2 = Author.objects.create(name="guo")
>>> w2 = Wife.objects.create(name="guofuren", author_id=2)
>>>
mysql> select * from oto_wife;
+----+-----------+-----------+
| id | name | author_id |
+----+-----------+-----------+
| 1 | wangfuren | 1 |
| 2 | guofuren | 2 |
+----+-----------+-----------+
2 rows in set (0.00 sec)
mysql>
>>> a1 = Author.objects.create(name="wang")
>>> w1 = Wife.objects.create(name="wangfuren", author=a1)
>>> a2 = Author.objects.create(name="guo")
>>> w2 = Wife.objects.create(name="guofuren", author_id=2)
>>> w2
(2)>
>>> w2.name
'guofuren'
>>> w2.author.name
'guo'
>>> w1.author.name
'wang'
>>>
>>> a1 = Author.objects.create(name="wang")
>>> w1 = Wife.objects.create(name="wangfuren", author=a1)
>>> a2 = Author.objects.create(name="guo")
>>> a1
(1)>
>>> a1.wife
(1)>
>>> a1.wife.name
'wangfuren'