2

I have two models

class Animal(models.Model):
    TYPE = (
        ('cat', 'Cat'),
        ('dog', 'Dog'),
        ('chicken', 'Chicken'),
    )

    type = models.CharField(max_length=10, choices=TYPE, default='cat')

class Injury(models.Model):
    BODY_PART = (
        ('leg', 'Leg'),
        ('eye', 'Eye'),
        ('beak', 'Beak'),
    )

    animal = models.ForeignKey(Animal, on_delete=models.CASCADE)
    injured_body_part = models.CharField(max_length=10, choices=BODY_PART, default='Leg')
    how_it_happened = models.CharField(max_length=100)

So in my contrived example an animal can have many injuries and an injury can belong to one animal.

When I create a new Injury in Django Admin I want it to restrict the choices for injured_body_part depending on which Animal I have selected.

For example, if I select dog the injured_body_part option for beak wouldn't appear.

I have found similar questions on SO but they are either very old or have unsuitable answers. Answers like this are amazing, but I need it specifically for Django Admin.

Thank you.

horse
  • 479
  • 7
  • 25
  • 1
    You can inject js to do this, when first element is selected as `dog`, second `select input` pop out the `beak` – minglyu Jul 06 '20 at 07:46

0 Answers0