2017-10-09 77 views
0

這是我的models.py文件。Django在同一數據表中的多個字段中搜索查詢

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 

現在我想在muliple fieds搜索等作爲customer_name, customer_mobile_no,customer_product_id等,所以我創建views.py文件

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模型中的多個字段。另外,我正在嘗試其他人,但不工作。

回答

1

您需要在一個查詢中使用多個字段進行搜索。並看着你的代碼,我認爲條件加入使用OR

這個問題可以用django ORM's Q object

什麼它可以讓你做的是把多個過濾條件在一起,邏輯連接來解決問題。

所以,如果你有3個條件,它們邏輯連接爲:使用Q Condition 1 OR Condition 2 AND Condition 3你可以寫他們爲:

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

在你的情況的濾波的3個不同的搜索可以爲進行:

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

當我搜索的任何數字,一切都還好。但我發現錯誤,如果我用字符串搜索 –

+0

這是因爲'pk'是一個整數,你用於過濾的其餘領域是'CharField'。所以,你必須應用適當的類型轉換。添加了這個答案。 –

相關問題