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

yipeiwu_com5年前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)

以上这篇python Django中models进行模糊查询的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python彻底删除文件夹及其子文件方式

我就废话不多说了,直接上代码吧! #coding:utf-8 import os import stat import shutil #filePath:文件夹路径 def...

python实现各进制转换的总结大全

前言 玩ctf经常遇到进制转换的问题,就正好做一个进制转换总结,分享出来供大家参考学习,下面来一起看看详细的介绍: 字符串与十六进制转换 例如百度ctf 12月的第二场第一个misc...

Ubuntu 16.04 LTS中源码安装Python 3.6.0的方法教程

前提 官网上提供了 Mac 和 Windows 上的安装包和 Linux 上安装需要的源码。 下载地址如下: https://www.python.org/downloads/relea...

python机器学习实战之树回归详解

本文实例为大家分享了树回归的具体代码,供大家参考,具体内容如下 #-*- coding:utf-8 -*- #!/usr/bin/python ''''' 回归树 连续值回归...

Python实现批量更换指定目录下文件扩展名的方法

本文实例讲述了Python实现批量更换指定目录下文件扩展名的方法。分享给大家供大家参考,具体如下: #encoding=utf-8 #author: walker #date: 20...