标签:mys asc 引擎 positive container eth import ffffff img
from django.db import models
?
?
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)
?
?
class AuthorDetail(models.Model):
?
nid = models.AutoField(primary_key=True)
birthday=models.DateField()
telephone=models.BigIntegerField()
addr=models.CharField( max_length=64)
?
?
class Publish(models.Model):
nid = models.AutoField(primary_key=True)
name=models.CharField( max_length=32)
city=models.CharField( max_length=32)
email=models.EmailField()
?
?
class Book(models.Model):
nid = models.AutoField(primary_key=True)
title = models.CharField( max_length=32)
publishDate=models.DateField()
price=models.DecimalField(max_digits=5,decimal_places=2)
?
# 与Publish建立一对多的关系,外键字段建立在多的一方
publish=models.ForeignKey(to="Publish",to_field="nid",on_delete=models.CASCADE)
# 与Author表建立多对多的关系,ManyToManyField可以建在两个模型中的任意一个,自动创建第三张表
authors=models.ManyToManyField(to=‘Author‘,)
?
x
# 一对多创建表
# 方式一: 通过实例一个对象
publish_obj = Publish.objects.get(pk=1)
book_obj = Book.objects.create(title="西游记", publishDate="2013-03-03", publish=publish_obj, price=100.00)
?
# 方式二: 通过外键字段 + __id
book_obj = Book.objects.create(title="西游记", publishDate="2012-12-12", price=100.00, publish_id=1)
x
# 正向查询(按字段:publish):
book_obj = Book.objects.get(title="西游记").publish.city
print(book_obj)
# 反向查询(按表名:book_set):
book_list = Publish.objects.get(id=1).book_set.all()
for book_obj in book_list:
print(book_obj.title)
# 正向查询(按字段:authorDetail):
?
egon=Author.objects.filter(name="egon").first()
print(egon.authorDetail.telephone)
?
?
# 反向查询(按表名:author):
# 查询所有住址在北京的作者的姓名
authorDetail_list=AuthorDetail.objects.filter(addr="beijing")
for obj in authorDetail_list:
print(obj.author.name)
author_obj = AuthorDetail.objects.get(pk=1).author.name
print(author_obj)
# 正向查询(按字段:authors)
# 金瓶眉所有作者的名字以及手机号
author_list = Book.objects.get(title="金瓶眉").authors.all()
print(author_list)
for author in author_list:
print(author.name, author.authorDetail.telephone)
# 反向查询(按表名:book_set):
# 查询egon出过的所有书籍的名字
book_list = Author.objects.get(name="yuan").book_set.all()
for book in book_list:
print(book.title)
?
# 练习: 查询苹果出版社出版过的所有书籍的名字与价格(一对多)
?
# --------------------基于Queryset的跨表查询------------------
# 一对多查询
# 反向查询
# 练习: 查询苹果出版社出版过的所有书籍的名字与价格(一对多)
book_list = Publish.objects.filter(name="苹果出版社").values("book__title", "book__price")
print(book_list)
?
# 正向查询Book中的字段
book_list = Book.objects.filter(publish__name="苹果出版社").values("title", "price")
print(book_list)
?
?
# 添加了related_name: publish=models.ForeignKey(to="Publish", to_field="nid", related_name="pub", on_delete=models.CASCADE)
# 反向查询添加了related_name
book_list = Publish.objects.filter(name="苹果出版社").values("books__title", "books__price")
print(book_list)
# 练习: 查询alex出过的所有书籍的名字(多对多)
# 反向
book_obj = Author.objects.filter(name="alex").values("book__title")
print(book_obj)
?
# 正向
book_list = Book.objects.filter(authors__name="alex").values("title")
print(book_list)
# 正向查询
ret=Author.objects.filter(name="alex").values("authordetail__telephone")
?
# 反向查询
ret=AuthorDetail.objects.filter(author__name="alex").values("telephone")
# 查询苹果出版社出版过的所有书籍的名字以及作者的姓名
?
ret = Book.objects.filter(publish__name="苹果出版社").values("title", "authors__authorDetail__addr")
print(ret)
?
# 练习: 手机号以133开头的作者出版过的所有书籍名称以及出版社名称
?
ret = Book.objects.filter(authors__authorDetail__telephone__regex="133").values("title", "publish__name")
print(ret)
?
from django.db.models import Avg, Sum, Max, Min, Count
?
?
avg_price = Book.objects.all().aggregate(Avg("price"))
print(avg_price)
>>> {‘price__avg‘: 19.79} # 返回一个字典
?
# 指定一个键
avg_price = (Book.objects.all().aggregate(print_avg=Avg("price")))
print(avg_price)
>>> {‘print_avg‘: 19.79}
?
price = Book.objects.all().aggregate(Avg("price"), Sum("price"), Max("price"))
print(price)
>>> {‘price__avg‘: 19.79, ‘price__sum‘: Decimal(‘98.95‘), ‘price__max‘: Decimal(‘29.00‘)}
# 查询每本书作者的个数
?
# select SUM(author_id), book_id from app01_author_books GROUP BY app01_author_books.book_id
ret = Book.objects.all().annotate(author_num=Count("author"))
print(ret)
# <QuerySet [<Book: 跟pontoon学python>, <Book: pontoon想跟dandylee谈朋友>, <Book: 特别想跟dandylee谈朋友>, <Book: pontoon一定要努力学好python>, <Book: 争取一入行就跟取加dandylee的微信>]>
?
注意这里!他相当于给Book表增加了一个字段author_num(作者的数量)
for book in ret:
print("书名:{}, 作者数量:{}".format(book.title, book.author_num))
>>> 书名:特别想跟dandylee谈朋友, 作者数量:2
书名:pontoon一定要努力学好python, 作者数量:0
书名:争取一入行就跟取加dandylee的微信, 作者数量:0
--------------------------------------------------------------------------------------------------------------------------------
# 查询作者数量大于1的书
ret = Book.objects.all().annotate(author_num=Count("author")).filter(author_num__gt=1)
print(ret)
?
>>><QuerySet [<Book: 跟pontoon学python>, <Book: 特别想跟dandylee谈朋友>]>
# ret = Book.objects.all().annotate(author_num=Count("author"))得到的是一个查询集,可以用filter进行筛选(Having)
?
for book in ret:
print("书名: %s, 作者的个数: %s" % (book.title, book.author_num))
?
>>>书名: 跟pontoon学python, 作者的个数: 3
书名: 特别想跟dandylee谈朋友, 作者的个数: 2
?
class Book(models.Model):
id = models.AutoField(primary_key=True)
price = models.DecimalField(max_digits=5, decimal_places=2, default=0.00)
# 库存
kucun = models.IntegerField(default=0)
# 售出
sail = models.IntegerField(default=0)
title = models.CharField(max_length=32)
publisher = models.ForeignKey(to="Publisher", to_field=‘id‘, on_delete=models.CASCADE, related_name="books")
# 查询卖出数卖出数小于100 价格小于10.00的书
ret = Book.objects.filter(Q(sail__lt=100) | Q(price__lt=10.00))
print(ret)
# 查询# 查询卖出数卖出数小于100 价格小于10.00的书,书名包含pontoon的书
ret = Book.objects.filter(Q(sail__lt=100) | Q(price__lt=10.00), title__contains="pontoon")
print(ret)
标签:mys asc 引擎 positive container eth import ffffff img
原文地址:https://www.cnblogs.com/pontoon/p/10216862.html