Improved view for creating a new part parameter

- Hide the Part input
- Reduce options based on parameters that already exist!
This commit is contained in:
Oliver Walters 2019-08-20 14:28:15 +10:00
parent c68c79ea43
commit 15a42878db
2 changed files with 42 additions and 1 deletions

View File

@ -19,7 +19,7 @@ part_attachment_urls = [
] ]
part_parameter_urls = [ part_parameter_urls = [
url('^new/?', views.PartParameterCreate.as_view(), name='part-param-create'), url('^new/', views.PartParameterCreate.as_view(), name='part-param-create'),
] ]
part_detail_urls = [ part_detail_urls = [

View File

@ -1404,6 +1404,47 @@ class PartParameterCreate(AjaxCreateView):
form_class = part_forms.EditPartParameterForm form_class = part_forms.EditPartParameterForm
ajax_form_title = 'Create Part Parameter' ajax_form_title = 'Create Part Parameter'
def get_initial(self):
initials = {}
part_id = self.request.GET.get('part', None)
if part_id:
try:
initials['part'] = Part.objects.get(pk=part_id)
except (Part.DoesNotExist, ValueError):
pass
return initials
def get_form(self):
""" Return the form object.
- Hide the 'Part' field (specified in URL)
- Limit the 'Template' options (to avoid duplicates)
"""
form = super().get_form()
part_id = self.request.GET.get('part', None)
if part_id:
try:
part = Part.objects.get(pk=part_id)
form.fields['part'].widget = HiddenInput()
query = form.fields['template'].queryset
query = query.exclude(id__in=[param.template.id for param in part.parameters.all()])
form.fields['template'].queryset = query
except (Part.DoesNotExist, ValueError):
pass
return form
class CategoryDetail(DetailView): class CategoryDetail(DetailView):