Limit choices for 'parent' field when editing PartCategory

This commit is contained in:
Oliver Walters 2019-05-08 17:57:31 +10:00
parent ab76525da2
commit fb38ddb1b3

View File

@ -404,12 +404,30 @@ class CategoryEdit(AjaxUpdateView):
context = super(CategoryEdit, self).get_context_data(**kwargs).copy()
try:
context['category'] = PartCategory.objects.get(pk=self.kwargs['pk'])
context['category'] = self.get_object()
except:
pass
return context
def get_form(self):
""" Customize form data for PartCategory editing.
Limit the choices for 'parent' field to those which make sense
"""
form = super(AjaxUpdateView, self).get_form()
category = self.get_object()
# Remove any invalid choices for the parent category part
parent_choices = PartCategory.objects.all()
parent_choices = parent_choices.exclude(id__in=category.getUniqueChildren())
form.fields['parent'].queryset = parent_choices
return form
class CategoryDelete(AjaxDeleteView):
""" Delete view to delete a PartCategory """