ORM所有查询基础代码示例:
序列化文件
serializers.py:
class UserSer(serializers.Serializer):
username = serializers.CharField(max_length=64)
age = serializers.IntegerField()
home = serializers.CharField(max_length=64)
hight = serializers.CharField(max_length=64)
# classrooms = serializers.PrimaryKeyRelatedField(read_only=True)
# classrooms = serializers.StringRelatedField(read_only=True)
classrooms = ClassroomSer(read_only=True)
def create(self, validated_data):
return User.objects.create(**validated_data)
def update(self, instance, validated_data):
pass
视图文件
views.py:
class UserShowView(APIView):
def get(self, request):
# 查询年龄等于age的数据----------------------
age = request.query_params.get('age')
user = User.objects.filter(age=age)
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)
# 查询年龄等于age的数据的个数-----------------------
# user = request.query_params.get('age')
# print(user_obj)
# user_obj = User.objects.filter(age=user).count()
# print(user)
# return HttpResponse('年龄为 %s 的个数有: %s 个' % (user_obj, user))
# 查询年龄不等于age的全部数据------------------------
# age = request.query_params.get('age')
# user = User.objects.exclude(age=age)
# user_obj = UserSer(user, many=True)
# return Response(user_obj.data)
# 查询一个字段是否包含要查询的关键字------------------------
# user = User.objects.filter(home__contains='河')
# user_obj = UserSer(user, many=True)
# return Response(user_obj.data)
# startswith,endswith 以指定值开头或结尾------------------------------
# user = User.objects.filter(home__startswith='东')
# user_obj = UserSer(user, many=True)
# return Response(user_obj.data)
# startswith,endswith 以指定值开头或结尾-----------------------
# user = User.objects.filter(home__endswith='州')
# user_obj = UserSer(user, many=True)
# return Response(user_obj.data)
# 查询一个字段是否为空-------------------------------
# user = User.objects.filter(age__isnull=True)
# user_obj = UserSer(user, many=True)
# return Response(user_obj.data)
# 范围查询-------------------------------
# in 在范围内 range 相当于between...and...
# user = User.objects.filter(age__in=[17, 19, 21, 22])
# user_obj = UserSer(user, many=True)
# return Response(user_obj.data)
# user = User.objects.filter(age__range=[18, 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)
# user_obj = User.objects.filter(Q(hight__gte=180) & Q(hight__lte=190) | Q(age__lt=19))
# user_ser = UserSer(user_obj, many=True)
# return Response(user_ser.data)
# 聚合函数---------------------------------------
# 使用aggregate()过滤器调用聚合函数,聚合函数包括 Avg 平均,Count 数量,Max 最大,Min 最小,Sum 求和
# user = User.objects.aggregate(sum=Sum('age'))
# return Response(user)
# user = User.objects.aggregate(count=Count('age'))
# return Response(user)
# user = User.objects.aggregate(avg=Avg('age'))
# return Response(user)
# user = User.objects.aggregate(min=Min('age'))
# return Response(user)
# user = User.objects.aggregate(max=Max('age'))
# return Response(user)
# 多对一的查询,多表查一表的数据------------------------------------
# user_obj = User.objects.get(id=1)
# classname = user_obj.classrooms.class_name
# return JsonResponse({'classname': classname})
# user_obj = User.objects.filter(classrooms_id=1)
# print(user_obj)
# list1 = []
# for i in user_obj:
# classname = i.classrooms.class_name
# list1.append(classname)
# return JsonResponse({'classname': list1})
# 一对多的查询,一表查多表的数据-----------------------------------------
# classroom_obj = Classroom.objects.get(class_name='Python')
# print(classroom_obj)
# # student_obj = classroom_obj.user_set.all()
# student_obj = classroom_obj.users.all()
# list1 = []
# for i in student_obj:
# list1.append(i.username)
# return JsonResponse({'学生有': list1})