2017-10-07 5 views
1

Ceci est mon fichier models.py.Calculer la requête Django Data selon la date

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 

Lorsque je trouve des informations de requête de données par date, je veux calculer "customer_price" uniquement pour la date sélectionnée. Ensuite, je vais montrer à ma page HTML. Ceci est ma requête de recherche par date.

c'est mon fichier 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) 

Ceci est la page 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 %} 

Maintenant, Calculer dans mon date sélectionnée tout "" customer_price "" et montrant prix total sous la table.

+1

utilisation globale et la fonction Somme: Model.objects.aggregate (Somme ('field_name')) –

+0

Comment Je montre ma page HTML? –

Répondre

1

peut-être que vous voulez total over-a-queryset

en vue existe, après la ligne ajouter 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

Comment afficher ma page HTML? –

+0

Mise à jour et ajout de détails à l'entrée –

+0

J'ai affiché toutes les listes de prix client dans le tableau HTML. Maintenant, je veux calculer le total customer_price et je ferai des chaussures sous le tableau HTML. –