ORM练习题:

models:

from django.db import models# Create your models here.# 书class Book(models.Model):    title = models.CharField(max_length=32)    publish_date = models.DateField(auto_now_add=True)    price = models.DecimalField(max_digits=5, decimal_places=2)    memo = models.TextField(null=True)    # 创建外键,关联publish、关键字传参to="Publisher"    publisher = models.ForeignKey(to="Publisher")    # 创建多对多关联author    author = models.ManyToManyField(to="Author")    def __str__(self):        return "<Book object: {} {}>".format(self.id, self.title)# 出版社class Publisher(models.Model):    name = models.CharField(max_length=32)    city = models.CharField(max_length=32)    def __str__(self):        return "<Publisher object: {} {}>".format(self.id, self.name)# 作者class Author(models.Model):    name = models.CharField(max_length=32)    age = models.IntegerField()    phone = models.CharField(max_length=11)    def __str__(self):        return "<Author object: {} {}>".format(self.id, self.name)练习题操作:
import osos.environ.setdefault("DJANGO_SETTINGS_MODULE","ORM_homework.settings")import djangodjango.setup()from app01 import modelsfrom django.db.models import Q,Max,Min,Count,Sum,Avg查找所有书名里包含金老板的书ret = models.Book.objects.filter(title__contains="金老板")结果:<QuerySet [<Book: <Book object: 1 跟金老板学开车>>, <Book: <Book object: 2 跟金老板学开潜艇>>]>查找出版日期是2018年的书ret = models.Book.objects.filter(publish_date__year="2018")结果:<QuerySet [<Book: <Book object: 1 跟金老板学开车>>, <Book: <Book object: 3 跟老男孩学思想>>, <Book: <Book object: 4 跟egon学喊麦>>]>查找出版日期是2017年的书名ret = models.Book.objects.filter(publish_date__year="2017").values("title")结果:<QuerySet [{‘title‘: ‘跟金老板学开潜艇‘}]>查找价格大于10元的书ret = models.Book.objects.filter(price__gt=10)结果:<QuerySet [<Book: <Book object: 1 跟金老板学开车>>, <Book: <Book object: 3 跟老男孩学思想>>]>查找价格大于10元的书名和价格ret = models.Book.objects.filter(price__gt=10).values("title","price")结果:<QuerySet [{‘title‘: ‘跟金老板学开车‘, ‘price‘: Decimal(‘12.90‘)}, {‘title‘: ‘跟老男孩学思想‘, ‘price‘: Decimal(‘39.99‘)}]>查找memo字段是空的书ret = models.Book.objects.filter(Q(memo__isnull=True)|Q(memo=""))结果:<QuerySet [<Book: <Book object: 1 跟金老板学开车>>, <Book: <Book object: 2 跟金老板学开潜艇>>, <Book: <Book object: 3 跟老男孩学思想>>, <Book: <Book object: 4 跟egon学喊麦>>]>查找在北京的出版社查找名字以沙河开头的出版社查找“沙河出版社”出版的所有书籍查找每个出版社出版价格最高的书籍价格ret = models.Book.objects.values("publisher").annotate(Max("price"))结果:<QuerySet [{‘publisher‘: 1, ‘price__max‘: Decimal(‘12.90‘)}, {‘publisher‘: 2, ‘price__max‘: Decimal(‘39.99‘)}, {‘publisher‘: 4, ‘price__max‘: Decimal(‘0.99‘)}]>查找每个出版社的名字以及出的书籍数量ret = models.Book.objects.values("publisher__name").annotate(Count("pk"))结果:<QuerySet [{‘publisher__name‘: ‘沙河出版社‘, ‘pk__count‘: 3}, {‘publisher__name‘: ‘西二旗出版社‘, ‘pk__count‘: 1}]>查找作者名字里面带“小”字的作者查找年龄大于30岁的作者查找手机号是155开头的作者查找手机号是155开头的作者的姓名和年龄查找每个作者写的价格最高的书籍价格ret = models.Book.objects.values("author").annotate(Max("price"))结果:<QuerySet [{‘author‘: 1, ‘price__max‘: Decimal(‘12.90‘)}, {‘author‘: 2, ‘price__max‘: Decimal(‘12.90‘)}, {‘author‘: 3, ‘price__max‘: Decimal(‘39.99‘)}]>查找每个作者的姓名以及出的书籍数量ret = models.Author.objects.annotate(count=Count("book__title")).values("count","name")结果:<QuerySet [{‘name‘: ‘金老板‘, ‘count‘: 2}, {‘name‘: ‘小哪吒‘, ‘count‘: 2}, {‘name‘: ‘Alex‘, ‘count‘: 2}]>查找书名是“跟金老板学开车”的书的出版社查找书名是“跟金老板学开车”的书的出版社所在的城市查找书名是“跟金老板学开车”的书的出版社的名称查找书名是“跟金老板学开车”的书的出版社出版的其他书籍的名字和价格pub_obj = models.Publisher.objects.filter(book__title="跟金老板学开车").first()ret = pub_obj.book_set.exclude(title="跟金老板学开车").values("title","price")结果:<QuerySet [{‘title‘: ‘跟金老板学开潜艇‘, ‘price‘: Decimal(‘9.99‘)}]>查找书名是“跟金老板学开车”的书的所有作者查找书名是“跟金老板学开车”的书的作者的年龄查找书名是“跟金老板学开车”的书的作者的手机号码查找书名是“跟金老板学开车”的书的作者们的姓名以及出版的所有书籍名称和价钱authors = models.Author.objects.filter(book__title="跟金老板学开车")for author in authors:    print(author.name)    for i in author.book_set.all().values("title","price"):        print(i)结果:  金老板        {‘title‘: ‘跟金老板学开车‘, ‘price‘: Decimal(‘12.90‘)}        {‘title‘: ‘跟金老板学开潜艇‘, ‘price‘: Decimal(‘9.99‘)}        小哪吒        {‘title‘: ‘跟金老板学开潜艇‘, ‘price‘: Decimal(‘9.99‘)}        {‘title‘: ‘跟金老板学开车‘, ‘price‘: Decimal(‘12.90‘)}

相关推荐