This is a common scenario in frontend where we want a dropdown options set with respect to another selection in another dropdown, but not getting a solution in django admin forms Scenario : django model:
class CompanyEmployee():
"""
An Emailed Report
"""
company = models.ForeignKey(Company,
on_delete=models.CASCADE,
)
employee = models.ManyToManyField(Employee,
blank=True,
verbose_name='Employes',)
class Meta:
unique_together = (
('company', 'name'),
)
so in CompanyEmailAdminForm company is in list_filter and Employee as filter_horizontal, that means company is a dropdown and employee as filter with multiple choice.
The queryset for employee widget
if instance.pk:
self.fields['employee'].queryset =Employee.objects.filter(company=instance.company)
else:
self.fields['employee'].queryset =Employee.objects.all()
Company and Employee have a relation. So from company I can get the related Employee records.
The issue is in add form where I don't have a saved instance.
My requirement is when I select a company say 'ABC' I should get only records related to 'ABC' in the Employee filter.
If onChange i can get the value back in the form I can re-evaluate the employee queryset. With django.JQuery the values in the employee section is not remaining permanently.