Pass StockItem object through to the SerializeStock form

This commit is contained in:
Oliver Walters 2020-05-15 21:16:00 +10:00
parent 5b5b848a98
commit 72cfaccac5
2 changed files with 26 additions and 2 deletions

View File

@ -13,6 +13,8 @@ from mptt.fields import TreeNodeChoiceField
from InvenTree.helpers import GetExportFormats
from InvenTree.forms import HelperForm
from InvenTree.fields import RoundingDecimalFormField
from .models import StockLocation, StockItem, StockItemTracking, StockItemAttachment
@ -79,7 +81,7 @@ class CreateStockItemForm(HelperForm):
self._clean_form()
class SerializeStockForm(forms.ModelForm):
class SerializeStockForm(HelperForm):
""" Form for serializing a StockItem. """
destination = TreeNodeChoiceField(queryset=StockLocation.objects.all(), label='Destination', required=True, help_text='Destination for serialized stock (by default, will remain in current location)')
@ -88,6 +90,17 @@ class SerializeStockForm(forms.ModelForm):
note = forms.CharField(label='Notes', required=False, help_text='Add transaction note (optional)')
quantity = RoundingDecimalFormField(max_digits=10, decimal_places=5)
def __init__(self, *args, **kwargs):
# Extract the stock item
stock_item = kwargs.pop('item')
super().__init__(*args, **kwargs)
# TODO - Pre-fill the serial numbers!
class Meta:
model = StockItem

View File

@ -755,7 +755,18 @@ class StockItemSerialize(AjaxUpdateView):
model = StockItem
ajax_template_name = 'stock/item_serialize.html'
ajax_form_title = _('Serialize Stock')
form_class = SerializeStockForm
#form_class = SerializeStockForm
def get_form(self):
context = self.get_form_kwargs()
# Pass the StockItem object through to the form
context['item'] = self.get_object()
form = SerializeStockForm(**context)
return form
def get_initial(self):