zoukankan      html  css  js  c++  java
  • 做题

    from django.shortcuts import render,HttpResponse
    from Af_Pr import models
    from django.db.models import Avg,Max,Min,Count,Sum
    from django.db.models import F,Q

    def query(request):
    # book_obj = models.Book.objects.filter(id=1)[0]
    # book_obj.authors.add(1,2)
    book_obj1 = models.Book.objects.filter(id=3)[0]
    book_obj1.authors.add(2)

    链表操作 用属性

    # print(models.Book.objects.filter(id=2),models.Book.objects.filter(id=2).values())   #<QuerySet [<Book: 小王子>]> <QuerySet [{'id': 2, 'title': '小王子', 'price': Decimal('44.00'), 'zan': 32, 'comment': 22, 'publishs_id': 2}]>
    # print(models.Book.objects.filter(id=2).values('title'))     #<QuerySet [{'title': '小王子'}]>
    # print(models.Book.objects.filter(id=2).values_list('title'))    #<QuerySet [('小王子',)]>
    # author_obj = models.Author.objects.filter(name='李继宏')[0]
    # print(models.Author.objects.filter(name='李继宏'),models.Author.objects.filter(name='李继宏')[0])   #<QuerySet [<Author: 李继宏   李继宏
    # print(models.Book.objects.filter(id=2)[0].authors.remove(author_obj))   # 可以根据这个作者名字删除,不是作者对象
    # print(models.Book.objects.filter(id=2).values('title').filter(id=2))    #<QuerySet [{'title': '小王子'}]>
    # print(models.Book.objects.values())     #<QuerySet [{'id': 1, 'title': '飞鸟集', 'price': Decimal('23.00'), 'zan': 33, 'comment': 45, 'publishs_id': 1}, {'id': 2, 'title': '小王子', 'price': Decimal('44.00'), 'zan': 32, 'comment': 22, 'publishs_id': 2}, {'id': 3, 'title': '边城', 'price': Decimal('12.00'), 'zan': 3, 'comment': 222, 'publishs_id': 3}]>
    # print(models.Book.objects.values('title'))      #<QuerySet [{'title': '飞鸟集'}, {'title': '小王子'}, {'title': '边城'}]>
    
    # print(models.Book.objects.values('title').annotate(Avg('')))
    
    # obj = models.Book.objects.filter(authors__name='李继宏').values('title', 'authors__book__authors')
    # print(obj)    # 查看的是作者写的那本书的所有作者    #<QuerySet [{'title': '飞鸟集', 'authors__book__authors': 1}, {'title': '飞鸟集', 'authors__book__authors': 2}, {'title': '飞鸟集', 'authors__book__authors': 2}, {'title': '边城', 'authors__book__authors': 1}, {'title': '边城', 'authors__book__authors': 2}, {'title': '边城', 'authors__book__authors': 2}]>
    # print(models.Book.objects.filter(authors__name='李继宏').values('title','authors'))    #<QuerySet [{'title': '飞鸟集', 'authors': 2}, {'title': '边城', 'authors': 2}]>
    # print(models.Book.objects.filter(authors__name='李继宏'))  # <QuerySet [<Book: 飞鸟集>, <Book: 边城>]>
    

    下午

    # 一对多查询 正向查询(用属性)    查询 出版社 译林出版过的所有书籍的名字与价格
    print(models.Book.objects.filter(publishs__name='译林').values_list('title','price'))    # <QuerySet [('飞鸟集', Decimal('23.00'))]>
    #反向查询
    print(models.Publish.objects.filter(name='译林').values_list('bp1__title','bp1__price'))
    #查询 书籍的出版社的名字
    print(models.Book.objects.filter(title='小王子').values_list('publishs__name'))    #<QuerySet [('人民邮电',)]>
    #多对多查询
    #正向  查询作者 李继宏 写作的所有书籍的名字
    print(models.Author.objects.filter(name='泰戈尔').values_list('book__title'))  #<QuerySet [('飞鸟集',), ('小王子',)]>
    # 反向  查询书籍 飞鸟集 的所有作者的电话
    print(models.Book.objects.filter(title='飞鸟集').values_list('authors__ad_id__telephone')) #<QuerySet [('123',), ('333',)]>
    
    # 连续跨表
    # 正向查询 出版社 译林的所有书籍的名字以及作者的名字
    # values_list 格式都是 :  #<QuerySet [('飞鸟集', '泰戈尔'), ('飞鸟集', '李继宏')]>
    print(models.Publish.objects.filter(name='译林').values('bp1__title','bp1__authors__name'))   #<QuerySet [{'bp1__title': '飞鸟集', 'bp1__authors__name': '泰戈尔'}, {'bp1__title': '飞鸟集', 'bp1__authors__name': '李继宏'}]>
    # 反向查询
    print(models.Book.objects.filter(publishs__name='译林').values('title','authors__name')) #    <QuerySet [{'title': '飞鸟集', 'authors__name': '泰戈尔'}, {'title': '飞鸟集', 'authors__name': '李继宏'}]>
    
    # 练习 :手机号以151 开头的作者 出版过的所有书籍名称 以及出版社名称
    print(models.AuthorDetail.objects.filter(telephone__startswith=123).values('author__book__title','author__book__publishs__name'))   #<QuerySet [{'author__book__title': '飞鸟集', 'author__book__publishs__name': '译林'}, {'author__book__title': '边城', 'author__book__publishs__name': '新华书店'}]>
    print(models.Author.objects.filter(ad_id__telephone__startswith=123).values('book__title','book__publishs__name'))
    
    # related_name      publishs = ForeignKey(to='Publish' related_name='bp1')
    
    
    # 聚合查询  #计算所有图书的平均价格
    # aggregate调用
    # models.Book.objects.aggregate(Avg('price')  也可以
    print(models.Book.objects.all().aggregate(Avg('price')))    #{'price__avg': 26.333333}
    print(models.Book.objects.aggregate(average_price=Avg('price')))    #{'average_price': 26.333333}
    print(models.Book.objects.aggregate(Avg('price'),Max('price'),Min('price')))    #{'price__avg': 26.333333, 'price__max': Decimal('44.00'), 'price__min': Decimal('12.00')}
    
    
    #  单表分组查询           value 分组条件    annotate  结果
    print(models.Book.objects.values('title').annotate(c= Count('price')))  #<QuerySet [{'title': '飞鸟集', 'c': 1}, {'title': '小王子', 'c': 1}, {'title': '边城', 'c': 1}]>
    # print(models.Book.objects.values('title').annotate(c= 'authors__name'))
    # 查询 每一个部门名称以及对应的员工数
    print(models.Author.objects.values('name').annotate(c = Count('book__title')))  #<QuerySet [{'name': '泰戈尔', 'c': 2}, {'name': '李继宏', 'c': 2}, {'name': '安东尼', 'c': 0}]>
    print(models.Author.objects.values('name').annotate(c=Count( 'book__title')).values('name'))         #<QuerySet [{'name': '泰戈尔'}, {'name': '李继宏'}, {'name': '安东尼'}]>
    print(models.Author.objects.values('name','age').annotate(c=Count('book__title')))  #<QuerySet [{'name': '泰戈尔', 'age': 78, 'c': 2}, {'name': '李继宏', 'age': 39, 'c': 2}, {'name': '安东尼', 'age': 56, 'c': 0}]>
    
    
    # 查询练习  1 统计每一个出版社最便宜的书
    print(models.Publish.objects.values('name','bp1__title').annotate(c=Min('bp1__price')))
    #<QuerySet [{'name': '译林', 'bp1__title': '飞鸟集', 'c': Decimal('23.00')}, {'name': '人民邮电', 'bp1__title': '小王子', 'c': Decimal('44.00')}, {'name': '新华书店', 'bp1__title': '边城', 'c': Decimal('12.00')}]>
    
    #2 练习:统计每一本书的作者个数
    print(models.Book.objects.values('title').annotate(c = Count('authors__id')))   #<QuerySet [{'title': '飞鸟集', 'c': 2}, {'title': '小王子', 'c': 1}, {'title': '边城', 'c': 1}]>
    #(3) 统计每一本以py开头的书籍的作者个数:
    print(models.Book.objects.filter(title__startswith='小').values('title').annotate(c = Count('authors__id'))) #<QuerySet [{'title': '小王子', 'c': 1}]>
    
    #  (4) 统计不止一个作者的图书:
    print(models.Book.objects.values('title').annotate(c=Count('authors__id')).filter(c__gt=0).values('title','c'))
        #<QuerySet [{'title': '飞鸟集', 'c': 2}, {'title': '小王子', 'c': 1}, {'title': '边城', 'c': 1}]>
    #(5) 根据一本图书作者数量的多少对查询集 QuerySet进行排序:
    print(models.Book.objects.values('title').annotate(c= Count('authors__id')).order_by('-c'))    #<QuerySet [{'title': '边城', 'c': 3}, {'title': '飞鸟集', 'c': 2}, {'title': '小王子', 'c': 1}]>
    
    #  (6) 查询各个作者出的书的总价格:
    print(models.Author.objects.values('name').annotate(Sum('book__price')))
            #<QuerySet [{'name': '泰戈尔', 'book__price__sum': Decimal('79.00')}, {'name': '李继宏', 'book__price__sum': Decimal('35.00')}, {'name': '安东尼', 'book__price__sum': Decimal('12.00')}]>
    

    F和Q查询

    F查询

    ## 查询评论数大于收藏数的书籍
    print(models.Book.objects.filter(comment__lt= F('zan')))    #<QuerySet [<Book: 小王子>]>
    #查询评论数大于收藏数2倍的书籍
    print(models.Book.objects.filter(comment__gt=F('zan')*2))   #<QuerySet [<Book: 边城>]>
    # 把所有的书籍价格 增加30
    # models.Book.objects.all().update(price=F('price')+30)
    

    Q 查询 &与 |或 ~非

    print(models.Book.objects.filter(Q(authors__name__startswith="李") | Q(authors__name__startswith="泰")).values('title'))
    a = models.Book.objects.filter(Q(authors__name__startswith="李") | Q(authors__name__startswith="泰")).values('title')
    # print(a.annotate(c = Max('price')))
    
    print(models.Book.objects.filter(Q(authors__name='泰戈尔') | Q(title='飞鸟集'), price__gt = 220 ))
    
    # 综合练习题
    #1 查询每个作者的姓名以及出版的书的最高价格
    print(models.Author.objects.values('name').annotate(c = Max('book__price')))    #<QuerySet [{'name': '泰戈尔', 'c': Decimal('224.00')}, {'name': '李继宏', 'c': Decimal('203.00')}, {'name': '安东尼', 'c': Decimal('192.00')}]>
    
    
    # 2 查询作者id大于2作者的姓名以及出版的书的最高价格
    print(models.Author.objects.filter(id__gt=2).values('name').annotate(c = Max('book__price')))   #<QuerySet [{'name': '安东尼', 'c': Decimal('192.00')}]>
    
    # 3 查询作者id大于2或者作者年龄大于等于20岁的女作者的姓名以及出版的书的最高价格
    print(models.Author.objects.filter(Q(id__gt=2) | Q(age__gte=30),name__startswith='李').values('name').annotate(c = Max('book__price')))
                                                                                              #<QuerySet [{'name': '李继宏', 'c': Decimal('203.00')}]>
    #4 查询每个作者出版的书的最高价格 的平均值
    print(models.Author.objects.values('name'))     #<QuerySet [{'name': '泰戈尔'}, {'name': '李继宏'}, {'name': '安东尼'}]>
    a = models.Author.objects.values('name').annotate(c = Max('book__price'))   #<QuerySet [{'name': '泰戈尔', 'c': Decimal('224.00')}, {'name': '李继宏', 'c': Decimal('203.00')}, {'name': '安东尼', 'c': Decimal('192.00')}]>
    print(models.Author.objects.values('name').annotate(c=Max('book__price')).aggregate(Avg('c')))  #{'c__avg': 206.333333}
    
    # 5 每个作者出版的所有书的价格以及最高价格的那本书的名称 (通过orm玩起来就是个死题,需要用原生sql)
    # print(models.Author.objects.values('name').annotate(c= Max('book__price')).values_list('name','book__title','c'))
    # print(models.Book.objects.values('authors__ad_id_id').annotate(m = Max('price')).values('authors__name,title'))
    # print(models.Book.objects.values_list('authors__name','price').order_by('authors__id') )
    # print(models.Book.objects.values_list('authors__name','price').order_by( '-price').filter(Q(price=224)|Q()).values('title'))
    # print(models.Book.objects.filter())
    # print(models.Book.objects.filter(Q(authors__id=1)|Q(authors__id=2)|Q(authors__id=3)).values_list('authors__name','price'))
    # print(models.Book.objects.filter(Q(authors__id=1)|Q(authors__id=2)|Q(authors__id=3)).values_list('authors__name','price'))
    
    print(models.Book.objects.annotate(c= Max('price')).values_list('authors__name','title'))
    return HttpResponse('ok')
    

    def order(request):

    for i in range(3300):

    print(i)

    return render(request,'order.html',locals())

  • 相关阅读:
    关于JAVA中的static方法、并发问题以及JAVA运行时内存模型
    【设计模式】抽象工厂模式
    spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明
    struts2.3.24 + spring4.1.6 + hibernate4.3.11+ mysql5.5.25开发环境搭建及相关说明
    git中Please enter a commit message to explain why this merge is necessary.
    扒一扒开源世界有哪些licenses?
    string.Format出现异常:输入字符串的格式不正确 Exception during StringFormat
    node-glob学习
    js中对URL进行转码与解码
    程序员如何修炼管理思维
  • 原文地址:https://www.cnblogs.com/Doner/p/10940863.html
Copyright © 2011-2022 走看看