Solve the problem of multi condition query in Django
- 2021-07-22 10:05:20
- OfStack
tags: Conditional query in django
1 Some cms projects all use multi-condition queries. How do our back-end handle the requested conditions?
Satisfy one condition
Satisfy two conditions
Satisfy multiple conditions
..............
This process can be very annoying. In fact, there are many ways such as (pass parameters, pass dictionaries, pass Q objects, pass F objects …) into deep thinking … how to use simple methods to solve this requirement.
Personally, I think. All the conditions of our query to build a dictionary to query more efficient. How to operate specifically see the following code:
View function.
def order_list(request):
if request.method == 'GET':
return render(request, 'admin/order_list.html')
if request.method == 'POST':
# Get the case number
case_order = request.POST.get('case_order')
# Get customer name
case_name = request.POST.get('case_name')
# Obtain ID number
idno = request.POST.get('idno')
# Date of obtaining loan
loan_date = request.POST.get('loan_date')
# Get loan status
state = request.POST.get('state')
# Get loan type
dk_type = request.POST.get('dk_type')
# Fixed 1 Dictionaries are used to save the query conditions sent by the front end
search_dict = dict()
# If you have this value Write it into the dictionary
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:
# Query the required data through the association relationship
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
# Multi-condition query The key point is in front of the dictionary in this position 1 Be sure to add two asterisks .
user_order_info = UserTable.objects.filter(**search_dict)
# Serialization
data_info = [user_order.to_dict() for user_order in user_order_info]
data = {
'code': 200,
'data_info': data_info
}
return JsonResponse(data)
Models See the previous article
Portal Model
Front-end html page
<head>
// Use jquery It must be introduced
<script src="/static/admin/js/jquery.js" type="text/javascript"></script>
// Need to use ajaxSubmit To submit a form, you must introduce
<script src="/static/admin/js/jquery.form.min.js" type="text/javascript"></script>
// Use template.js The rendering page must be introduced
<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> Inquire about repayment cases </em></span>
</div>
<div class="operate">
<form id="search-order">
{% csrf_token %}
<div>
<div style="margin: 10px">
<label for=""> Customer order number :</label>
<input type="text" class="textBox length-long " name="case_order" value=""/>
<label for=""> Customer name :</label>
<input type="text" class="textBox length-long " name="case_name" value=""/>
</div>
<div style="margin: 10px">
<label for=""> ID number :</label>
<input type="text" class="textBox length-long " name="idno" value=""/>
<label for=""> Date of loan :</label>
<input type="text" class="textBox length-long" id="datepicker" name="loan_date" value=""/>
</div>
<div style="margin: 10px">
<label for=""> Processing status :</label>
<select class="inline-select textBox length-long" name="state">
<option value=" Unprocessed "> Unprocessed </option>
<option value=" Processed "> Processed </option>
</select>
<label for=""> Loan project :</label>
<select class="inline-select textBox length-long" name="dk_type">
<option value="POS Loan ">POS Loan </option>
<option value=" Cash loan "> Cash loan </option>
</select>
<div style="margin-right: 20px;margin-top: 10px;">
<input type="submit" value=" Query " class="tdBtn"/>
</div>
</div>
</div>
</form>
</div>
<table class="list-style Interlaced" id="test">
<tr>
<th> Application number </th>
<th> Customer name </th>
<th> Contact information </th>
<th> ID card number </th>
<th> Date of processing </th>
<th> Handler </th>
<th> Processing status </th>
<th> Processing time </th>
<th> Operation </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=" View the case "><img
src="/static/admin/images/icon_view.gif"/></a>
<a class="inline-block" title=" Delete a case ">
<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"> All selection </label>
<a href="/admin/export_excel/" rel="external nofollow" ><button id="export_excel" type="button" class="btnStyle" > Export excel</button></a>
<input type="button" value=" Delete a case " class="btnStyle"/>
</div>
After the back end is done, you can write ajax in the front end to render the page.
$(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> Application number </th>\n' +
' <th> Customer name </th>\n' +
' <th> Contact information </th>\n' +
' <th> ID card number </th>\n' +
' <th> Date of processing </th>\n' +
' <th> Handler </th>\n' +
' <th> Processing status </th>\n' +
' <th> Processing time </th>\n' +
' <th> Operation </th>\n' +
' </tr>'
var tr_html = template('tr_list', {users: data.data_info})
html += tr_html
$('#test').html(html)
}
}
})
// Block default commit
return false;
})
})
Summary:
The key point is how to pass the parameters of the finally constructed fields after how to construct the dictionary.