我希望表单集每次迭代都自动更改,但事实并非如此,它只考虑了最后一个增加的值。
views.py:
class VenteFormView(View):
VenteFormSet = formset_factory(VenteForm)
template_name = "venteH.html"
def get(self, request):
return render(request, self.template_name, {'form': self.VenteFormSet()})
def post(self, request, totalvente=0, totalarticle=0):
vente_formset = self.VenteFormSet(self.request.POST,error_class=ParagraphErrorList)
#try:
if vente_formset.is_valid():
for temp in vente_formset:
vente =temp.save(commit=True)
article = vente.id_article
print('article au depart :',article)
print('quantité article au depart :', article.quantite)
entre = article.quantite
entre -= vente.quantite_v
if entre >= 0:
article = vente.id_article
article.quantite -= vente.quantite_v
print('article final :', article.quantite)
totalvente +=article.prix_de_vente * vente.quantite_v
print('Total à payer :',totalvente)
article.save()
print('article sauve',article.quantite)
else:
vente.delete()
messages.warning(request, 'Stock insuffisant pour cette opération! ')
return render(request, self.template_name, {'form': self.VenteFormSet(),'totalvente':totalvente})
else:
messages.warning(request,'Formulaire invalide!!!')
return render(request, self.template_name, {'form': self.VenteFormSet(),'totalvente':totalvente})
#except:
# vente.delete()
# messages.warning(request, 'Recommencer une nouvelle fois, présence !!!')
#return render(request, self.template_name, {'form': self.VenteFormSet(),'totalvente': totalvente})