相同数据表中多个字段内的Django Search查询 [英] Django Search query within multiple fields in same data table

查看:48
本文介绍了相同数据表中多个字段内的Django Search查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的models.py文件.

This is my models.py file.

class CustomerInfo(models.Model):
    customer_name=models.CharField('Customer Name', max_length=50)
    customer_mobile_no = models.CharField('Mobile No', null=True, blank=True,max_length=12)
    customer_price=models.IntegerField('Customer Price')
    customer_product_warrenty = models.CharField('Product Warrenty',null=True, blank=True,max_length=10)
    customer_sell_date = models.DateTimeField('date-published', auto_now=True)
    customer_product_id=models.CharField('Product ID',max_length=150,null=True, blank=True)
    customer_product_name=models.CharField('Product Name', max_length=50)
    customer_product_quantity=models.IntegerField('Quantity',default=1)


    def __str__(self):
        return self.customer_name

现在,我想搜索诸如 customer_name,customer_mobile_no,customer_product_id 等这样的多行文件,因此我创建了views.py文件

Now I want to search in muliple fieds like as customer_name, customer_mobile_no,customer_product_id etc. So I created views.py file

def customerPage(request):
    customers = CustomerInfo.objects.all()

    if request.method =="GET":
       customerid = request.GET['customer_id']

       try:
           customers = CustomerInfo.objects.get(pk=customerid)
           cus_name = CustomerInfo.objects.filter(customer_name__contains=customerid)
           mobile_number = CustomerInfo.objects.filter(customer_mobile_no__contains=customerid)



           return render(request, 'shop/customer.html', {"cus_name": cus_name,"mobile_number": mobile_number, "customers": 'customers', "site_name": "Moon Telecom"})
       except:
           return render(request, 'shop/customer.html', {"error": "Not found any info"})

    return render(request, 'shop/customer.html', {'customers': customers})

这是我的html文件

{% extends "shop/base.html" %}

{% block content_area %}

<div class="col-lg-4">
    <div class="customer_search" >
        <form action="{% url "shop:customerPage" %}" method="GET">
            {% csrf_token %}
            <div class="form-group">
                <label for="customer_id">Id:</label>
                <input type="text" class="form-control" id="customer_id" placeholder="Enter customer ID" name="customer_id">
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
        </form>
    </div>
</div>

<div class="col-lg-8 customers_info">
    {% if error %}
    <div class="alert alert-danger">
        <strong>{{error}}</strong>
    </div>
    {% endif %}



{% if cus_name %}

    {% for x in cus_name  %}
    <p>{{x.customer_name}}</p>
    {% endfor %}
{% else %}
<p>nothing foung</p>
{% endif %}


{% if customers %}
    <table class="table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Mobile No</th>
                <th>Product Name</th>
                <th>Price</th>
                <th>Date</th>
                <th>Product ID</th>
                <th>Warrenty</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><a href="/shop/{{customers.id}}/customerprofile">{{customers.customer_name}}</a></td>
                <td>{{customers.customer_mobile_no}}</td>
                <td>{{customers.customer_product_name}}</td>
                <td>{{customers.customer_price}} TK</td>
                <td>{{customers.customer_sell_date}}</td>
                <td>{{customers.customer_product_id}}</td>
                <td>{% if customers.customer_product_warrenty == '' %}
                <b>No Warrenty</b>
                {% else %}
                 <b>{{customers.customer_product_warrenty}}</b> Month
                {% endif %}
                </td>

            </tr>
        </tbody>
    </table>
     {% else %}
    <p>nothing found</p>
    {% endif %}


</div>



{% endblock  %}

我有结果如果我使用POST方法并且 customers = CustomerInfo.objects.get(pk = customerid)当我搜索一个字段时,我有我的结果,但是当我从中开始多个搜索查询时数据库.我无法获取任何信息.我想在 CustomerInfo 模型中搜索多个字段.另外,我正在尝试其他方法,但是没有用.

I got results If I use POST method and customers = CustomerInfo.objects.get(pk=customerid) When I searched one field, I have got my results but When I start multiple search query from the database. I cant get any info. I want to search multiple fields within CustomerInfo model. Also, I was trying others mehtod but not working.

推荐答案

您需要在一个查询中使用多个字段进行搜索.并查看您的代码,我假设条件是使用 OR 进行连接的.

You need to search using multiple fields in one query. And looking at your code, i presume that the conditions are joined using OR.

可以使用

This problem can be solved using django ORM's Q object

它允许您做的是将多个过滤条件链接在一起并进行逻辑连接.

What it allows you do is to chain multiple filtering conditions together and connect them logically.

因此,如果您有3个条件,并且它们在逻辑上联系为:条件1或条件2和条件3 使用 Q ,您可以将它们写为:

So, if you have 3 conditions and they are logically connected as : Condition 1 OR Condition 2 AND Condition 3 using Q you can write them as :

Q(Condition1) | Q(Conditon2) & Q(Condition2).

在您的情况下,可以按以下三种方式进行过滤搜索:

In your case the 3 different searches of filterings can be performed as:

filtered_customers = CustomerInfo.objects.filter( Q(pk = int(customerid)) | Q(customer_name__contains = str(customerid)) | Q(customer_mobile_no__contains = str(customerid)))

这篇关于相同数据表中多个字段内的Django Search查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆