From fd22e713ff8359749be91e091815ccc1ed95a80c Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 4 Oct 2020 20:50:06 +1100 Subject: [PATCH] Filter available stock items by Part reference --- InvenTree/stock/forms.py | 6 ++++-- InvenTree/stock/views.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/InvenTree/stock/forms.py b/InvenTree/stock/forms.py index ebcf31cb23..28d0b47ef3 100644 --- a/InvenTree/stock/forms.py +++ b/InvenTree/stock/forms.py @@ -283,8 +283,10 @@ class InstallStockForm(HelperForm): help_text=_('Stock item to install') ) - quantity = RoundingDecimalFormField( + quantity_to_install = RoundingDecimalFormField( max_digits=10, decimal_places=5, + initial=1, + label=_('Quantity'), help_text=_('Stock quantity to assign'), validators=[ MinValueValidator(0.001) @@ -295,7 +297,7 @@ class InstallStockForm(HelperForm): model = StockItem fields = [ 'stock_item', - 'quantity', + 'quantity_to_install', ] diff --git a/InvenTree/stock/views.py b/InvenTree/stock/views.py index d818e82aa9..1f6ddcc0f4 100644 --- a/InvenTree/stock/views.py +++ b/InvenTree/stock/views.py @@ -703,6 +703,22 @@ class StockItemInstall(AjaxUpdateView): form = super().get_form() + queryset = form.fields['stock_item'].queryset + + part = self.request.GET.get('part', None) + + # Filter the available stock items based on the Part reference + if part: + try: + part = Part.objects.get(pk=part) + + queryset = queryset.filter(part=part) + + except (ValueError, Part.DoesNotExist): + pass + + form.fields['stock_item'].queryset = queryset + return form def post(self, request, *args, **kwargs):