django-关系映射 一对一 一对多 多对多
关系映射
什么是关系映射
- 怎么建表
- 怎么传入数据
- 怎么查询数据
一对一的映射 on_delete
官网链接
cascade 是在orm层面进行设置
models.protect mysql默认
实例说明
models生成
oto.models.py
from django.db import models
# Create your models here.
class Author(models.Model):
name=models.CharField('姓名',max_length=11)
class Wife(models.Model):
name=models.CharField('姓名',max_length=11)
author=models.OneToOneField(Author,on_delete=models.CASCADE)
迁移
E:\django_video_study\mysite2>python manage.py startapp oto
<class 'list'>
E:\django_video_study\mysite2>python manage.py makemigrations
<class 'list'>
Migrations for 'bookstore':
bookstore\migrations\0006_auto_20210813_1917.py
- Change Meta options on book
- Alter field pub on book
Migrations for 'oto':
oto\migrations\0001_initial.py
- Create model Author
- Create model Wife
E:\django_video_study\mysite2>python manage.py migrate
<class 'list'>
Operations to perform:
Apply all migrations: admin, auth, bookstore, contenttypes, oto, sessions
Running migrations:
Applying bookstore.0006_auto_20210813_1917... OK
Applying oto.0001_initial... OK
E:\django_video_study\mysite2>
数据库内样式
mysql> desc oto_wife;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name | varchar(11) | NO | | NULL | |
| author_id | int | NO | UNI | NULL | |
+-----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> show create table oto_wife;
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| oto_wife | CREATE TABLE `oto_wife` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(11) NOT NULL,
`author_id` int NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `author_id` (`author_id`),
CONSTRAINT `oto_wife_author_id_3d57c7a2_fk_oto_author_id` FOREIGN KEY (`author_id`) REFERENCES `oto_author` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
创建数据
In [1]: from oto.models import Author,Wife
In [2]: author1=Author.objects.create(name='王老师')
In [3]: #通过类的属性名创建数据
In [4]: wife1=Wife.objects.create(name='王夫人',author=author1)
mysql> select * from oto_wife;
+----+-----------+-----------+
| id | name | author_id |
+----+-----------+-----------+
| 1 | 王夫人 | 1 |
+----+-----------+-----------+
In [5]: #通过外键字段名创建数据
In [6]: author2=Author.objects.create(name='果子哥')
In [7]: wife2=Wife.objects.create(name='郭老师',author_id=2)
mysql> select * from oto_wife;
+----+-----------+-----------+
| id | name | author_id |
+----+-----------+-----------+
| 1 | 王夫人 | 1 |
| 2 | 郭老师 | 2 |
+----+-----------+-----------+
2 rows in set (0.00 sec)
数据查询
正向查询
In [7]: wife2=Wife.objects.create(name='郭老师',author_id=2)
In [8]: # 正向查询
In [9]: wife2
Out[9]: <Wife: Wife object (2)>
In [10]: wife2.author
Out[10]: <Author: Author object (2)>
In [11]: wife2.author.name
Out[11]: '果子哥'
反向查询
In [12]: # 反向查询
In [13]: # Author在onetoone中会有一个反向属性
In [14]: author1
Out[14]: <Author: Author object (1)>
In [15]: author1.wife.name
Out[15]: '王夫人'