bornhack-website/src/shop/forms.py

27 lines
726 B
Python
Raw Normal View History

from django import forms
from django.forms import modelformset_factory
from shop.models import OrderProductRelation
2016-05-12 16:16:25 +00:00
class OrderProductRelationForm(forms.ModelForm):
class Meta:
model = OrderProductRelation
2019-03-29 21:19:49 +00:00
fields = ["quantity"]
def clean_quantity(self):
product = self.instance.product
2019-03-29 21:19:49 +00:00
new_quantity = self.cleaned_data["quantity"]
if product.stock_amount and product.left_in_stock < new_quantity:
raise forms.ValidationError(
2019-03-29 21:19:49 +00:00
"Only {} left in stock.".format(product.left_in_stock)
)
return new_quantity
OrderProductRelationFormSet = modelformset_factory(
2019-03-29 21:19:49 +00:00
OrderProductRelation, form=OrderProductRelationForm, extra=0
)