Limit available choicse in form

- Only allow selection of StockItem which matches the correct part
This commit is contained in:
Oliver Walters 2019-04-30 15:48:07 +10:00
parent 0208c6efe6
commit db5521f02e
2 changed files with 30 additions and 0 deletions

View File

@ -132,6 +132,11 @@ class BuildItem(models.Model):
quantity: Number of units allocated
"""
def get_absolute_url(self):
# TODO - Fix!
return '/build/item/{pk}/'.format(pk=self.id)
#return reverse('build-detail', kwargs={'pk': self.id})
class Meta:
unique_together = [
('build', 'stock_item'),

View File

@ -8,6 +8,7 @@ from __future__ import unicode_literals
from django.shortcuts import get_object_or_404
from django.views.generic import DetailView, ListView
from django.forms import HiddenInput
from part.models import Part
from .models import Build, BuildItem
@ -137,6 +138,30 @@ class BuildItemCreate(AjaxCreateView):
ajax_template_name = 'modal_form.html'
ajax_form_title = 'Allocate new Part'
def get_form(self):
""" Create Form for making / editing new Part object """
form = super(AjaxCreateView, self).get_form()
# If the Build object is specified, hide the input field.
# We do not want the users to be able to move a BuildItem to a different build
if form['build'].value() is not None:
form.fields['build'].widget = HiddenInput()
# If the sub_part is supplied, limit to matching stock items
part_id = self.get_param('part')
if part_id:
try:
part = Part.objects.get(pk=part_id)
query = form.fields['stock_item'].queryset
query = query.filter(part=part_id)
form.fields['stock_item'].queryset = query
except Part.DoesNotExist:
pass
return form
def get_initial(self):
""" Provide initial data for BomItem. Look for the folllowing in the GET data: