2017-10-07 122 views
1

這是我的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') 
    customer_product_id=models.CharField('Product ID',max_length=150,null=True, blank=True) 
    customer_product_name=models.CharField('Product Name', max_length=50) 


    def __str__(self): 
     return self.customer_name 

當我通過日期找到數據查詢信息時,我想通過僅選定日期來計算「customer_price」。然後我會顯示到我的HTML頁面。 這是我按日期進行的搜索查詢。

這是我的views.py文件

def dailyReport(request): 

    if request.method =='POST': 
     datelist=request.POST['datewisesearch'] 

     try: 
      customers = CustomerInfo.objects.filter(
       customer_sell_date__day=datelist) 

      months=timezone.now().month 
      years = timezone.now().year 

      return render(request, 'shop/report.html', {"customers": customers, "datelist": datelist, "months": months, "years": years}) 


     except: 
      pass 

     return render(request,'shop/report.html', context=None) 

這是html頁面

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

    {% block content_area %} 

    <div class="col-lg-12"> 
     <div class="daily_title"> 
      <h2>Daily Report</h2> 
     </div> 
     <form action="{% url 'shop:dailyReport' %}" class="" method="POST"> 
      {% csrf_token %} 
      <div class="form-group"> 
       <label for="datewisesearch">Search Product:</label> 
       <input type="text" class="form-control" id="datewisesearch" name="datewisesearch"> 
      </div> 

      <button type="submit" class="btn btn-default">Submit</button> 
     </form> 
    </div> 

    <div class="col-lg-12"> 
     <br> 
     <hr> 
     <button type="button" class="btn btn-success">See Today Date is: {{datelist}}-{{months}}-{{years}} : Reports</button> 
     <br> 
     <hr> 


    <div class="table table-bordered"> 
     <table class="table"> 
     <thead> 
      <tr> 
       <th>No</th> 
       <th>Invoice ID</th> 
       <th>Name</th> 
       <th>Mobile</th> 
       <th>Product Name</th> 
       <th>Product ID</th> 
       <th>Warrenty</th> 
       <th>Customer Price</th> 

      </tr> 
     </thead> 
     {% for x in customers %} 
      <tbody> 
       <tr> 
        <td>{{ forloop.counter }}</td> 
        <td>{{x.id}}</td> 
        <td><a href="{% url "shop:customersProfile" x.id %}">{{x.customer_name}}</a></td> 
        <td>{{x.customer_mobile_no}}</td> 
        <td>{{x.customer_product_name}}</td> 
        <td>{{x.customer_product_id}}</td> 
        <td>{{x.customer_product_warrenty}} Month</td> 
        <td>{{x.customer_price}} TK</td> 
       </tr> 

      </tbody> 

     {% endfor %} 
     <h2>Total Cost is:{{sum_price}} </h2> 
     </table> 
    </div> 
    </div> 




{% endblock content_area %} 

現在,我計算選定日期內的所有 「」 customer_price 「」,並顯示總價格在桌子下面。

+1

使用聚合和求和功能:Model.objects.aggregate(總和( 'FIELD_NAME')) –

+0

如何能我顯示我的HTML頁面? –

回答

1

可能是你想要總over-a-queryset

的意見

後存在行添加sum_price

from django.db.models import Sum 

customers = CustomerInfo.objects.filter(customer_sell_date__day=datelist) 
# Add next line 
sum_price = customers.aggregate(sp=Sum('customer_price')).get('sp', 0) 
# Don't foget to add sum_price to your context 
+0

如何顯示我的HTML頁面? –

+0

更新,並且詳情添加到短信搜索 –

+0

我在HTML表格中顯示了所有customer_price列表。現在我想計算總客戶價格,我將在HTML表格下穿鞋。 –