溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

解決Django中多條件查詢的問題

發(fā)布時間:2020-08-19 18:14:04 來源:腳本之家 閱讀:142 作者:_古時候 欄目:開發(fā)技術(shù)

tags: django中對條件查詢

一些cms項目都會使用到多條件查詢,我們后端如何處理請求的條件呢?

滿足一個條件

滿足兩個條件

滿足多個條件

………………….

這樣處理起來會非常的惱火. 其實有多方法比如(傳參數(shù),傳字典,傳Q對象,傳F對象…)陷入深深的思考中…怎么用做簡單的方法把這個需求解決了.

個人覺得.把我們的查詢的所有條件來構(gòu)建一個字典來查詢起來比較高效.具體如何操作見下面的代碼:

視圖函數(shù).

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')
    # 獲取貸款狀態(tài)
    state = request.POST.get('state')
    # 獲取貸款類型
    dk_type = request.POST.get('dk_type')

    # 定一個字典用于保存前端發(fā)送過來的查詢條件
    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:
      # 通過關(guān)聯(lián)關(guān)系查詢出來需要的數(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)鍵點在這個位置傳如的字典前面一定要加上兩個星號.
    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 >
          <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 >
          <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="">處理狀態(tài):</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="現(xiàn)金貸">現(xiàn)金貸</option>
          </select>
          <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>聯(lián)系方式</th>
      <th>身份證號碼</th>
      <th>辦理日期</th>
      <th>處理人</th>
      <th>處理狀態(tài)</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" >
            <span>{{ user.loan_id }}</span>
          </a>
        </td>
        <td class="center">
          <span class="block">{{ user.name }}</span>
        </td>
        <td width="200" >
          <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 >
    <!-- 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去渲染頁面了.


$(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>聯(lián)系方式</th>\n' +
            '      <th>身份證號碼</th>\n' +
            '      <th>辦理日期</th>\n' +
            '      <th>處理人</th>\n' +
            '      <th>處理狀態(tài)</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)
        }
      }
    })
    // 阻止默認(rèn)提交
    return false;
  })
})

總結(jié):

重點就在怎么構(gòu)建字典后最后構(gòu)建好的字段如何傳參的問題.

以上這篇解決Django中多條件查詢的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI