聚合函数及分组查询及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)

posted @ 2019-01-16 21:29  彼岸花纽约  阅读(210)  评论(0编辑  收藏  举报