Django multiple User profiles/subprofiles

前端 未结 3 854
别那么骄傲
别那么骄傲 2020-12-30 17:42

I am trying to create an intranet/extranet with internal/external user-specific profiles, with a common generic profile. I\'ve looked at several answers on this site, but n

3条回答
  •  离开以前
    2020-12-30 18:18

    I have a solution I dont Know if its the best but see it:

    models.py

    from django.db import models
    from django.contrib.auth.models import User
    
    class Pollster(models.Model):
        """docstring for Polister"""
        user   = models.OneToOneField(User, related_name = 'polister', unique=True)
        cedule = models.CharField( max_length = 100 ) 
    
    class Respondent(models.Model):
        """ """
        born_date   = models.DateField( verbose_name=u'fecha de nacimiento' )
        cedule      = models.CharField( max_length = 100, verbose_name=u'cedula' ) 
        comunity    = models.CharField( max_length = 100, verbose_name=u'comunidad')
        phone       = models.CharField( max_length = 50, verbose_name=u'telefono')
        sanrelation = models.TextField( verbose_name =u'Relacion con SAN')
        user        = models.OneToOneField( User, related_name = 'respondent')
    

    I create a MiddleWare: so

    i create middleware.py

    from django.contrib.auth.models import User
    from encuestas.models import Pollster, Respondent
    
    class RequestMiddleWare(object):
        """docstring for """
        def process_request(self,request):
            if isPollster(request.user):
                request.user.userprofile = Pollster.objects.get( user = request.user.id)
            elif isRespondent(request.user):
                request.user.userprofile = Respondent.objects.get(user = request.user.id)   
            return None   
    
    def isPollster(user):
        return Pollster.objects.filter(user=user.id).exists()
    
    def isRespondent(user):
        return Respondent.objects.filter(user=user.id).exists()
    

    and you need to configure settings.py for the middleware: add to MIDDLEWARE_CLASSES atribute:

    'encuestas.middleware.RequestMiddleWare'
    

    encuestas is my_app name middleware is the Middleware file RequestMiddleWare is the middleware class

提交回复
热议问题