how to implement dependent dropdown list based on value from foreign model in admin panel

145 Views Asked by At

I have classes like this:

class Building(models.Model):
    name = models.CharField(max_length=10)
    floors= models.IntegerField()

    def __str__(self):
        return self.name

class BuidingAccessRole(models.Model):
    role_name = models.CharField(max_length=10)  #name for the role of accessing a floor of one building
    building = models.ForeignKey(building,on_delete=models.CASCADE)
    floor = models.IntegerField()   Note:#i want this floor to be shown as choicefield with count starting from 1 to (building.floors) and get saved as integer value in database.
    def __str__(self):
       return self.name

  """If we set Building admin page as this object : 
       name='b1'
       floors=3
  then :
  BuidingAccessRole admin page to be shown as 
       name='Access for floor'
       building = b1 or 1 as 'id'
      floor shown as <select>1,2,3 as option </select>""" 

Note:#i want BuidingAccessRole.floor to be shown as choicefield in admin page with count starting from 1 to (building.floors) and get saved as integer value in database.
i tried acheiving this using chained foreign key but doesnt work out in this case .Is there any other way to do this!

0

There are 0 best solutions below