Monday, 15 April 2013

django - Check user account model type -



django - Check user account model type -

what best way check model type of business relationship has extended django user model? have 2 types of user accounts (students , teachers, allow say) extend django user model:

class student(models.model): student_bd = models.charfield(max_length=50) student_user = models.foreignkey(user) ... class teacher(models.model): teacher_bd = models.charfield(max_length=50) teacher_user = models.foreignkey(user) ...

so, how can know if request.user pupil or teacher?

if have user model , want check if model corresponds either teacher or student model, need corresponding row each tables:

try: student.objects.get(student_user=request.user) is_student = true except student.doesnotexist: is_student = false

and same teacher. is_student , is_teacher tell if user student or teacher respectively. work in odd case if user both pupil and teacher :) not useful in case, thought it'd help other roles in school.

django django-users

No comments:

Post a Comment