I am struggling with implementing a dependent drop-down list within my Django admin page. I have a project foreign key in my Phase model and I would like the user to be able to select a project from the first drop-down, and then have the phases of that project displayed in the second drop-down.
What is the best approach to achieve this functionality? It would be beneficial if the items in the drop-downs could filter based on the value selected in the parent drop-down.
https://i.sstatic.net/9dc0K.png
class Project(models.Model):
name = models.CharField(max_length=100, unique=True)
short_name = models.CharField(max_length=4, unique=True)
slug = models.SlugField(max_length=100, allow_unicode=True, null=True, editable=False)
location = models.OneToOneField(Location, on_delete=models.SET_NULL, null=True, blank=False, verbose_name='موقعیت')
start_date = models.DateField(default=timezone.now, null=True, blank=True)
end_date = models.DateField(default=timezone.now, null=True, blank=True)
duration = models.IntegerField(default=0, editable=False)
class Phase(models.Model):
title = models.CharField(max_length=20)
class ProjectPhase(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='phase')
phase = models.ForeignKey(Phase, on_delete=models.CASCADE, related_name='project')
start_date = models.DateField(default=timezone.now)
end_date = models.DateField(default=timezone.now)
duration = models.IntegerField(default=0, editable=True)