[Answered ]-Create DB tables with different user types and authenticate user with username and password

1👍

Instead of creating four separate tables, you can simply add a field which will reflect the user_type of the user.

class CustomUser(AbstractBaseUser):

    ...
    [other model fields]
    ...

    USER_TYPE_CHOICES = (
        ('student', 'Student'),
        ('teacher', 'Teacher'),
        ('parent', 'Parent'),
        ('admin', 'Admin'),
    )

    user_type = models.CharField(choices=USER_TYPE_CHOICES, max_length=7)

By doing this, all users will have unique usernames and they will become easy to manage too.

You can check type of user just by accessing its user_type, it’ll return one of this text values “Student, Teacher, Parent and Admin“. So you’ll be able to handle business logic for different user types.

👤v1k45

1👍

You can use the Django authentication system :
Using the Django authentication system
and for each category of user you can use : Groups

django.contrib.auth.models.Group models are a generic way of categorizing users so you can apply
permissions, or some other label, to those users. A user can belong to
any number of groups.

0👍

Creating four tables is not the correct way.
Alternatively you can ,

class User():

    id = Column(PrimaryKey)
    username = Column(String, constraints)
    password = Column(String, constraints)
    usertype = Column(Enum('student','teacher','parent','admin'))

— The code is only for representation .

For setting user role , you can use the Enum Property. In case you are using SQLALchemy , check this out

How to create ENUM in SQLAlchemy?

Leave a comment