django多条件查询

from django.core import serializers
from django.core.paginator import Paginator
from django.http import JsonResponse

def search_monitorItem(request):
    monitorItemInfos = []
    pageNum = 0
    total = 0
    try:
        monitorItemName = request.POST.get("monitorItemName", "")
        xx_id = request.POST.get("xx_id", "")
        monitorType = request.POST.get("monitorType", "")
        pageSize = int(request.POST.get("pageSize", "5"))

        # 多条件查询
        queryset = models.MonitorItemManage.objects.all()
        if monitorItemName:
            queryset = queryset.filter(monitorItemName__contains=monitorItemName)
        if monitorType:
            queryset = queryset.filter(monitorType=monitorType)
        if floatingPlatformId:
            queryset = queryset.filter(xx_ID_id=xx_id)

        # queryset = serializers.serialize('json', queryset)
        # return JSONResponse({'data': queryset})

        #
        for monitorItem in queryset:
            monitorItemInfo = {}
            monitorItemInfo["monitorItemId"] = monitorItem.id
            monitorItemInfo["itemIndex"] = monitorItem.itemIndex
            monitorItemInfo["monitorItemName"] = monitorItem.monitorItemName
            monitorItemInfo["monitorType"] = monitorItem.monitorType
            monitorItemInfo["unit"] = monitorItem.unit
            monitorItemInfo["updater"] = monitorItem.updater.userName
            monitorItemInfo["updateTime"] = datetimeformat(monitorItem.updateTime)
            monitorItemInfos.append(monitorItemInfo)
        # 分页
        page = Paginator(monitorItemInfos, pageSize)
        pageNum = page.num_pages
        total = page.count
    except Exception as e:
        print(e, e.__traceback__.tb_lineno)
    return JsonResponse({"monitorItemInfos": monitorItemInfos, "pageNum": pageNum, "total": total})

你可能感兴趣的:(django多条件查询)