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

bgarcial
  • 2,915
  • 10
  • 56
  • 123

2 Answers2

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
sponrad
  • 680
  • 6
  • 9
  • Hi @sponrad 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 ... – bgarcial Nov 01 '15 at 19:10
  • 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. – bgarcial Nov 01 '15 at 19:21
  • 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. – sponrad Nov 01 '15 at 23:08
  • 7
    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. – sponrad Nov 01 '15 at 23:11
  • 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? – bgarcial Nov 02 '15 at 00:10
  • 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: https://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. – sponrad Nov 02 '15 at 17:15
  • 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 .. – bgarcial Nov 05 '15 at 21:05
  • 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. – bgarcial Nov 05 '15 at 21:24
  • 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 - https://github.com/django/django/blob/master/django/contrib/auth/forms.py -) ... 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 ... – bgarcial Nov 05 '15 at 21:24
  • 1
    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. http://stackoverflow.com/questions/5745197/django-create-custom-usercreationform-basic. Fields you add to the form can be accessed in the view. – sponrad Nov 10 '15 at 03:47
  • How to use user groups for permission? – Lucky Apr 06 '20 at 08:07
2

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 ....

Maddy
  • 76
  • 7