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是学生姓名,我想列出其姓名 –