OK,我會建議堅持從Django的默認的用戶系統,並建立了特定類型的一到一個配置文件需要的地方。後者上,你可以根據外鍵的值的用戶之間進行區分,或者你可以實現其綁定到用戶類型
from django.db.models.query_utils import Q
# for example, this could be a way to extend users to hold teachers and students
class TeacherProfile(models.Model):
user = models.OneToOneField(User, related_name='teacher_profile')
# other relevant teacher profile items
ONLY_TEACHERS_FILTER = Q(teacher_profile__isnull=False) & Q(student_profile__isnull=True)
class StudentProfile(models.Model):
user = models.OneToOneField(User, related_name='student_profile')
# other relevant student profile items
sections = models.ManyToManyField('Section', related_name='students') # mind the quotes to Section name
class Section(models.Model)
name = models.CharField(max_length=50)
# other section fields goes here...
class Course(models.Model):
name = models.CharField(max_length=50)
teacher = models.ForeingKey(User, related_name='courses', limit_choices_to=ONLY_TEACHERS_FILTER)
sections = models.ManyToManyField(Section, related_name='courses')
我們問題的答案是什麼,其學生參加課程的權限到:
queryset = Course.objects.filter(section__students__in=[user])
希望它有幫助!
你有沒有試過做這件事。 – CurseStacker
你應該檢查https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#extending-the-existing-user-model – cdvv7788
@CurseStacker newobj = teacher.objects.raw('select * from app_teacher t,app_loginstudent l其中l.section = t.section和l.username =%s',[name])..name是學生姓名,我想列出其姓名 –