使用auth.User的Django多个配置文件。标准学生老师的例子

时间:2022-10-03 23:21:15

Is it correct way to create 2 profiles in django? I want student to have access to his teachers, and teacher to his students as well. I mean if I log in as a student, I want to browse exactly teachers who teach me, and if I log in as a teacher, I want to browse exactly students which I teach. That is way I rather can not create one profile for both teachers and students.

在django中创建2个配置文件是否正确?我希望学生能够接触他的老师,也希望老师能够接触他的学生。我的意思是,如果我以学生身份登录,我想浏览一下教我的老师,如果我以老师的身份登录,我想浏览我教的学生。这是我不能为教师和学生创建一个配置文件的方式。

class Teacher(models.Model):
    ...
    user = models.OneToOneField(User)

class Student(models.Model):
    ...
    user = models.OneToOneField(User)
    teacher = models.ManyToManyField(Teacher)

if so, my AUTH_PROFILE_MODULE should look that way?

如果是这样,我的AUTH_PROFILE_MODULE应该是这样的吗?

AUTH_PROFILE_MODULE = 'school.Teacher, school.Student'

would be grateful for any answers. Thanks!

会对任何答案表示感谢。谢谢!

1 个解决方案

#1


1  

You can make use of a single profile;

您可以使用单个配置文件;

class People(models.Model):
    user = models.ForeignKey(User)
    teachers = models.ManyToManyField("self")

Then you can access the necessary information with;

然后你可以访问必要的信息;

teacher = People.objects.get(pk=<teacher_pk>)
students = People.objects.filter(teachers=teacher)

student = People.objects.get(pk=<student_pk>)
teachers = student.teachers.all()

#1


1  

You can make use of a single profile;

您可以使用单个配置文件;

class People(models.Model):
    user = models.ForeignKey(User)
    teachers = models.ManyToManyField("self")

Then you can access the necessary information with;

然后你可以访问必要的信息;

teacher = People.objects.get(pk=<teacher_pk>)
students = People.objects.filter(teachers=teacher)

student = People.objects.get(pk=<student_pk>)
teachers = student.teachers.all()