Vishnu
Vishnu

Reputation: 152

How do we make use of multiple user types in Django

I'm trying to create a web application which has 2 types of users i.e. students and universities. I want to have a different login page and different views after logging in for each of them. They need to have different permissions. I want the university to be able to list itself. The student needs to be able to view the list of all universities. Which is the best way to do this? I read other answers but I couldn't find a suitable solution to help me with this.
As of now, I have managed to make use of the inbuilt User model to do the authentication but I read that it is not advised to do that.

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('django.contrib.auth.urls')),
]

Is there any website which explains how to use multiple user models as I searched for it and didn't find it. I am still new to Django so kindly help me out. Any site to help me learn this would be highly appreciated.

Upvotes: 0

Views: 123

Answers (2)

Andrew Zame
Andrew Zame

Reputation: 1

This question makes sense, but wouldn't it be better if you have like a table for users and another table for user permissions based on the user types.

Upvotes: 0

Gavin
Gavin

Reputation: 874

I would use the normal user model provided with Django to handle the login. Then I would create a separate model for universities(because the university does not need the same fields as a user).

I would extend only the student model, maybe using one of the two options suggested by the docs here Extending User Model. Then depending on the user, assign them to a group as university staff or student. Then give the group certain permissions and use the Django auth to manage and protect different views based on who logs in.

Best of luck

Upvotes: 2

Related Questions