Goun2
Goun2

Reputation: 437

How to have multiple types of users in separated tables?

I'm building a project in which I'll have 3 types of users.

Super Admin Teacher Student

Teacher and Student will be in a table called Class, each Class will have one teacher and several Student.

As far as I know(Very limited), Django only provide one user table for all kinds of users, But I don't know how to go on, because I will need separated tables to keep the data organized and without redudance, in addition to relate with other tables.

Is there any kind of solution to solve this problem ?

Upvotes: 0

Views: 617

Answers (1)

Abijith Mg
Abijith Mg

Reputation: 2693

You can implement related_name attributes something similar to this:

from django.contrib.auth.models import User

class ClassRoom(models.Model):
    # One classroom one teacher
    teacher = models.ForeignKey(User, related_name="teacher")
    # One classroom many students
    student = models.ManytoManyField(User, blank=True, null=True,
                                     related_name="students")
    .....

Also refer to these links for more info:

Django teacher students easy solution. Use separate tables, or permissions and groups? How? Other ideas?

Django model with Foreign Key and ManyToMany relations to same model

Two sets of users (teacher and student) in Django authentication

Upvotes: 1

Related Questions