mercredi 29 mars 2017

Filter objects from another in django admin

In the django admin when the user adds an animal, I need to only see the "troops" that belong to the "farm" when selecting the "farm" field in the "troop" field. That is, respect the foreign key.

class Troop(models.Model):
    name = models.CharField(max_length=255)
    farm = models.ForeignKey(Farm)

class Farm(models.Model):
    name = models.CharField(max_length=255)

class Animals(models.Model):
    rp = models.CharField(max_length=50)
    name = models.CharField(max_length=50)
    farm = models.ForeignKey(Farm)
    troop = models.ForeignKey(Troop)

Thanks




Aucun commentaire:

Enregistrer un commentaire