django-关系映射 一对一 一对多 多对多
2021/8/13 23:07:25
本文主要是介绍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]: '王夫人'
这篇关于django-关系映射 一对一 一对多 多对多的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26go.mod的文件内容是什么?-icode9专业技术文章分享
- 2024-11-23MongoDB身份认证机制揭秘!
- 2024-11-20MongoDB教程:从入门到实践详解
- 2024-11-17执行 Google Ads API 查询后返回的是空数组什么原因?-icode9专业技术文章分享
- 2024-11-17google广告数据不同经理账户下的凭证可以获取对方的api数据吗?-icode9专业技术文章分享
- 2024-11-15SendGrid 的 Go 客户端库怎么实现同时向多个邮箱发送邮件?-icode9专业技术文章分享
- 2024-11-15SendGrid 的 Go 客户端库怎么设置header 和 标签tag 呢?-icode9专业技术文章分享
- 2024-11-12Cargo deny安装指路
- 2024-11-02MongoDB项目实战:从入门到初级应用
- 2024-11-01随时随地一键转录,Google Cloud 新模型 Chirp 2 让语音识别更上一层楼