python-20 Django中的mysql增删改查

class BookView(View):
    #查询
    def get(self,request):
        blist=BookInfo.objects.all()                                                                              # 所有数据
        blist=BookInfo.objects.filter(pk__exact=3)                                                  # id等于3
        blist=BookInfo.objects.filter(pk=3)                                                                 # id等于3
        blist=BookInfo.objects.filter(btitle__contains='记')                                     # btitle中包含‘记’
        blist=BookInfo.objects.filter(btitle__endswith='记')                                     # bittle结尾以‘记’结尾
        blist=BookInfo.objects.exclude(btitle__endswith='记')                               # btitle不以‘记’结尾
        blist=BookInfo.objects.filter(btitle__isnull=False)                                        # btitle不为空
        blist=BookInfo.objects.filter(pk__in=[1,3,4])                                                 # id是1,3,4
        blist=BookInfo.objects.filter(pk__in=[1,4])                                                    # id是1,4
        blist=BookInfo.objects.filter(pk__gt=3)                                                          # id大于3
        blist=BookInfo.objects.exclude(pk__gt=3)                                                    # id不大于3(小于等于3)
        blist=BookInfo.objects.filter(pk__lt=3)                                                           # id小于3
        blist=BookInfo.objects.filter(bpub_date__year=1980)                              # 发行日期的年份是1980
        blist=BookInfo.objects.filter(bpub_date__lt=date(1995,1,1))                  # 发行日期小于1995.1.1

        blist=BookInfo.objects.filter(bread__gte=F('bcomment'))                         # 阅读量 大于等于 评论量
        blist=BookInfo.objects.filter(bread__gt=F('bcomment')*2)                       # 阅读量 大于 评论量的2倍

        blist=BookInfo.objects.filter(Q(pk__gt=2)&Q(bcomment__gt=3))            # 逻辑与
        blist=BookInfo.objects.filter(Q(pk__gt=2,bcomment__gt=3))                   # 逻辑与
        blist=BookInfo.objects.filter(Q(pk__gt=4)|Q(pk__lt=3))                              # 逻辑或
        blist=BookInfo.objects.filter(~Q(pk=4))                                                          # 逻辑非

        bread_sum=BookInfo.objects.aggregate(Sum('bread'))
        print(bread_sum)

        book_count=BookInfo.objects.aggregate(Count('pk'))
        # book_count=BookInfo.objects.count()
        print(book_count)

        blist=BookInfo.objects.all().order_by('bread')
        blist=BookInfo.objects.all().order_by('-bread')
        blist=BookInfo.objects.all().order_by('-bread').order_by('-pk')


        return render(request,'book.html',{"blist":blist})
        pass


    #增加(推荐方式一,因为方式二是先update,再insert)
    def post(self,request):
        #第一种方式
         book=BookInfo.objects.create(
             btitle='牧神记',
             bpub_date=date(2017,11,18)
         )
        #第二种方式
        # book=BookInfo()
        # book.btitle='西行纪'
        # book.bpub_date=date(2017,5,14)
        # book.save()
        # return HttpResponse(book.btitle)
        pass

    #修改(推荐方式二,因为方式二是先update,再insert)
    def put(self,request):
        # 第一种方式
        # BookInfo.objects.filter(pk=41).update(btitle='斗破苍穹')
        # return HttpResponse('ok')
        # 第二种方式
        book=BookInfo.objects.get(pk=3)
        book.btitle='笑傲江湖'
        book.save()
        return HttpResponse('ok')
        pass

    #删除
    def delete(self,request):
        #第一种方法,删除多条符合条件的数据
        # BookInfo.objects.filter(pk__gt=4).delete()
        #第二种方法,删除单条数据
        book=BookInfo.objects.get(pk=41)
        book.delete()
        return HttpResponse('ok')
        pass

你可能感兴趣的:(python,编程)