gpt4 book ai didi

Django:for循环中的总和值

转载 作者:行者123 更新时间:2023-12-03 06:04:09 25 4
gpt4 key购买 nike

我想计算表单集中订单价格的总和。如何求和 int(food.price) * int(f.order_count)

def reservation_f(request, year, month, day):
# order_count = Food.objects.filter(serve_date__startswith = datetime.date(int(year), int(month), int(day))).count()
ReservationFormset = modelformset_factory(
Reservation,
extra = 1,
fields = ('order_count', 'order_name', 'order_date'),
form = Reserve,
)
if request.method == "POST":
formset = ReservationFormset(request.POST, queryset=Food.objects.none(),
form_kwargs={'month': month, 'year': year, 'day':day})
if formset.is_valid():
formset = formset.save(commit=False)
for f in formset:
food = Food.objects.get(
serve_date__startswith = datetime.date(int(year), int(month), int(day)),
food_name=f.order_name)
print int(food.price) * int(f.order_count)
for f in formset:
f.user = request.user
f.save()

return HttpResponse("Add")
else:
formset = ReservationFormset(queryset=Food.objects.none(),
form_kwargs={'month': month, 'year': year, 'day':day})
return render(request, 'form.html', {'formset':formset})

最佳答案

你的变量的含义和类型不是很清楚,所以我可能是错的。但你可以做类似的事情:

for f in formset:
food = Food.objects.get(
se_date__startswith = datetime.date(int(year), int(month), int(day)),
food_name=f.order_name)
print sum([int(item.price) * int(f.order_count) for item in food])

关于Django:for循环中的总和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40961069/

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