1 回答
TA贡献1943条经验 获得超7个赞
limit_choices_to=…
是的,您可以使用参数 [Django-doc]过滤此内容:
class Course(models.Model):
course = models.CharField(max_length=150)
start_date = models.DateField()
end_date = models.DateField()
instructor = models.ForeignKey(
CustomUser,
on_delete=models.CASCADE,
related_name='instructor_name',
limit_choices_to={'role': 'staff'}
)
examinar = models.ForeignKey(
CustomUser,
on_delete=models.CASCADE,
related_name='examinar_name',
limit_choices_to={'role': 'student'}
)
然而,参数related_name=…
[Django-doc]是反向关系的名称。因此,这是一种访问Course
具有instructor
/examinar
用户身份的所有对象的方法。因此,您可能希望将这些字段重命名为:
class Course(models.Model):
course = models.CharField(max_length=150)
start_date = models.DateField()
end_date = models.DateField()
instructor = models.ForeignKey(
CustomUser,
on_delete=models.CASCADE,
related_name='taught_courses',
limit_choices_to={'role': 'staff'}
)
examinar = models.ForeignKey(
CustomUser,
on_delete=models.CASCADE,
related_name='followed_courses',
limit_choices_to={'role': 'student'}
)
添加回答
举报