Best solution for extending user model twice (Student / Teacher)

198 Views Asked by At

I'm coding a application in Django where there are 2 types of users that need to login on the webapplication: teachers and students. Now I'm not sure what is the best approach for this. I already read a lot of tutorials and Stackoverflow questions that explains how to extend the user model, but I'm not sure which is the best option.

The requirements:

  • 2 kind of profiles with different fields needed (teacher and student)
  • Possibility to have separate admin "objects" where the superstaff of the application can login in the backend and manage the separate objects.

Option 1:

Make use of the method to extend the AbstractUser class. This works for now, however I don't know if I can use AUTH_USER_MODEL twice (teacher and student). Now it's one profile with both fields for students and teachers. With proxy models I can show only the necessary fields for teachers or students.

Option 2: (I think this is the best solution)

Make 2 models (Teacher and Student) with each a Foreignkey to User (OneToOneField). Problem here is that if the Administrator of the website creates a new teacher or student, they also need to create a separate user first (that can login) and then link it when creating the teacher or student. (I can solve this with signals I think).

Which is the best approach / best practice for this situation?

1

There are 1 best solutions below

1
Salaah Amin On

I'd personally go with option 2.

I'm not sure how you intend on creating the users (teachers/students). If it is via the admin panel, then you can create a user using a separate pop up whilst creating your teacher/student. Doing this will create that link.

If you are using a custom form, you can include the fields that you wish to include from the User model and your custom models. Then within the view, you can create model a row in the User model and your custom models and link the two.