我有这样的课程:
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>"""
注意:#我希望 BuidingAccessRole.floor 在管理页面中显示为选择字段,计数从 1 到 (building.floors) 并保存为数据库中的整数值。
我尝试使用链式外键来实现这一点,但在这种情况下不起作用。有没有其他方法可以做到这一点!