如何基于管理面板中外部模型的值实现依赖下拉列表

时间:2019-12-16 17:02:27

标签: django django-models django-admin django-smart-selects

我有这样的课程:

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>""" 

注意:#i希望将BuidingAccessRole.floor显示为管理页面中的choicefield,其计数从1开始到(building.floors),并保存为整数值在数据库中。
我试图使用链接的外键来实现这一点,但在这种情况下不起作用。还有其他方法可以做到这一点!

0 个答案:

没有答案