I have spend all yesterday making this run, and I have a solution.

 I dont know if its the best, but see it and please tel me if there is a 
mor professional way to do it:

first I add in customize my user 
profile<https://docs.djangoproject.com/en/dev/topics/auth/#customizing-the-user-model>
 in 
my model.py :

models.py

from django.db import modelsfrom 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')

but there was a problem and its that if i follow the django instructions, 
its will be imposible to add different user profiles so I found in 
apost<http://stackoverflow.com/questions/6662844/django-user-profiles-of-different-types>
  a 
solution  so I spend a time searching how to and this is the way:

I create a own 
MiddleWare<https://docs.djangoproject.com/en/dev/topics/http/middleware/#process_request>:
 
so

i create middleware.py in my app

from django.contrib.auth.models import Userfrom encuestas.models import 
Pollster, Respondent
class RequestMiddleWare(object):
    """docstring for """
    def process_request(self,request):
        if isPollster(request.user):
            request.user.profile = Pollster.objects.get( user = request.user.id)
        elif isRespondent(request.user):
            request.user.profile = 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  configure settings.py for the middleware adding to MIDDLEWARE_CLASSES 
atribute this line:

'encuestas.middleware.RequestMiddleWare'

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


in this wahy i dont have to declare

AUTH_USER_MODEL = 'myapp.MyUser'

so I dont have plroblems with other apps an I just control my redirect in 
my login submit view this way:


def display_user_profile(request):
    res = ""
    if hasattr(request.user,'profile'):
        if request.user.profile.__class__.__name__ == "Pollster":
            res = "I am a Pollster"
            return HttpResponse(res)
        elif request.user.profile.__class__.__name__ == "Respondent":
            res = "I am a Respondent"
            return HttpResponse(res)
    elif request.user.__class__.__name__ == "User":
        res  = "I am a Admin or Unregitred user"
        return HttpResponse(res)
    elif request.user.__class__.__name__ == "AnonymousUser": 
        res  = "I am a AnonymousUser" 

return HttpResponse(res)    

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/a-XPTdByg-MJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to