zoukankan      html  css  js  c++  java
  • 聚合函数及分组查询及F&Q

    -related_name:基于双下划线的跨表查询,修改反向查询的字段

             -related_query_name:基于对象的跨表查询,修改反向查询字段

            

             -聚合查询

                       -aggregate(聚合函数)

             -分组查询:

                       '''

                                gruop by 谁就以谁做基表

                                filter在前,表示where条件

                                filter在后,表示havaing

                                values在前,表示group by的字段

                                values在后,表示取值

                       '''

                       查询每本书的作者个数

                       1 红楼梦

                       2 三国演义

                        

                       id book_id author_id

                       1     1        1

                       2     1        2

                       3     2        1

                      

                      

                       1 红楼梦  1  1  1

                       1 红楼梦  2  1  2

                      

                       2 三国演义3  2  1

                      

                       Book.objects.all().values(pk).annotate(c=Count(authors)).values('name','c')

                       Book.objects.all().annotate(c=Count(authors)).values('name','c')

                       红楼梦:2

                       三国演义:1

            

                       1       红楼梦     100         2019-01-01       1    1

                       1       红楼梦     100         2019-01-01       1    2

                       2       西游记     44      2018-09-12       2    3

                       3       西厢记     44      2018-07-12       2    2

                       4       三国演义 20      2018-07-12       1    3

                      

                      

             F和Q查询

                       -F:取出数据库某个字段的值

            

                       Q:构造出与(&)或(|)非(~)得关系

                      

             图书管理系统:

                       -for 循环中加if判断

                                {% for author in book.authors.all %}

                                         {% if forloop.last %}

                                                   {{ author.name }}

                                         {% else %}

                                                   {{ author.name }},

                                         {% endif %}

                                {% endfor %}

                       -渲染作者

                                <select name="authors" class="form-control" multiple>

                                {% for author in authors %}

                                {% if author in book.authors.all %}

                                         <option value="{{ author.pk }}" selected>{{ author.name }}</option>

                                {% else %}

                                         <option value="{{ author.pk }}">{{ author.name }}</option>

                                {% endif %}

                                {% endfor %}

                                </select>

                       -新增作者:

                                -先创建详情数据

                                -再往作者表中插入数据

                       -图书的新增和更新

                                -新增

                                         -authors=request.POST.getlist('authors')

                                         -book.authors.add(*authors)

                                -更新

                                         -第一种方式:拿到book对象,

                                                   authors=request.POST.getlist('authors')

                                                   book.name=name

                                                   book.save()

                                                   book.authors.set(authors)

                                         -第二种方式:queryset对象的update方法

                                                   -book_qu=Book.objects.filter(pk=pk)

                                                   -book_qu.update(name=name)

                                                   -book_qu.first().authors.set(authors)

  • 相关阅读:
    无法重用Linq2Entity Query
    The Joel Test
    MSBuilder directly instead of default VSComplie with keyborad shotcut 原创
    客户端缓存(Client Cache)
    关于代码重构和UT的一些想法,求砖头
    ExtJS2.0实用简明教程 应用ExtJS
    Perl information,doc,module document and FAQ.
    使用 ConTest 进行多线程单元测试 为什么并行测试很困难以及如何使用 ConTest 辅助测试
    史上最简单的Hibernate入门简介
    汽车常识全面介绍 传动系统
  • 原文地址:https://www.cnblogs.com/zhouhai007/p/10279523.html
Copyright © 2011-2022 走看看