Django has an awesome user model coupled with authentication. Using that, I want to define multiple user types, say a student and a professor. I want to do this using minimal change to the existing model.
One of the ways would be the User Groups. Another would be to extend the user model and store a flag. Which would be a better method for my problem, given that the two entities aren't entirely independent and a member of type one can also be a member of type two.
If I understand exactly your problem my choice would be an
OneToOneField
.When I'm working on projects where the clients want customized users, one of the best solution is to create a new model with a
OneToOneField
pointing to the Django User like aProfile
model. When you have anUser
object, you can douser.profile
and you get the profile related with user (so you can douser.profile.any_profile_field
).I recommend you to use this kind of solution because it is easy to manage and scalable. If in 1 month you need to add a new property/field/value to an User, with this solution you only need to change this new model.
Here you can add to the Profile model as many fields as you need, and it's easy to manage because if you have the user you have the profile and viceversa
EDIT
If you use this, your
authenticate
method can remain the same as it is right no.You could do things as this example:
So you can use the profile fields in your login function, or when an user is accessing to some url, and check the type user to allow or not.
Example to control a template where only teachers can enter: