From 45c888e13d2df97512d688f2e00f84dafc6e7e3c Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 4 Oct 2020 21:31:44 +1100 Subject: [PATCH] Custom cleaning for form Ok, looks like I've been doing this wrong the whole time! The "djangonic" way is pretty cool --- InvenTree/stock/forms.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index 28d0b47ef3..729831a9ec 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -9,6 +9,7 @@ from django import forms from django.forms.utils import ErrorDict from django.utils.translation import ugettext as _ from django.core.validators import MinValueValidator +from django.core.exceptions import ValidationError from mptt.fields import TreeNodeChoiceField @@ -293,13 +294,33 @@ class InstallStockForm(HelperForm): ] ) + notes = forms.CharField( + required=False, + help_text=_('Notes') + ) + class Meta: model = StockItem fields = [ 'stock_item', 'quantity_to_install', + 'notes', ] + def clean(self): + + data = super().clean() + + print("Data:", data) + + stock_item = data['stock_item'] + quantity = data['quantity_to_install'] + + if quantity > stock_item.quantity: + raise ValidationError({'quantity_to_install': _('Must not exceed available quantity')}) + + return data + class UninstallStockForm(forms.ModelForm): """