多個(gè)字段模糊查詢, 括號(hào)中的下劃線是雙下劃線,雙下劃線前是字段名, 雙下劃線后 可以是icontains或contains,區(qū)別是是否大小寫敏感,豎線是或的意思
#搜索功能 @csrf_exempt#使用@csrf_exempt裝飾器,免除csrf驗(yàn)證 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()方法轉(zhuǎn)為str類型 ApiCasesList_len = json.dumps(len(ApiCasesList)) # 構(gòu)造一個(gè)字典 json_data_list = {'rows': ApiCasesList, 'total': ApiCasesList_len} # dumps()將字典轉(zhuǎn)變?yōu)閖son形式, easyList = json.dumps(json_data_list) # 將json返回去,json的鍵值對(duì)中的鍵需要與前臺(tái)的表格field=“X”中的X名稱保持一致) return HttpResponse(easyList)
以上這篇python Django中models進(jìn)行模糊查詢的示例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
