diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index 56ff7c5804..79bedd5616 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -260,13 +260,13 @@ class BuildItem(models.Model): if self.stock_item is not None and self.stock_item.part is not None: if self.stock_item.part not in self.build.part.required_parts(): - errors['stock_item'] = _("Selected stock item not found in BOM for part '{p}'".format(p=str(self.build.part))) + errors['stock_item'] = [_("Selected stock item not found in BOM for part '{p}'".format(p=self.build.part.name))] if self.stock_item is not None and self.quantity > self.stock_item.quantity: - errors['quantity'] = _("Allocated quantity ({n}) must not exceed available quantity ({q})".format( + errors['quantity'] = [_("Allocated quantity ({n}) must not exceed available quantity ({q})".format( n=self.quantity, q=self.stock_item.quantity - )) + ))] if len(errors) > 0: raise ValidationError(errors) diff --git a/InvenTree/build/views.py b/InvenTree/build/views.py index f429980c4e..f7de8a3a54 100644 --- a/InvenTree/build/views.py +++ b/InvenTree/build/views.py @@ -13,7 +13,7 @@ from django.forms import HiddenInput from part.models import Part from .models import Build, BuildItem from .forms import EditBuildForm, EditBuildItemForm, CompleteBuildForm -from stock.models import StockLocation +from stock.models import StockLocation, StockItem from InvenTree.views import AjaxView, AjaxUpdateView, AjaxCreateView, AjaxDeleteView @@ -328,3 +328,25 @@ class BuildItemEdit(AjaxUpdateView): return { 'info': 'Updated Build Item', } + + def get_form(self): + """ Create form for editing a BuildItem. + + - Limit the StockItem options to items that match the part + """ + + build_item = self.get_object() + + form = super(BuildItemEdit, self).get_form() + + query = StockItem.objects.all() + + if build_item.stock_item: + part_id = build_item.stock_item.part.id + query = query.filter(part=part_id) + + form.fields['stock_item'].queryset = query + + form.fields['build'].widget = HiddenInput() + + return form