赞
踩
class User(AbstractUser): is_student = models.BooleanField(default=False) is_teacher = models.BooleanField(default=False)如果用户还有其他属性,可以使用一对一关系来记录
class Student(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) quizzes = models.ManyToManyField(Quiz, through='TakenQuiz') interests = models.ManyToManyField(Subject, related_name='interested_students')
class User(AbstractUser): is_student = models.BooleanField(default=False) is_teacher = models.BooleanField(default=False)如果用户类型比较多,则这样列出来就比较混乱了,可以采用choices的方式
class User(AbstractUser): USER_TYPE_CHOICES = ( (1, 'student'), (2, 'teacher'), (3, 'secretary'), (4, 'supervisor'), (5, 'admin'), ) user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES)但是上面的方式有个缺陷,那就是你要么是student,要么是teacher,而不能同时属于两种角色,要实现这样的场景,可以新建Role表
class Role(models.Model): ''' The Role entries are managed by the system, automatically created via a Django data migration. ''' STUDENT = 1 TEACHER = 2 SECRETARY = 3 SUPERVISOR = 4 ADMIN = 5 ROLE_CHOICES = ( (STUDENT, 'student'), (TEACHER, 'teacher'), (SECRETARY, 'secretary'), (SUPERVISOR, 'supervisor'), (ADMIN, 'admin'), ) id = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, primary_key=True) def __str__(self): return self.get_display_id() class User(AbstractUser): roles = models.ManyToManyField(Role)下面是流程图
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。