• djangoDRF查询


    DRF全部查询基础示例

    	from django.db.models import Q, F, Sum, Avg, Count, Min, Max
            from rest_framework.response import Response
    	from rest_framework.views import APIView
    
    
    	class ShowView(APIView):
        	def get(self, request):
        
                # 查询年龄等于19的数据
                user = User.objects.filter(age=19)
                print(user)
                user_obj = UserSer(user, many=True)
                return Response(user_obj.data)
    
                # 查询全部数据
                user = User.objects.all()
                print(user)
                user_obj = UserSer(user, many=True)
                return Response(user_obj.data)
    
                # 查询年龄等于19的数据的个数
                user = User.objects.filter(age=19).count()
                print(user,'--------------------------------')
                return Response('ok')
    
                # 查询年龄不等于19的全部数据
                user = User.objects.exclude(age=19)
                print(user)
                user_obj = UserSer(user, many=True)
                return Response(user_obj.data)
    
                # 查询一个字段是否包含要查询的关键字
                user = User.objects.filter(classrooms__address__contains='庆')
                print(user)
                user_obj = UserSer(user, many=True)
                return Response(user_obj.data)
    
                # startswith,endswith 以指定值开头或结尾
                user = User.objects.filter(classrooms__address__startswith='昌')
                print(user)
                user_obj = UserSer(user, many=True)
                return Response(user_obj.data)
    
                user = User.objects.filter(classrooms__address__endswith='区')
                print(user)
                user_obj = UserSer(user, many=True)
                return Response(user_obj.data)
    
                # 查询一个字段是否为空
                user = User.objects.filter(age__isnull=False)
                user_obj = UserSer(user, many=True)
                return Response(user_obj.data)
    
                # 范围查询
                # in  在范围内    range  相当于between...and...
                user = User.objects.filter(age__in =[ 1, 13,18, 19, 22])
                user_obj = UserSer(user, many=True)
                return Response(user_obj.data)
    
                user = User.objects.filter(age__range=[10, 20])
                user_obj = UserSer(user, many=True)
                return Response(user_obj.data)
    
                # ⽐较查询:
                # gt      ⼤于
                # gte     ⼤于等于
                # lt      ⼩于
                # lte     ⼩于等于
                # exclude  不等于
    
                # F对象和Q对象
                # ⽐较两个字段对象之间的关系⽤F对象。(F对象可以进⾏运算)
                user = User.objects.filter(age__gte=F('hight'))
                user_obj = UserSer(user, many=True)
                return Response(user_obj.data)
    
                user = User.objects.filter(age__gte=F('hight') * 2)
                user_obj = UserSer(user, many=True)
                return Response(user_obj.data)
            	
                
                class HightsView(APIView):
    
        		def get(self, request):
            	user_obj = User.objects.filter(Q(hight__gte=150) and Q(hight__lte=170) | Q(age__lt=20))
            	
                    ser = UserSer(user_obj, many=True)
                    return Response(ser.data)
    
                # 聚合函数
                # 使⽤aggregate()过滤器调⽤聚合函数。聚合函数包括:Avg 平均,Count 数量,Max 最⼤,
                # Min 最⼩,Sum 求和
                user = User.objects.aggregate(sum=Sum('age'))
                print(user)
                return Response('ok')
    
                user = User.objects.aggregate(avg=Avg('age'))
                print(user)
                return Response('ok')
    
                user = User.objects.aggregate(count=Count('age'))
                print(user)
                return Response('ok')
    
                user = User.objects.aggregate(min=Min('age'))
                print(user)
                return Response('ok')
    
                user = User.objects.aggregate(max=Max('age'))
                print(user)
                return Response('ok')
    
  • 相关阅读:
    1.9
    在VS中添加lib库的三种方法
    第一章之位向量和位运算
    【转载】window下配置pthread的方法及出现问题的解决方法
    opencv环境配置问题
    box-shadow用法简介
    创建资源文件
    nhibernate Mybatisnet
    js中(function(){…})()立即执行函数写法理解
    最近项目中用到的js
  • 原文地址:https://www.cnblogs.com/-tender/p/14034679.html
Copyright © 2020-2023  润新知