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

作者:_古时候 时间:2021-10-10 07:32:16 

tags: django中对条件查询

一些cms项目都会使用到多条件查询,我们后端如何处理请求的条件呢?

满足一个条件

满足两个条件

满足多个条件

………………….

这样处理起来会非常的恼火. 其实有多方法比如(传参数,传字典,传Q对象,传F对象…)陷入深深的思考中…怎么用做简单的方法把这个需求解决了.

个人觉得.把我们的查询的所有条件来构建一个字典来查询起来比较高效.具体如何操作见下面的代码:

视图函数.


def order_list(request):

if request.method == 'GET':
   return render(request, 'admin/order_list.html')

if request.method == 'POST':
   # 获取案件号
   case_order = request.POST.get('case_order')
   # 获取客户姓名
   case_name = request.POST.get('case_name')
   # 获取身份证号码
   idno = request.POST.get('idno')
   # 获取贷款日期
   loan_date = request.POST.get('loan_date')
   # 获取贷款状态
   state = request.POST.get('state')
   # 获取贷款类型
   dk_type = request.POST.get('dk_type')

# 定一个字典用于保存前端发送过来的查询条件
   search_dict = dict()
   # 如果有这个值 就写入到字典中去
   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:
     # 通过关联关系查询出来需要的数据
     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

# 多条件查询 关键点在这个位置传如的字典前面一定要加上两个星号.
   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见上一篇文章

传送门 Model

前端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="search-order">
     {% csrf_token %}
     <div>
       <div style="margin: 10px">
         <label for="">客户单号:</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="">身份证号:</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="">处理状态:</label>
         <select class="inline-select textBox length-long" name="state">
           <option value="未处理">未处理</option>
           <option value="已处理">已处理</option>
         </select>
         <label for="">贷款项目:</label>
         <select class="inline-select textBox length-long" name="dk_type">
           <option value="POS贷">POS贷</option>
           <option value="现金贷">现金贷</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>申请编号</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 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" >导出excel</button></a>
     <input type="button" value="删除案件" class="btnStyle"/>
   </div>

后端搞定了就可以在前端写ajax去渲染页面了.



$(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>申请编号</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', {users: data.data_info})
         html += tr_html
         $('#test').html(html)
       }
     }
   })
   // 阻止默认提交
   return false;
 })
})

总结:

重点就在怎么构建字典后最后构建好的字段如何传参的问题.

来源:https://blog.csdn.net/qq_38059635/article/details/83903109

标签:Django,多条件,查询
0
投稿

猜你喜欢

  • 下拉框二级联动的JavaScript代码

    2009-05-18 18:39:00
  • 详解supervisor使用教程

    2022-02-18 09:12:07
  • php使用递归与迭代实现快速排序示例

    2023-11-14 09:46:31
  • Python实战使用Selenium爬取网页数据

    2021-06-18 19:11:56
  • python的flask框架难学吗

    2023-08-18 15:34:32
  • Python配置pip国内镜像源的实现

    2021-09-17 03:21:24
  • Python利用公共键如何对字典列表进行排序详解

    2022-03-04 01:31:33
  • Python传递参数的多种方式(小结)

    2023-05-10 23:05:46
  • python+opencv+selenium自动化登录邮箱并解决滑动验证的问题

    2021-10-01 07:32:18
  • JS删除数组里的某个元素方法

    2023-07-14 22:45:47
  • Python中glob库实现文件名的匹配

    2022-08-06 21:50:19
  • Flask框架单例模式实现方法详解

    2023-01-24 17:04:55
  • 解密CSS Sprites:技巧、工具和教程

    2011-01-11 19:38:00
  • Firefox 3.6新功能预览

    2009-12-01 14:23:00
  • jQuery 1.3.3 新功能[译]

    2009-06-04 12:23:00
  • Python实现孤立随机森林算法的示例代码

    2021-11-25 14:38:24
  • 20分钟成功编写bootstrap响应式页面 就这么简单

    2023-08-12 06:12:13
  • python运行或调用另一个py文件或参数方式

    2023-10-26 02:04:47
  • python实现将多个文件分配到多个文件夹的方法

    2023-04-13 20:48:46
  • 轻松实现php文件上传功能

    2023-11-17 04:34:12
  • asp之家 网络编程 m.aspxhome.com