解決Django中多條件查詢的問(wèn)題
tags: django中對(duì)條件查詢
一些cms項(xiàng)目都會(huì)使用到多條件查詢,我們后端如何處理請(qǐng)求的條件呢?
滿足一個(gè)條件
滿足兩個(gè)條件
滿足多個(gè)條件
………………….
這樣處理起來(lái)會(huì)非常的惱火. 其實(shí)有多方法比如(傳參數(shù),傳字典,傳Q對(duì)象,傳F對(duì)象…)陷入深深的思考中…怎么用做簡(jiǎn)單的方法把這個(gè)需求解決了.
個(gè)人覺(jué)得.把我們的查詢的所有條件來(lái)構(gòu)建一個(gè)字典來(lái)查詢起來(lái)比較高效.具體如何操作見下面的代碼:
視圖函數(shù).
def order_list(request):
if request.method == 'GET':
return render(request, 'admin/order_list.html')
if request.method == 'POST':
# 獲取案件號(hào)
case_order = request.POST.get('case_order')
# 獲取客戶姓名
case_name = request.POST.get('case_name')
# 獲取身份證號(hào)碼
idno = request.POST.get('idno')
# 獲取貸款日期
loan_date = request.POST.get('loan_date')
# 獲取貸款狀態(tài)
state = request.POST.get('state')
# 獲取貸款類型
dk_type = request.POST.get('dk_type')
# 定一個(gè)字典用于保存前端發(fā)送過(guò)來(lái)的查詢條件
search_dict = dict()
# 如果有這個(gè)值 就寫入到字典中去
if case_order:
search_dict['loan_id'] = case_order
if case_name:
search_dict['name'] = case_name
if idno:
search_dict['user_card'] = idno
if loan_date:
search_dict['pri_date'] = loan_date
if state:
# 通過(guò)關(guān)聯(lián)關(guān)系查詢出來(lái)需要的數(shù)據(jù)
state_info = StatuTable.objects.filter(statu_name=state).first()
search_dict['statu_id'] = state_info.statu_id
if dk_type:
loa = LoantypeTable.objects.filter(loan_name=dk_type).first()
search_dict['loa_loan_id'] = loa.loan_id
# 多條件查詢 關(guān)鍵點(diǎn)在這個(gè)位置傳如的字典前面一定要加上兩個(gè)星號(hào).
user_order_info = UserTable.objects.filter(**search_dict)
# 序列化
data_info = [user_order.to_dict() for user_order in user_order_info]
data = {
'code': 200,
'data_info': data_info
}
return JsonResponse(data)
Models見上一篇文章
前端html頁(yè)面
<head>
// 使用jquery就必須引入
<script src="/static/admin/js/jquery.js" type="text/javascript"></script>
// 需要使用ajaxSubmit去提交表單就必須引入
<script src="/static/admin/js/jquery.form.min.js" type="text/javascript"></script>
// 使用template.js渲染頁(yè)面就必須引入
<script src="/static/admin/js/template.js" type="text/javascript"></script>
<script src="/static/admin/js/order_list.js" type="text/javascript"></script>
</head>
<div class="wrap">
<div class="page-title">
<span class="modular fl"><i class="order"></i><em>查詢還款案件</em></span>
</div>
<div class="operate">
<form id="search-order">
{% csrf_token %}
<div>
<div style="margin: 10px">
<label for="">客戶單號(hào):</label>
<input type="text" class="textBox length-long " name="case_order" value=""/>
<label for="">客戶名稱:</label>
<input type="text" class="textBox length-long " name="case_name" value=""/>
</div>
<div style="margin: 10px">
<label for="">身份證號(hào):</label>
<input type="text" class="textBox length-long " name="idno" value=""/>
<label for="">貸款日期:</label>
<input type="text" class="textBox length-long" id="datepicker" name="loan_date" value=""/>
</div>
<div style="margin: 10px">
<label for="">處理狀態(tài):</label>
<select class="inline-select textBox length-long" name="state">
<option value="未處理">未處理</option>
<option value="已處理">已處理</option>
</select>
<label for="">貸款項(xiàng)目:</label>
<select class="inline-select textBox length-long" name="dk_type">
<option value="POS貸">POS貸</option>
<option value="現(xiàn)金貸">現(xiàn)金貸</option>
</select>
<div style="margin-right: 20px;margin-top: 10px;">
<input type="submit" value="查詢" class="tdBtn"/>
</div>
</div>
</div>
</form>
</div>
<table class="list-style Interlaced" id="test">
<tr>
<th>申請(qǐng)編號(hào)</th>
<th>客戶名稱</th>
<th>聯(lián)系方式</th>
<th>身份證號(hào)碼</th>
<th>辦理日期</th>
<th>處理人</th>
<th>處理狀態(tài)</th>
<th>處理時(shí)間</th>
<th>操作</th>
</tr>
{% verbatim %}
<script type="text/html" id="tr_list">
{{ each users as user }}
<tr>
<td>
<input type="checkbox"/>
<a href="/admin/order_detail/?id={{ user.user_id }}" rel="external nofollow" rel="external nofollow" style="text-decoration:underline; color: blue">
<span>{{ user.loan_id }}</span>
</a>
</td>
<td class="center">
<span class="block">{{ user.name }}</span>
</td>
<td width="200" style="text-align:center">
<span class="block">{{ user.phone }}</span>
</td>
<td class="center">
<span>{{ user.card }}</span>
</td>
<td class="center">
<span>{{ user.date }}</span>
</td>
<td class="center">
<span>{{ user.deal_peo }}</span>
</td>
<td class="center">
<span>{{ user.status }}</span>
</td>
<td class="center">
<span>{{ user.deal_time }}</span>
</td>
<td class="center">
<a href="/admin/order_detail/?id={{ user.user_id }}" rel="external nofollow" rel="external nofollow" class="inline-block" title="查看案件"><img
src="/static/admin/images/icon_view.gif"/></a>
<a class="inline-block" title="刪除案件">
<img src="/static/admin/images/icon_trash.gif"/>
</a>
</td>
</tr>
{{ /each }}
</script>
{% endverbatim %}
</table>
<!-- BatchOperation -->
<div style="overflow:hidden;">
<!-- Operation -->
<div class="BatchOperation fl">
<input type="checkbox" id="del"/>
<label for="del" class="btnStyle middle">全選</label>
<a href="/admin/export_excel/" rel="external nofollow" ><button id="export_excel" type="button" class="btnStyle" >導(dǎo)出excel</button></a>
<input type="button" value="刪除案件" class="btnStyle"/>
</div>
后端搞定了就可以在前端寫ajax去渲染頁(yè)面了.
$(function () {
var token = $(':input[name="csrfmiddlewaretoken"]').val()
$('#search-order').submit(function () {
$(this).ajaxSubmit({
url: '/admin/order_list/',
dataType: 'json',
type: 'POST',
headers: {'X-CSRFToken': token},
success: function (data) {
if (data.code == 200) {
var html ='<tr>\n' +
' <th>申請(qǐng)編號(hào)</th>\n' +
' <th>客戶名稱</th>\n' +
' <th>聯(lián)系方式</th>\n' +
' <th>身份證號(hào)碼</th>\n' +
' <th>辦理日期</th>\n' +
' <th>處理人</th>\n' +
' <th>處理狀態(tài)</th>\n' +
' <th>處理時(shí)間</th>\n' +
' <th>操作</th>\n' +
' </tr>'
var tr_html = template('tr_list', {users: data.data_info})
html += tr_html
$('#test').html(html)
}
}
})
// 阻止默認(rèn)提交
return false;
})
})
總結(jié):
重點(diǎn)就在怎么構(gòu)建字典后最后構(gòu)建好的字段如何傳參的問(wèn)題.
以上這篇解決Django中多條件查詢的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim())
這篇文章主要介紹了matplotlib之pyplot模塊坐標(biāo)軸范圍設(shè)置(autoscale(),xlim(),ylim()),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Python實(shí)現(xiàn)3行代碼解簡(jiǎn)單的一元一次方程
這篇文章主要介紹了Python實(shí)現(xiàn)3行代碼解簡(jiǎn)單的一元一次方程,很適合Python初學(xué)者學(xué)習(xí)借鑒,需要的朋友可以參考下2014-08-08
Python中利用json庫(kù)進(jìn)行JSON數(shù)據(jù)處理詳解
JSON是一種輕量級(jí)的數(shù)據(jù)交換格式,易于人閱讀和編寫,同時(shí)也易于機(jī)器解析和生成,下面就跟隨小編一起來(lái)了解下如何使用Python實(shí)現(xiàn)JSON數(shù)據(jù)處理吧2025-02-02
Python中用于轉(zhuǎn)換字母為小寫的lower()方法使用簡(jiǎn)介
這篇文章主要介紹了Python中用于轉(zhuǎn)換字母為小寫的lower()方法使用,是Python學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05

