Hi! Nice post, was really useful to me. But now I want to expand on this. I have different models that I want to relate to a User of the system (lets say, Student and Teacher), each with different fields. How can I differentiate the registration of these?
From this, you can make different UserProfiles like indicated from the post. For instance:
fromdjango.dbimportmodelsfromdjango.contrib.authimportget_user_modelUser=get_user_model()classTeacherProfile(models.Model):user=models.OneToOne(User,limit_choices_to={'role':'Teacher'},on_delete=models.CASCADE,related_name="teacher_profile")# your custom fields for teacher model
def__str__(self):returnself.user.usernameclassStudentProfile(models.Model):user=models.OneToOne(User,limit_choices_to={'role':'Student'},on_delete=models.CASCADE,related_name="student_profile")# write your custom fields for student profile from here.
def__str__(self):returnself.user.username
After that has been set up, you'll just make a post_save signal that listens to the type of role on the new UserAccount instance. If it's a teacher, initialize TeacherProfile instance and populate it.
This can be achieved in a multistep form. Same for the student profile.
Hi! Nice post, was really useful to me. But now I want to expand on this. I have different models that I want to relate to a User of the system (lets say, Student and Teacher), each with different fields. How can I differentiate the registration of these?
hey Fmesteban,
really happy you found this post helpful.
One way you can approach this is by extending the Django AbstractUser model and introducing a field by the name role.
From this, you can make different UserProfiles like indicated from the post. For instance:
After that has been set up, you'll just make a post_save signal that listens to the type of role on the new UserAccount instance. If it's a teacher, initialize TeacherProfile instance and populate it.
This can be achieved in a multistep form. Same for the student profile.
Thanks for the quick response! I had about the same idea but wanted to make sure that was the way to go.
Thanks again!
You're welcome :)
sir i want to use this role choice in the login process hwo can i do ti ?