1、多表操做之一對多
from django.db import models
class Book(models.Model):
name = models.CharField(max_length=30)
price = models.FloatField()
pub_date = models.DateField()
publish = models.ForeignKey('Publish',on_delete='CASCADE') #外鍵關聯
def __str__(self):
return self.name
# 做者和書的關係是多對多的關係
class Author(models.Model):
name = models.CharField(max_length=20)
# 書和出版社的表是一對多的關係
class Publish(models.Model):
name = models.CharField(max_length=32)
city = models.CharField(max_length=32)
一、插入數據
Book.objects.create(name="Liunx運維手冊",price=99,pub_date="2018-08-08",publish_id=2) #直接對外鍵_id賦值
publish_obj = Publish.objects.filter(name="人民出版社")[0]
Book.objects.create(name="Liunx運維手冊2版",price=199,pub_date="2019-08-08",publish=publish_obj) #直接對外鍵賦值
二、查詢數據
# 查詢書籍是由哪一個出版社出版的,正向查詢
book_obj = Book.objects.get(name="python")
print(book_obj.name)
print(book_obj.pub_date)
print(book_obj.publish.name) #一對多中必定是一個對象
print(book_obj.publish.city)
print(type(book_obj.publish))
# 查詢出版社出過哪些書籍,反向查詢
pub_obj = Publish.objects.get(name="人民出版社")
print(pub_obj.book_set.all().values("name","price")) #多對一查到的是一個集合
# 查詢出版社出過哪些書
ret = Book.objects.filter(publish__name="人民出版社").values("name","price")
print(ret)
# 查詢書的出版社
ret = Publish.objects.filter(book__name="python").values("name")
print(ret)
ret = Book.objects.filter(name="python").values("publish__name")
print(ret)
2、多表操做之多對多(自動維護多對多關係表)
from django.db import models
# Create your models here.
class Book(models.Model):
name = models.CharField(max_length=30)
price = models.FloatField()
pub_date = models.DateField()
publish = models.ForeignKey('Publish',on_delete='CASCADE') #外鍵關聯
authors = models.ManyToManyField("Author") #建立表多對多的關係
def __str__(self):
return self.name
# 做者和書的關係是多對多的關係
class Author(models.Model):
name = models.CharField(max_length=20)
age = models.IntegerField(default=0)
def __str__(self):
return self.name
# 書和出版社的表是一對多的關係
class Publish(models.Model):
name = models.CharField(max_length=32)
city = models.CharField(max_length=32)
一、數據查找
# 經過對象的方式建立多對多的綁定關係
book_obj = Book.objects.get(id=3)
print(book_obj.authors.all()) #這裏拿到的authors是書對應做者的對象
author_obj = Author.objects.get(id=2)
print(author_obj.book_set.all())
ret = Book.objects.filter(authors__name="maria").values("name","price")
print(ret)
二、添加對應關係
book_obj = Book.objects.get(id=5) #獲取書的對象
author_objs = Author.objects.get(id=3) #獲取做者的對象
book_obj.authors.add(author_objs) #將做者對象加入到書的對應做者對象中,若是author_objs是一個集合則須要book_obj.authors.add(*author_objs)這樣寫
三、刪除對應關係
book_obj = Book.objects.get(id=5)
author_objs = Author.objects.get(id=3)
book_obj.authors.remove(author_objs)
3、多表操做之多對多(手動維護多對多關係表)
from django.db import models
# Create your models here.
class Book(models.Model):
name = models.CharField(max_length=30)
price = models.FloatField()
pub_date = models.DateField()
publish = models.ForeignKey('Publish',on_delete='CASCADE') #外鍵關聯
# authors = models.ManyToManyField("Author") #建立表多對多的關係
def __str__(self):
return self.name
# 做者和書的關係是多對多的關係
class Author(models.Model):
name = models.CharField(max_length=20)
age = models.IntegerField(default=0)
def __str__(self):
return self.name
# 建立做者和書的關係綁定表
class Book_Author(models.Model):
book = models.ForeignKey("Book",on_delete="CASCADE")
author = models.ForeignKey("Author",on_delete="CASCADE")
# 書和出版社的表是一對多的關係
class Publish(models.Model):
name = models.CharField(max_length=32)
city = models.CharField(max_length=32)
Book_Author.objects.create(book_id=1,author_id=2)
obj = Book.objects.get(id=1)
print(obj.book_author_set.all()[0].author)
ret = Book.objects.filter(book_author__author__name='maria').values("name","price")
print(ret)
4、聚合和分組查詢
from django.db.models import Avg,Min,Sum,Max,Count
print(Book.objects.all().aggregate(Avg("price"))) #求平均值
print(Book.objects.all().aggregate(Sum("price"))) #求和
print(Book.objects.filter(authors__name="maria").aggregate(Sum("price"))) #求和
print(Book.objects.filter(authors__name="maria").aggregate(Count("name")))
print(Book.objects.values("authors__name").annotate(Sum("price"))) #按照author_name分組後將price作求和
print(Publish.objects.values("name").annotate(Min("book__price")))
from django.db.models import Avg,Min,Sum,Max,Count,F
Book.objects.all().update(price=F("price")*2) #將書的價格翻一倍
from django.db.models import Avg,Min,Sum,Max,Count,F,Q
print(Book.objects.filter(Q(price=178)|Q(name="python"))) #或
print(Book.objects.filter(Q(price=178)| ~Q(name="python"))) #或非