1 回答

TA貢獻1943條經(jīng)驗 獲得超7個贊
limit_choices_to=…
是的,您可以使用參數(shù) [Django-doc]過濾此內(nèi)容:
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'}
? ? )
然而,參數(shù)related_name=…
[Django-doc]是反向關(guān)系的名稱。因此,這是一種訪問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'}
? ? )
添加回答
舉報