Re: Checking for user type in view
You can use a user profile for a lot more, and there's no reason the two roles have to be mutually exclusive when you're creating your own class. I just wanted to make that point. I have never used the built-in groups myself, but from your explanation it seems like a good solution for the original problem. Shawn -- You received this message because you are subscribed to the Google Groups "Django users" group. 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.
Re: Checking for user type in view
On 25/04/2011 7:15am, Kenny Meyer wrote: Hello guys, In my application models I have two models, Judge and Participant: Another way might be to use django groups. Have a participant group and a judge group and pop your users into one or the other. I prefer this approach for my own purposes because it means the user can be either or both. When it comes right down to it they are both users. Mike from django.contrib.auth.models import User class Judge(User): pass class Participant(User): pass In my view I want to find out if the authenticated user is either a Judge or a Participant. How can I do that? I have done the following, and it works most of the time for me: def index(request): user = request.user if user.is_authenticated(): if user.is_superuser: return redirect('/admin') judge = None participant = None competition = None try: participant = user.participant or None judge = user.judge or None if participant: competition = participant.competition if judge: competition = judge.competition except Participant.DoesNotExist, e: pass except Judge.DoesNotExist, e: pass except Exception, e: raise # Do some more stuff... But this is ugly. It would be cool if you could come up with better ideas. Kenny -- You received this message because you are subscribed to the Google Groups "Django users" group. 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.
Re: Checking for user type in view
On Sun, Apr 24, 2011 at 5:23 PM, Shawn Milochikwrote: > It's not recommended that you subclass User. > > Better: Create a class to use for a user profile and associate it with > the User using the instructions here: > > http://docs.djangoproject.com/en/1.3/topics/auth/#storing-additional-information-about-users I should have read that section before. This is much better in the long run. Thanks, Shawn. -- You received this message because you are subscribed to the Google Groups "Django users" group. 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.
Re: Checking for user type in view
> I have done the following, and it works most of the time for me: > > def index(request): > user = request.user > if user.is_authenticated(): > if user.is_superuser: > return redirect('/admin') > > judge = None > participant = None > competition = None > try: > participant = user.participant or None > judge = user.judge or None > if participant: > competition = participant.competition > if judge: > competition = judge.competition > except Participant.DoesNotExist, e: > pass > except Judge.DoesNotExist, e: > pass > except Exception, e: > raise > # Do some more stuff... Actually this *doesn't* work most of the time for me, because the moment user.participant raises an exception I'm screwed. -- You received this message because you are subscribed to the Google Groups "Django users" group. 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.
Re: Checking for user type in view
It's not recommended that you subclass User. Better: Create a class to use for a user profile and associate it with the User using the instructions here: http://docs.djangoproject.com/en/1.3/topics/auth/#storing-additional-information-about-users Then give that class a 'role' field or something and use that. You can use request.user.get_profile() to get at it in your views. It's cleaner and easier than subclassing User. Shawn -- You received this message because you are subscribed to the Google Groups "Django users" group. 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.
Checking for user type in view
Hello guys, In my application models I have two models, Judge and Participant: from django.contrib.auth.models import User class Judge(User): pass class Participant(User): pass In my view I want to find out if the authenticated user is either a Judge or a Participant. How can I do that? I have done the following, and it works most of the time for me: def index(request): user = request.user if user.is_authenticated(): if user.is_superuser: return redirect('/admin') judge = None participant = None competition = None try: participant = user.participant or None judge = user.judge or None if participant: competition = participant.competition if judge: competition = judge.competition except Participant.DoesNotExist, e: pass except Judge.DoesNotExist, e: pass except Exception, e: raise # Do some more stuff... But this is ugly. It would be cool if you could come up with better ideas. Kenny -- You received this message because you are subscribed to the Google Groups "Django users" group. 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.