0

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?

RVE
  • 328
  • 4
  • 17
  • I'd go with 2. That way, if required, an user could be both a student and a teacher (that could happen in some schools!) without additional headache. – AKX Jul 31 '21 at 13:10

1 Answers1

1

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.

Salaah Amin
  • 382
  • 3
  • 14
  • Ok, thanks! It will be used mostly from the admin panel, so I will go for option 2, the One To One Field. – RVE Aug 01 '21 at 19:53