erikankrom
erikankrom

Reputation: 75

Django multiple User profiles/subprofiles

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

Upvotes: 4

Views: 4038

Answers (3)

jcvargas
jcvargas

Reputation: 501

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

Upvotes: 9

Luke Sneeringer
Luke Sneeringer

Reputation: 9428

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.

Upvotes: 4

Andrew
Andrew

Reputation: 21

Please check this excellent article that describes how to inherit from the User class and add your own information. For me, at least, this clearly seems to be the way to go: http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/

Using this method one should easily be able to add multiple user types to their Django application.

Upvotes: -1

Related Questions