Answer the question
In order to leave comments, you need to log in
How to make multiple user types in Django 1.7?
Hello.
What's the best way to support multiple user types in Django?
Let's take an abstract example Teachers & Students
Each type of user should have their own profile and dashboard, login and registration.
The user at the same time cannot be Teacher'om and Student'om
to look aside AbstractUser?
Answer the question
In order to leave comments, you need to log in
Yes. Another BoolField or https://docs.djangoproject.com/en/1.7/ref/forms/fi...
In django, there should be only one model per user. Everything is nailed down - bad architecture.
If they have very different fields in the model, then you have to make two ModelForms and use them.
Through AbstractBaseUser, you can make your own user model and extend it. And you can also inherit from the created model and make a separate model with user profiles where you can fence whatever you like. Not everything is nailed down.
It is more reasonable to do not models.BooleanField but models.IntegerField with choices. Maybe in the future you want to expand the values and add more types of users.
In principle, everything is possible in jung. The question is how you want to organize the storage of this data in the database.
Offhand 3 options:
a. Emulate old django user_profile(). Three tables, three User/TeacherProfile/StudentProfile models in the user model you set up the type field, in the user.get_profile() method check the user type and return the desired profile
b. One common table with many fields, three User/Teacher/Student models. Teacher and Student are proxy models for User. The user model still has a type field, and with a little hack , you can replace the class with the right one at the time of its creation.
with. Well, the most difficult option. Three plates, three User/Teacher/Student models. Teacher and Student are inherited from User. The type field can be done, it can not be done. The essence of the hack is to replace the User model manager by making select_related(Student, Teacher) for any request. And when iterating over objects, cast the type to the required one by filling in the fields from the request. For an example, you can look here or here
UPDATE:
And if you spread the user keys, you can make a request right away in the auth backend and return the desired class. It turns out two plates, two models not connected in any way.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question