django 数据库 多表操作
生成表的代码:
from django.db import models # Create your models here. class Book(models.Model): title=models.CharField(max_length=32,unique=True) price=models.DecimalField(max_digits=8,decimal_places=2,null=True)# 999999.99 pub_date=models.DateTimeField() publish=models.ForeignKey(to="Publish",to_field="id",on_delete=models.CASCADE) #多对多 authors=models.ManyToManyField(to="Author") def __str__(self): return self.title class Publish(models.Model): name=models.CharField(max_length=32) email=models.CharField(max_length=32) addr=models.CharField(max_length=32) class Author(models.Model): name=models.CharField(max_length=32) age=models.IntegerField() # 外键 ad=models.ForeignKey(to="AuthorDetail",to_field="id",on_delete=models.CASCADE,unique=True) #一对一
ad=models.OneToOneField(to="AuthorDetail",to_field="id",on_delete=models.CASCADE,) def __str__(self): return self.name class AuthorDetail(models.Model): gf=models.CharField(max_length=32) tel=models.CharField(max_length=32)
一对一:
class Author(models.Model):
nid = models.AutoField(primary_key=True) name=models.CharField( max_length=32) age=models.IntegerField() # 与AuthorDetail建立一对一的关系 authorDetail=models.OneToOneField(to="AuthorDetail",on_delete=models.CASCADE)
添加数据:
先添加关联的表信息,在添加被关联表的信息:
a1=AuthorDetail.objects.create(gf=gf,tel=tel) #关联表
Author.objects.create(name=name, age=age,authorDetail=a1) #被关联表
一对多
方式1: publish_obj=Publish.objects.get(nid=1) book_obj=Book.objects.create(title="金瓶眉",publishDate="2012-12-12",price=100,publish=publish_obj) 方式2: book_obj=Book.objects.create(title="金瓶眉",publishDate="2012-12-12",price=100,publish_id=1)
核心:book_obj.publish与book_obj.publish_id是什么?
多对多
# 当前生成的书籍对象 book_obj=Book.objects.create(title="追风筝的人",price=200,publishDate="2012-11-12",publish_id=1) # 为书籍绑定的做作者对象 yuan=Author.objects.filter(name="yuan").first() # 在Author表中主键为2的纪录 egon=Author.objects.filter(name="alex").first() # 在Author表中主键为1的纪录 # 绑定多对多关系,即向关系表book_authors中添加纪录 book_obj.authors.add(yuan,egon) # 将某些特定的 model 对象添加到被关联对象集合中。 ======= book_obj.authors.add(*[])
数据库表纪录生成如下:
book表
book_authors表
核心:book_obj.authors.all()是什么?
多对多关系其它常用API:
book_obj.authors.remove() # 将某个特定的对象从被关联对象集合中去除。 ====== book_obj.authors.remove(*[]) book_obj.authors.clear() #清空被关联对象集合 book_obj.authors.set() #先清空再设置
基于对象的跨表查询
一对多查询(Publish 与 Book)
正向查询(按字段:publish):
1
2
3
4
|
# 查询主键为1的书籍的出版社所在的城市 book_obj = Book.objects. filter (pk = 1 ).first() # book_obj.publish 是主键为1的书籍对象关联的出版社对象 print (book_obj.publish.city) |
反向查询(按表名:book_set):
1
2
3
4
5
|
publish = Publish.objects.get(name = "苹果出版社" ) #publish.book_set.all() : 与苹果出版社关联的所有书籍对象集合 book_list = publish.book_set. all () for book_obj in book_list: print (book_obj.title) |
一对一查询(Author 与 AuthorDetail)
正向查询(按字段:authorDetail):
1
2
|
egon = Author.objects. filter (name = "egon" ).first() print (egon.authorDetail.telephone) |
反向查询(按表名:author):
1
2
3
4
5
|
# 查询所有住址在北京的作者的姓名 authorDetail_list = AuthorDetail.objects. filter (addr = "beijing" ) for obj in authorDetail_list: print (obj.author.name) |
多对多查询 (Author 与 Book)
正向查询(按字段:authors):
1
2
3
4
5
6
|
# 金瓶眉所有作者的名字以及手机号 book_obj = Book.objects. filter (title = "金瓶眉" ).first() authors = book_obj.authors. all () for author_obj in authors: print (author_obj.name,author_obj.authorDetail.telephone) |
反向查询(按表名:book_set):
1
2
3
4
5
6
|
# 查询egon出过的所有书籍的名字 author_obj = Author.objects.get(name = "egon" ) book_list = author_obj.book_set. all () #与egon作者相关的所有书籍 for book_obj in book_list: print (book_obj.title) |
注意:
你可以通过在 ForeignKey() 和ManyToManyField的定义中设置 related_name 的值来覆写 FOO_set 的名称。例如,如果 Article model 中做一下更改:
1
|
publish = ForeignKey(Book, related_name = 'bookList' ) |
那么接下来就会如我们看到这般:
1
2
3
4
|
# 查询 人民出版社出版过的所有书籍 publish = Publish.objects.get(name = "人民出版社" ) book_list = publish.bookList. all () # 与人民出版社关联的所有书籍对象集合 |