User Role Diagram in Django

Big greeting community

My question is related to the type of managing users and schema users in Django. In the beginning, I ask you to apologize just in case that my questions may be too β€œnewbies” or without meaning, I’m starting to connect me with Django user schemes and their various possibilities of working in projects.

I have the following situation.

I am creating an application in which I will have three different user types:

  • Medicine
  • A patient
  • Physiotherapist

I am using the standard Django authentication scheme (django.contrib.auth).

At first, I thought about this entity scheme, in which the User table is a table auth_userwhere Django saves the created users:

The scheme is accepted (bad -?)

is_patient, is_medical is_physiotherapist, User.

, , Django .

, is_patient, is_medical is_physiotherapist .

User Userprofile, User Model OneToOne. :

Django User Model Extension

, , Django ...

, (patient, medical physiotherapist )?

Another scheme thought

:

...

?

Users UserProfile. ​​ ? ?


, , :

  • /

/, , Django User ( ) , ?

  1. Django

, . , , ....

? , , ? ? , , , ... , /

  1. AUTH_USER_MODEL

, ?

+4
4

, , , OneToOne .

class Medic(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    # other fields

class Physio(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    # other fields

class Patient(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    # other fields

, / ( - , Django, , ChiefMedical...).

,

def user_is_patient(user):
     ...

, , , , , ...

Django . , , , , , ( , , , !)

, (, )?

, , , ( ). , Medics Physios , , , .

Users UserProfile. ​​ ? ?

( ), . , (, "", "" "" ).

? , , ? ? , , , ... , /

() Django . (, , , ... ...)

Django .

, , . .

,

( ), , .

+6

, , , , .

(, , ), Django, () , , .

+3

, :

(https://cldup.com/jxVsgfneGb.png)

, , get_medical_profile, get_patient_profile get_physiotherapist_profile , , , (, ) OneToOneField, , . ( ), , : " ".

enter image description here

, , , , ​​ , :

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    # common fields shared by medical, patient and physiotherapist profiles

class MedicalUser(models.Model):
    profile = models.OneToOneField(UserProfile)
    # medical fields here

class PatientUser(models.Model):
    profile = models.OneToOneField(UserProfile)
    # patient fields here

class PhysiotherapistUser(models.Model):
    profile = models.ForeignKey(UserProfile)
    # patient fields here

, , , . .

, , , , , , , :

def is_medical_profile(profile):
    try:
        profile.medical_user
        return True
    except:
        return False

( ) :

{% if profile | is_medical_profile %}

AUTH_USER_MODEL

, .


:

, , .AUTH_USER_MODEL .

Django :

Django 1.5, ForeignKey, OneToOneField ManyToManyField

, :

from django.conf import settings
from django.db import models

class UserProfile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)

, , , Django .

+2

@geoom @Ire @lorenzo-peΓ±a Django, (is_medical, is_patient, is_physiotherapist) python

In [6]: User.objects.filter(username='agarcial').values('is_medical','is_patient','is_physiotherapist')

Out[6]: [{'is_physiotherapist': True, 'is_patient': True, 'is_medical': True}]

views.py , , (, )

# Create your views here.


class ProfileView(LoginRequiredMixin, TemplateView):
    template_name = 'profile.html'
    def get_context_data(self, **kwargs):
        self.request.session['Hi'] = True
        context = super(ProfileView, self).get_context_data(**kwargs)
        is_auth = False
        name = None
        # Check if in the request goes the user
        user = self.request.user

        # Check about of possible cases (For now is one profile)
        if user.is_medical:
        #if self.request.user.is_authenticated():
            print (user.is_medical)
            is_auth = True
            profile=user.get_medical_profile()
            #name = self.request.user.username

            data = {
                'is_auth':is_auth,
                'profile':profile,
            }

            context.update({'userprofile':profile, 'data':data})
        elif user.is_patient:
            print (user.is_patient)
            is_auth=True
            profile=user.get_patient_profile()

            data = {
                'is_auth':is_auth,
                'profile':profile,
            }
            context.update({'userprofile':profile,'data':data})
        elif user.is_physiotherapist:
            print (user.is_physiotherapist)
            is_auth=True
            profile=user.get_physiotherapist_profile()

            data = {
                'is_auth':is_auth,
                'profile':profile,
            }
            context.update({'userprofile':profile,'data':data})
        return  context

    def get_userprofile(self):
        return self.request.user.userprofile

If I checked other possible combinations (patient-patient, medical and physiotherapist), can this work?

I think, create groups for (Medicals, Patients, Physiotherapists) and linking users for the authorization topic, although I should consider other things for the authorization process, such as django guardian for example?

How about this?

0
source

Source: https://habr.com/ru/post/1619416/


All Articles