解决Django中多条件查询的问题

更新时间:2023-07-20 12:26:11 阅读: 评论:0

解决Django中多条件查询的问题
tags: django中对条件查询
⼀些cms项⽬都会使⽤到多条件查询,我们后端如何处理请求的条件呢?
满⾜⼀个条件
满⾜两个条件
满⾜多个条件
………………….
这样处理起来会⾮常的恼⽕. 其实有多⽅法⽐如(传参数,传字典,传Q对象,传F对象…)陷⼊深深的思考中…怎么⽤做简单的⽅法把这个需求解决了.
个⼈觉得.把我们的查询的所有条件来构建⼀个字典来查询起来⽐较⾼效.具体如何操作见下⾯的代码:
视图函数.
def order_list(request):
hod == 'GET':
return render(request, 'admin/order_list.html')
hod == 'POST':
# 获取案件号
ca_order = ('ca_order')
# 获取客户姓名
ca_name = ('ca_name')
# 获取⾝份证号码
idno = ('idno')
# 获取贷款⽇期
loan_date = ('loan_date')
# 获取贷款状态
state = ('state')
# 获取贷款类型
dk_type = ('dk_type')
# 定⼀个字典⽤于保存前端发送过来的查询条件
arch_dict = dict()
# 如果有这个值就写⼊到字典中去
if ca_order:
arch_dict['loan_id'] = ca_order
if ca_name:
arch_dict['name'] = ca_name
if idno:
arch_dict['ur_card'] = idno
if loan_date:
arch_dict['pri_date'] = loan_date
if state:
# 通过关联关系查询出来需要的数据
state_info = StatuTable.objects.filter(statu_name=state).first()
arch_dict['statu_id'] = state_info.statu_id
if dk_type:
loa = LoantypeTable.objects.filter(loan_name=dk_type).first()
arch_dict['loa_loan_id'] = loa.loan_id
# 多条件查询关键点在这个位置传如的字典前⾯⼀定要加上两个星号.
ur_order_info = UrTable.objects.filter(**arch_dict)
# 序列化
data_info = [_dict() for ur_order in ur_order_info]
data = {
'code': 200,
'data_info': data_info
}
return JsonRespon(data)
Models见上⼀篇⽂章
前端html页⾯
<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渲染页⾯就必须引⼊
<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="arch-order">
{% csrf_token %}
<div>
<div >
<label for="">客户单号:</label>
<input type="text" class="textBox length-long " name="ca_order" value=""/>
<label for="">客户名称:</label>
<input type="text" class="textBox length-long " name="ca_name" value=""/>
</div>
<div >
<label for="">⾝份证号:</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 >
<label for="">处理状态:</label>
<lect class="inline-lect textBox length-long" name="state">
<option value="未处理">未处理</option>
<option value="已处理">已处理</option>
</lect>
<label for="">贷款项⽬:</label>
<lect class="inline-lect textBox length-long" name="dk_type">
<option value="POS贷">POS贷</option>
<option value="现⾦贷">现⾦贷</option>
</lect>
<div >
秋季喝什么汤好<input type="submit" value="查询" class="tdBtn"/>
</div>
</div>
</div>
</form>
</div>
<table class="list-style Interlaced" id="test">
<tr>
<th>申请编号</th>
<th>客户名称</th>
<th>联系⽅式</th>
<th>⾝份证号码</th>
<th>办理⽇期</th>
<th>处理⼈</th>
<th>处理状态</th>
<th>处理时间</th>
<th>操作</th>
</tr>
{% verbatim %}
<script type="text/html" id="tr_list">
{{ each urs as ur }}
描写时间快的句子
<tr>
<td>
<input type="checkbox"/>
<a href="/admin/order_detail/?id={{ ur.ur_id }}" rel="external nofollow" rel="external nofollow" >            <span>{{ ur.loan_id }}</span>
</a>
<td class="center">
<span class="block">{{ ur.name }}</span>
</td>
<td width="200" >
<span class="block">{{ ur.phone }}</span>
</td>
<td class="center">
<span>{{ ur.card }}</span>
</td>
<td class="center">
<span>{{ ur.date }}</span>
氢氧化钠变质的化学方程式</td>
<td class="center">
读书的事例<span>{{ ur.deal_peo }}</span>
</td>
<td class="center">
<span>{{ ur.status }}</span>
</td>
<td class="center">
<span>{{ ur.deal_time }}</span>
</td>
兴趣的英文
<td class="center">
<a href="/admin/order_detail/?id={{ ur.ur_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 >
<!-- 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" >导出excel</button></a>      <input type="button" value="删除案件" class="btnStyle"/>
</div>
后端搞定了就可以在前端写ajax去渲染页⾯了.
$(function () {
嘴巴小
var token = $(':input[name="csrfmiddlewaretoken"]').val()
$('#arch-order').submit(function () {
$(this).ajaxSubmit({
url: '/admin/order_list/',
dataType: 'json',
type: 'POST',
headers: {'X-CSRFToken': token},
success: function (data) {
if (de == 200) {
var html ='<tr>\n' +
'      <th>申请编号</th>\n' +
'      <th>客户名称</th>\n' +
'      <th>联系⽅式</th>\n' +
'      <th>⾝份证号码</th>\n' +
'      <th>办理⽇期</th>\n' +
'      <th>处理⼈</th>\n' +
'      <th>处理状态</th>\n' +
'      <th>处理时间</th>\n' +
'      <th>操作</th>\n' +
'    </tr>'
var tr_html = template('tr_list', {urs: data.data_info})
html += tr_html
$('#test').html(html)
}
}
})
// 阻⽌默认提交
})
})
总结:
重点就在怎么构建字典后最后构建好的字段如何传参的问题.
以上这篇解决Django中多条件查询的问题就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

本文发布于:2023-07-20 12:26:11,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1089048.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:查询   条件   获取   字典   问题
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图