Add hidden input to the InstallStockForm form

- keeps track of "part" object
- so we can filter the stock_items queryset if the form validation fails
- Is there a more djangonic way of doing this??
This commit is contained in:
Oliver Walters 2020-10-04 23:45:52 +11:00
parent 46f459b4c7
commit 42a75a8238
2 changed files with 24 additions and 6 deletions

View File

@ -19,6 +19,8 @@ from InvenTree.fields import RoundingDecimalFormField
from report.models import TestReport
from part.models import Part
from .models import StockLocation, StockItem, StockItemTracking
from .models import StockItemAttachment
from .models import StockItemTestResult
@ -278,6 +280,11 @@ class InstallStockForm(HelperForm):
Form for manually installing a stock item into another stock item
"""
part = forms.ModelChoiceField(
queryset=Part.objects.all(),
widget=forms.HiddenInput
)
stock_item = forms.ModelChoiceField(
required=True,
queryset=StockItem.objects.filter(StockItem.IN_STOCK_FILTER),
@ -302,6 +309,7 @@ class InstallStockForm(HelperForm):
class Meta:
model = StockItem
fields = [
'part',
'stock_item',
'quantity_to_install',
'notes',

View File

@ -699,6 +699,8 @@ class StockItemInstall(AjaxUpdateView):
form_class = StockForms.InstallStockForm
ajax_form_title = _('Install Stock Item')
part = None
def get_stock_items(self):
"""
Return a list of stock items suitable for displaying to the user.
@ -713,14 +715,19 @@ class StockItemInstall(AjaxUpdateView):
items = StockItem.objects.filter(StockItem.IN_STOCK_FILTER)
# Filter by Part association
try:
part = self.request.GET.get('part', None)
if part is not None:
part = Part.objects.get(pk=part)
items = items.filter(part=part)
# Look at GET params
part_id = self.request.GET.get('part', None)
if part_id is None:
# Look at POST params
part_id = self.request.POST.get('part', None)
try:
self.part = Part.objects.get(pk=part_id)
items = items.filter(part=self.part)
except (ValueError, Part.DoesNotExist):
pass
self.part = None
return items
@ -735,6 +742,9 @@ class StockItemInstall(AjaxUpdateView):
item = items.first()
initials['stock_item'] = item.pk
initials['quantity_to_install'] = item.quantity
if self.part:
initials['part'] = self.part
return initials