欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

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

时间:2021-09-26 10:16:12|栏目:Python代码|点击:

多个字段模糊查询, 括号中的下划线是双下划线,双下划线前是字段名,双下划线后可以是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_interfacename = allFunction().get_interfaceName(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_interfacename": need_interfacename,
          "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_interfacename": need_interfacename,
          "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))
    # 构造一个字典
    json_data_list = {'rows': ApiCasesList, 'total': ApiCasesList_len}
    # dumps()将字典转变为json形式,
    easyList = json.dumps(json_data_list)
    # 将json返回去,json的键值对中的键需要与前台的表格field=“X”中的X名称保持一致)
    return HttpResponse(easyList)

上一篇:在ironpython中利用装饰器执行SQL操作的例子

栏    目:Python代码

下一篇:在PyCharm的 Terminal(终端)切换Python版本的方法

本文标题:python Django中models进行模糊查询的示例

本文地址:http://www.codeinn.net/misctech/177430.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有