Working with user roles in Django
Asked Answered
B

2

23

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

Banns answered 30/10, 2015 at 21:40 Comment(3)
django-user-roles was done 4 years ago and is not updated. It looks like it was made for Django 1.3. I wouldn't recommend using it.Impellent
It's true Andrés. I am currently considering other options, between which include the model user customization. Thanks for the clarificationBanns
My pleasure! I would consider this too: docs.djangoproject.com/en/1.8/topics/auth/default/…Impellent
O
30

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
Otherworld answered 30/10, 2015 at 22:13 Comment(11)
Hi @Otherworld I've been exploring, and, a UserProfile Model is used generally for add extra information or extra attributes to the User Model, without I have that modify the User model ... For example add an avatar picture to an user ... Under this thinking, UserProfile is different of UserRole if we speak about of models or data structures. Are you agree with it? I think that I can create a UserRole Model and I can say that One User have Many UserRoles ... for example ...Banns
Other alternative is study the permissions system in Django (you referred me above) and I can see the possibility of create groups and assign privileges or permissions and assign them to the users. Each group can be a role? It's possible.Banns
The name "UserProfile" is arbitrary, the table represents any information that you want tied to a user whether it is profile picture, birthday, role, whatever.Otherworld
If your role system is more complex then you can move on to having a role be its own model. This would work better if one person can have multiple roles or if you need to store information on a persons role. For example say someone is an administrator of something, but in addition to just stating that they are the administrator you want to store a bunch of fields about their specific administrator role, that would belong in its own table.Otherworld
If I define a Patient model/table and a Physician model/table independent of UserProfiles model ... How to can I make that these users (patients and physicians) sign in in the system and work them as a user model properties? Inheriting of User model?Banns
It is probably not a good idea to make tables for each role (like Patient, Physician etc), keep it under one table (Role). I think you should read the Django docs for Models: docs.djangoproject.com/en/1.8/topics/db/models Read it several times. Especially the example with the Beatles about Many to Many relationships, which it sounds like you are headed towards. In that example just substitute the Person table with the User class.Otherworld
I understood the ManyToMany relationshipwhen I want add extra fields through a intermediate model assigned and don't automatically generated by Django. But I have some doubts about of How to can I distinguished when an user is physician or patient in the moment of register them or create them? I explain my question in the following comment more below ..Banns
I have the following tables/models: User and UserProfile. I decide one ManyToMany relationship for them and the UserProfile govern the relationship ponting to intermediate Model named Membership. Until here all is o.k, I think so :P.Banns
My doubt is the following: In the UserProfile Model I wiil should define all the fields that I will need for the users (Patient and Physician) that I will have in my app. When I register some of these users via forms (I think use the UserCreationForm class - github.com/django/django/blob/master/django/contrib/auth/… -) ... and in the class Meta I redefine the fields adding these fields that I need? ,,, I don't know if I wrote clear my question for you ...Banns
For this you need probably want to create a new custom form that inherits from UserCreationForm and adds the fields that you want to the form. I do this in one of my apps, here is another example stackoverflow question that shows an example. #5745697. Fields you add to the form can be accessed in the view.Otherworld
How to use user groups for permission?Radiotelegraph
R
3

See here I have a simple solution for you!

If my assumption is right then there will be a admin who will create a user for the application right ?

So for admin make it easy by giving the choices in database and that is the solution.. admin will select roles from the drop down and then your work is done..

You have model for your user so add that fields in user model like this.

here I giving sample code to you


class CreateUser(models.Model):
    ROLES = (

        ('Patient', 'Patient'),
        ('Doctor', 'Doctor'),
        ('Physician', 'Physician'),

    )

    name= models.CharField(max_length=100, null=True)
    last_name= models.CharField(max_length=100, null=True)
    roles = models.CharField(max_length=50, choices = ROLES, null=True)
    date_joined = models.DateField(auto_now_add=True)

    def __str__(self):
        return self.name

Hope this will make your work ....

Rambow answered 29/7, 2020 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.