1

I am stuck with the schema creation so could you provide me an idea to implement this scenario

i do have 4 types of users Student, Teacher, Parent and Admin

i have tried to create 4 tables for each users and a table for username, password and tokens but i am not able to relate this table to the users because more than one user cannot have same username

What i want is i need to authenticate each user withe their user name and password and the Student table might be having relationship with Parent table !!!!

so while authenticating i need to know which type of user he/she is

i am using python Django 1.9

1
  • if would be helpful if you can show what you have done =) Commented Mar 22, 2016 at 5:51

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So can i implement relation between Parent user and student user also ? and the teacher user might have more fields so can i add it as optional fields in the User model ?
Yes, you can. You can add optional fields depending on type of user. I would suggest to create something like Profile model so that your User model remains clean.
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.

Comments

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?

2 Comments

So can i implement relation between Parent user and student user also ? and the teacher user might have more fields so can i add it as optional fields in the User model ?
Adding it to the user class is not advisable. As mentioned below you can use a Profile class and implement a one to one relation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.