Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with user roles in Django

Tags:

I have some question In a project I have the need of work with users which are of three (may be more) types of them have different roles: physician patient administrator

I have been thinking use of the Django Model Users and extend it creating a userprofile model ... But I ignore how to manage the different roles because, the userprofile model will have fields of the user model, althought I don't know how to address the roles topic.

1 User have Many userprofiles may be? I don't know

Or may be I will should create a Roles Model/Table in where I specify the roles types and create a relation with the Users Model. This is a good possibility.

Another possibility (as a comment more below) is check the Django permissions system, in which I can create user groups, and assign permissions to these groups, althought here I can only edit, create and delete models really?

I am a few confuse about of how to address this subject

Searching I found this app. https://github.com/dabapps/django-user-roles

If somebody can orient me about it, I will be much grateful Best Regards

like image 244
bgarcial Avatar asked Oct 30 '15 21:10

bgarcial


1 Answers

You could probably get this to work with django's built in permissions

That may be more than you need though. A simple solution would just be a UserProfile model with a role field with a OneToOneField to your user:

class UserProfile(models.Model):   user = models.OneToOneField(User, related_name="profile")   role = models.CharField() 

Set the roles and do checks with the roles in your views:

user.profile.role = "physician" user.profile.save()  if user.profile.role == "physician":   #do physician case here 
like image 73
sponrad Avatar answered Sep 19 '22 13:09

sponrad