python Django中models进行模糊查询的示例

  • 时间:
  • 浏览:46

多个字段恍惚查询, 括号中的下划线是单下划线,单下划线前是字段名,单下划线后能够是icontains或contains,区分是是不是巨细写敏感,横线是或的意义

#搜刮功用
@csrf_exempt#利用@csrf_exempt装潢器,免去csrf考证
def search_testCaseApi(request):
  if request.method == 'POST':
    name = request.POST.get('task_name')
    updateUser=request.POST.get('task_updateUser')
    if name=="" and updateUser=="":
      obj_all = tnw_test_case_api.objects.filter(del_flag=0)
    elif name!="" and updateUser=="":
      obj_all = tnw_test_case_api.objects.filter(del_flag=0,case_name__contains=name)
    elif name=="" and updateUser!="":
      obj_all = tnw_test_case_api.objects.filter(del_flag=0,update_user__contains=updateUser)
    else:
      obj_all = tnw_test_case_api.objects.filter(del_flag=0,case_name__contains=name,update_user__contains=updateUser)
    ApiCasesList = []
    for li in obj_all:
      need_inte***cename = allFunction().get_inte***ceName(li.id)
      api_list, api_sum = allFunction().testIDConnect_needid(li.id)
      if li.case_module is not None:
        ApiCasesList.append({
          "testCaseApi_id": li.id,
          "testCaseApi_name": li.case_name,
          "testCaseApi_sum": api_sum,
          "testCaseApi_version": li.case_version,
          "testCaseApi_module": li.case_module,
          "testCaseApi_need_inte***cename": need_inte***cename,
          "testCaseApi_createTime": str(li.create_time),
          "testCaseApi_updateTime": str(li.update_time),
          "testCaseApi_updateUser": li.update_user,
        })
      else:
        ApiCasesList.append({
          "testCaseApi_id": li.id,
          "testCaseApi_name": li.case_name,
          "testCaseApi_sum": 1,
          "testCaseApi_version": li.case_version,
          "testCaseApi_module": li.case_module,
          "testCaseApi_need_inte***cename": need_inte***cename,
          "testCaseApi_createTime": str(li.create_time),
          "testCaseApi_updateTime": str(li.update_time),
          "testCaseApi_updateUser": li.update_user,
        })
    # 将int范例利用dumps()办法转为str范例
    ApiCasesList_len = json.dumps(len(ApiCasesList))
    # 机关1个字典
    json_data_list = {'rows': ApiCasesList, 'total': ApiCasesList_len}
    # dumps()将字典改变为json情势,
    easyList = json.dumps(json_data_list)
    # 将json前往来,json的键值对中的键需求取前台的表格field=“X”中的X称号连结1致)
    return HttpResponse(easyList)

以上那篇Python Django中models停止恍惚查询的示例便是小编分享给各人的全数内容了,期望能给各人1个参考,也期望各人多多撑持剧本之家。