我想在formset中对订单价格求和。
如何总结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})
答案 0 :(得分:0)
变量的含义和类型不是很清楚,所以我可能错了。 但你可以这样做:
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])