I am starting a new project and I created my user model extending the AbstractBaseUser, like below:
class NewUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField( unique=True)
user_name = models.CharField(max_length=150, unique=True)
...
I am wondering whether it is possible and makes sense to create my user (e.g. Teacher)
by extending NewUser, and adding all of the Teacher profile info.
That would be something like:
class Teacher(NewUser):
role = models.CharField(max_length=150)
title = models.CharField(max_length=150)
date_of_birth = models.CharField(max_length=150)
...
Would this be good practice and would it allow me to use the built-in authentication methods with DRS?
And would this give more flexibility if then I want to enable other users (e.g. Students) to also register an account and log-in?
Or would it make more sense to just use NewUser directly?
Would Really appreciate some advice/ insight!
CodePudding user response:
I'll suggest using OneToOneField like this:
class Teacher(models.Model):
user = models.OneToOneField(NewUser, related_name='teacher')
# Other fields that related to Teacher
class Student(models.Model):
user = models.OneToOneField(NewUser, related_name='student')
# Other fields that related to Student
So, NewUser model will be responsible only for the login, signup, and permissions.
