根据日期计算Django数据查询

Calculate Django Data query according to date

这是我的 models.py 文件。

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"”,并显示 table 下的总价。

也许你想要sum aggregate 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