- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Django中数据库的数据关系:一对一,一对多,多对多由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
一对一:
一对一的关系极为一个数据仅对应一个数据,用下图的结构图可以帮助理解:
下面用代码实现一下,首先要创建工程项目如下:
接着,我们定义模型:
来到models.py文件,创建两个模型:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from
django.db
import
models
# Create your models here.
class
One(models.Model):
oname
=
models.CharField(max_length
=
20
,null
=
True
)
oage
=
models.CharField(max_length
=
20
,null
=
True
)
odate
=
models.DateField(null
=
True
)
class
Two(models.Model):
# 设置一对一关系,是通过将表中的字段设置为主键完成的
# on_delete=models.CASCADE 当父表中的某一条数据删除的时候
# 相关字表中的数据也会被删除
tsub
=
models.OneToOneField(One,on_delete
=
models.CASCADE,primary_key
=
True
)
tfond
=
models.CharField(max_length
=
20
,null
=
True
)
tdes
=
models.CharField(max_length
=
200
,null
=
True
)
|
来到myPro文件夹下添加以下两句代码:
1
2
|
import
pymysql
pymysql.install_as_MySQLdb()
|
下面可以迁移文件:
1
2
|
python manage.py makemigrations
python manage.py migrate
|
这样我们就创建了两个表:
来到views.py文件中添加数据,代码如下:
1
2
3
4
5
6
7
8
9
|
from
django.shortcuts
import
render
from
.models
import
One,Two
# Create your views here.
def
index(request):
o1
=
One.objects.create(oname
=
'张三'
,oage
=
11
,odate
=
'2011-11-11'
)
o2
=
One.objects.create(oname
=
'张三2'
,oage
=
12
,odate
=
'2012-12-12'
)
t1
=
Two.objects.create(tsub
=
o1,tfond
=
'o1'
,tdes
=
'我喜欢o1'
)
t2
=
Two.objects.create(tsub
=
o2,tfond
=
'o2'
,tdes
=
'我喜欢o2'
)
return
render(request,
'index.html'
)
|
运行之后,将添加数据的代码注释掉,否则后面每运行一次都会添加.
下面,我们通过查询数据来甄别其中的关系.
1
2
3
|
def
select(request):
t1
=
Two.objects.get(tsub__oname
=
'张三'
)
return
render(request,
'index.html'
,{
't1'
:t1})
|
一对多 。
即一个对象对应着对个对象.
创建模型代码:
1
2
3
4
5
6
7
8
9
|
from
django.db
import
models
# Create your models here.
class
People(models.Model):
name
=
models.CharField(max_length
=
50
)
card_num
=
models.IntegerField(default
=
0
)
class
Card(models.Model):
number
=
models.CharField(max_length
=
20
)
person
=
models.ForeignKey(People,on_delete
=
models.CASCADE)
source
=
models.CharField(max_length
=
50
)
|
urls.py路由设置:
1
2
3
4
5
6
7
8
|
from
django.contrib
import
admin
from
django.urls
import
path
from
myApp
import
views
urlpatterns
=
[
path(
'admin/'
, admin.site.urls),
path(
'add/'
,views.add),
path(
'select/'
,views.select),
]
|
views.py文件中代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
from
django.shortcuts
import
render
from
.models
import
People,Card
from
django.http
import
HttpResponse
# Create your views here.
# 添加数据
def
add(request):
# p1 = People.objects.create(name='小王',card_num = 4)
# p2 = People.objects.create(name='老王', card_num=40)
#
# c1 = Card(number='101',source = '中国银行',person = p1)
# c2 = Card(number='102', source='中国农行', person=p1)
# c3 = Card(number='110', source='中国建行', person=p1)
# c1.save()
# c2.save()
# c3.save()
#
# c4 = Card(number='201', source='河南郑州美容美发', person=p2)
# c5 = Card(number='202', source='郑州交通一卡通', person=p2)
# c6 = Card(number='203', source='郑州逍遥镇胡辣汤', person=p2)
# c7 = Card(number='204', source='郑州惠济四附院', person=p2)
#
# c4.save()
# c5.save()
# c6.save()
# c7.save()
return
HttpResponse(
'添加成功'
)
def
select(request):
# 查找number=203的人
c1
=
Card.objects.get(number
=
'203'
)
print
(c1.person.name)
# 查找id为3对应的人
c2
=
Card.objects.get(
id
=
3
)
print
(c2.person.name)
# 查找c2的所有卡
result
=
c2.person.card_set.
all
()
print
(result)
for
res
in
result:
print
(res.source)
# 查找名字为老王的所有卡种
result
=
People.objects.get(name
=
'老王'
)
for
card
in
result.card_set.
all
():
print
(card.source)
return
HttpResponse(
'查询成功'
)
|
多对多 。
即多个对象对应对个对象,类似公交车坐车,人可以坐多个公交车,公交车也可以载不同的人.
创建模型代码如下:
1
2
3
4
5
6
7
8
9
|
from
django.db
import
models
# Create your models here.
class
Publication(models.Model):
pname
=
models.CharField(max_length
=
200
)
paddress
=
models.CharField(max_length
=
200
)
class
Book(models.Model):
bname
=
models.CharField(max_length
=
200
)
bauthor
=
models.CharField(max_length
=
200
)
publication
=
models.ManyToManyField(Publication)
|
视图文件views.py文件代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
from
django.shortcuts
import
render
from
.models
import
Publication,Book
from
django.http
import
HttpResponse
# Create your views here.
def
add(request):
# p1 = Publication(pname='大象出版社',paddress='河南',)
# p2 = Publication(pname='北京出版社',paddress='北京')
# p3 = Publication(pname='清华出版社',paddress='河北')
# p1.save()
# p2.save()
# p3.save()
#
# b1 = Book(bname='海底两万里',bauthor='赵四')
# b2 = Book(bname='遮天',bauthor='辰东')
# b3 = Book(bname='童年', bauthor='xxxx')
# b4 = Book(bname='在人间', bauthor='yyyy')
# b5 = Book(bname='我的大学', bauthor='张飞')
# b6 = Book(bname='汤姆索亚历险记', bauthor='赵六儿')
# b1.save()
# b2.save()
# b3.save()
# b4.save()
# b5.save()
# b6.save()
#
# b1.publication.add(p1,p2,p3)
# b2.publication.add(p1,p2)
# b3.publication.add(p1,p3)
# b4.publication.add(p2,p3)
# b5.publication.add(p3)
# 多对多关系,两个表不直接产生联系,而是将两个表之间的关系记录在中间表上
# 中间表不需要创建,会自动生成
return
HttpResponse(
'添加成功'
)
def
select(request):
# 通过书籍查找对应的出版社
b1
=
Book.objects.get(bname
=
'童年'
)
# 获取出版童年的所有出版社
b1_publication
=
b1.publication.
all
()
for
pub
in
b1_publication:
print
(pub.pname)
print
(pub.paddress)
p1
=
Publication.objects.get(pname
=
'清华出版社'
)
all_book
=
p1.book_set.
all
()
print
(
'------------------'
)
for
book
in
all_book:
print
(book.bname)
print
(book.bauthor)
return
HttpResponse(
'查找成功'
)
|
这样,就介绍完了.
总结 。
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我的支持。如果你想了解更多相关内容请查看下面相关链接 。
原文链接:https://blog.csdn.net/qq_39138295/article/details/82631678 。
最后此篇关于Django中数据库的数据关系:一对一,一对多,多对多的文章就讲到这里了,如果你想了解更多关于Django中数据库的数据关系:一对一,一对多,多对多的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我的问题是如何在 python 中创建一个简单的数据库。我的例子是: User = { 'Name' : {'Firstname', 'Lastname'}, 'Address' : {'Street
我需要创建一个与远程数据库链接的应用程序! mysql 是最好的解决方案吗? Sqlite 是唯一的本地解决方案吗? 我使用下面的方法,我想知道它是否是最好的方法! NSString *evento
给定两台 MySQL 服务器,一台本地,一台远程。两者都有一个包含表 bohica 的数据库 foobar。本地服务器定义了用户 'myadmin'@'%' 和 'myadmin'@'localhos
我有以下灵活的搜索查询 Select {vt:code},{vt:productcode},{vw:code},{vw:productcode} from {abcd AS vt JOIN wxyz
好吧,我的电脑开始运行有点缓慢,所以我重置了 Windows,保留了我的文件。因为我的大脑还没有打开,所以我忘记事先备份我的 MySQL 数据库。我仍然拥有所有原始文件,因此我实际上仍然拥有数据库,但
如何将我的 Access 数据库 (.accdb) 转换为 SQLite 数据库 (.sqlite)? 请,任何帮助将不胜感激。 最佳答案 1)如果要转换 db 的结构,则应使用任何 DB 建模工具:
系统检查发现了一些问题: 警告:?:(mysql.W002)未为数据库连接“默认”设置 MySQL 严格模式 提示:MySQL 的严格模式通过将警告升级为错误来修复 MySQL 中的许多数据完整性问题
系统检查发现了一些问题: 警告:?:(mysql.W002)未为数据库连接“默认”设置 MySQL 严格模式 提示:MySQL 的严格模式通过将警告升级为错误来修复 MySQL 中的许多数据完整性问题
我想在相同的 phonegap 应用程序中使用 android 数据库。 更多说明: 我创建了 phonegap 应用程序,但 phonegap 应用程序不支持服务,所以我们已经在 java 中为 a
Time Tracker function clock() { var mytime = new Date(); var seconds
我需要在现有项目上实现一些事件的显示。我无法更改数据库结构。 在我的 Controller 中,我(从 ajax 请求)传递了一个时间戳,并且我需要显示之前的 8 个事件。因此,如果时间戳是(转换后)
我有一个可以收集和显示各种测量值的产品(不会详细介绍)。正如人们所期望的那样,显示部分是一个数据库+建立在其之上的网站(使用 Symfony)。 但是,我们可能还会创建一个 API 来向第三方公开数据
我们将 SQL Server 从 Azure VM 迁移到 Azure SQL 数据库。 Azure VM 为 DS2_V2、2 核、7GB RAM、最大 6400 IOPS Azure SQL 数据
我正在开发一个使用 MongoDB 数据库的程序,但我想问在通过 Java 执行 SQL 时是否可以使用内部数据库进行测试,例如 H2? 最佳答案 你可以尝试使用Testcontainers Test
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 9 年前。 此问题似乎与 a specific programming problem, a sof
我正在尝试使用 MSI 身份验证(无需用户名和密码)从 Azure 机器学习服务连接 Azure SQL 数据库。 我正在尝试在 Azure 机器学习服务上建立机器学习模型,目的是我需要数据,这就是我
我在我的 MySQL 数据库中使用这个查询来查找 my_column 不为空的所有行: SELECT * FROM my_table WHERE my_column != ""; 不幸的是,许多行在
我有那个基地:http://sqlfiddle.com/#!2/e5a24/2这是 WordPress 默认模式的简写。我已经删除了该示例不需要的字段。 如您所见,我的结果是“类别 1”的两倍。我喜欢
我有一张这样的 table : mysql> select * from users; +--------+----------+------------+-----------+ | userid
我有表: CREATE TABLE IF NOT EXISTS `category` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL
我是一名优秀的程序员,十分优秀!