我正在尝试在管理面板中为不同的模型创建一个状态字段,该字段应该从我的主模型中继承该属性:
class Students(models.Model):
Username = models.ForeignKey(User)
FirstName = models.CharField(max_length=50)
LastName = models.CharField(max_length=50)
Group = models.CharField(max_length=4)
status = models.CharField(max_length=1,null=True, choices=STATUS_CHOICES, default='n')
def __str__(self):
return str(self.Username) + ' ' + self.FirstName +' ' + self.LastName
在CatalogStudentsFirstYear中我想仅存储状态来自学生的状态值
class CatalogStudentsFirstYear(models.Model):
catalogFirstYear = models.ForeignKey(CatalogFirstYear)
student = models.ForeignKey(Students)
grade = models.IntegerField(blank=True, null=True)
status =
def __str__(self):
return str(self.catalogFirstYear) +' ' + str(self.student)
并将其添加到admin.py,如下所示:
list_display = ["student", "catalogFirstYear", "grade", "status"]
这就是目前看起来如何看待CatalogStudentsFirstYear
您能否建议我如何创建一个只接受学生提交状态的FK,并将其存储在CatalogStudentsFirstYear 状态中?
谢谢
答案 0 :(得分:2)
您可以添加status
作为模型属性:
class CatalogStudentsFirstYear(models.Model):
catalogFirstYear = models.ForeignKey(CatalogFirstYear)
student = models.ForeignKey(Students)
grade = models.IntegerField(blank=True, null=True)
def __str__(self):
return str(self.catalogFirstYear) +' ' + str(self.student)
@property
def status(self):
return self.student.status
并将此属性添加到管理员list_display
:
list_display = ["student", "catalogFirstYear", "grade", "status"]