gpt4 book ai didi

python - Django根据日期计算数据查询

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:49 25 4
gpt4 key购买 nike

这是我的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”并在表格下显示总价。

最佳答案

也许你想要 sum合计over-a-queryset

在现有行后的 View 中添加 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

关于python - Django根据日期计算数据查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46625348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com