I would like to create a custom horizontal filter inside Django admin. My use case looks like this. I am working on a Testmanagment tool. You got "Projects" like websiteA or WebsiteB. Every Project has its own Elements/Groups. E.g WebsiteA got "Dashboard" and "Login" and WebsiteB has "Users" and "Orders".
If I am creating a new Testcase I would like to choose the Project first and offer a filtered list to all corresponding Elements for that project.
I don't want to get all Elements/Groups for a new Testcase.
My code looks something like this:
Forms.py:
class TestCaseForm(forms.ModelForm):
@staticmethod
def groupSmartFilter():
return GroupModel.objects.filter(id=2) #working fine so far
but should be something like
return Projects.objects.filter(projectname=<$project_from_dropdown>).groups()
def __init__(self, *args, **kwargs):
forms.ModelForm.__init__(self, *args, **kwargs)
self.fields['groups'].queryset = TestCaseForm.groupSmartFilter()
class Meta:
model = TestCaseModel
fields = ('name', 'TestcaseNummer', 'project','groups', 'description' , 'url' )
admin.py
class TestCaseAdmin(CompareVersionAdmin):
form = TestCaseForm
filter_horizontal = ('groups',)