Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django multiple User profiles/subprofiles

Tags:

django

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 none specifically address what I'm looking to do. Below are the (stripped down) files I have so far.

What's the best way to create a profile model, with subprofiles for each user type? I'm trying not to require a custom authentication backend if at all possible.

https://gist.github.com/1196077

like image 297
erikankrom Avatar asked Dec 17 '22 09:12

erikankrom


2 Answers

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

like image 68
jc.vargas.valencia Avatar answered Jan 03 '23 18:01

jc.vargas.valencia


You need a combination of storing additional information about users and model inheritance.

Basically, you'll need the generic User models we all know and either love or hate, and then you need a generic profile model that is your AUTH_PROFILE_MODULE setting.

That profile model will be a top-level model, with model subclasses for internal and extrernal users. You probably don't want an abstract model in this case since you'll need a common profile table to load user profiles from with User.get_profile().

So...I think the major thing you want to change is to make your Associate, External, etc. models inherit from your Profile model.

like image 35
Luke Sneeringer Avatar answered Jan 03 '23 19:01

Luke Sneeringer