Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django registration and multiple profiles

I'm using django-registration in my application. I want to create different kinds of users with different profiles. For example, one user is a teacher and another user is a student.

How can I modify registration to set the user_type and create the right profile?

like image 537
Ste Avatar asked Jun 23 '10 09:06

Ste


2 Answers

Long answer :p

I've found The Missing Manual post invaluable for this kind of problem as it explains many of features of the django-profiles and django-registration systems.

I'd suggest using multi table inheritance on the single profile you're allowed to set via the AUTH_PROFILE_MODULE

For instance

#models.py
class Profile(models.Model):
    #add any common fields here (first_name, last_name and email come from User)

    #perhaps add is_student or is_teacher properites here
    @property
    def is_student(self):
        try:
            self.student
            return True
        except Student.DoesNotExist:
            return False

class Teacher(Profile):
    #teacher fields

class Student(Profile):
    #student fields

django-registration uses signals to notify you of a registration. You should be creating the profile at that point so you are confident that calls to user.get_profile() will always return a profile. The signal code used is

#registration.signals.py
user_registered = Signal(providing_args=["user", "request"])

Which means when handling that signal you have access to the request made. So when you POST the registration form include a field that identifies what type of user to create.

#signals.py (in your project)
user_registered.connect(create_profile)

def create_profile(sender, instance, request, **kwargs):
    from myapp.models import Profile, Teacher, Student

    try:
        user_type = request.POST['usertype'].lower()
        if user_type == "teacher": #user .lower for case insensitive comparison
            Teacher(user = instance).save()
        elif user_type == "student":
            Student(user = instance).save()
        else:
            Profile(user = instance).save() #Default create - might want to raise error instead
    except KeyError:
        Profile(user = instance).save() #Default create just a profile

If you want to add anything to the model that is created, that isn't covered by default field values, at registration time you can obviously pull that from the request.

like image 53
Chris Avatar answered Nov 09 '22 09:11

Chris


http://docs.djangoproject.com/en/1.2/topics/auth/#groups

Django groups are a great way to define what you are looking for. You can have one User extended profile that will contain all attributes of teachers and students.

class MasterProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    # add all the fields here

Then define groups: teacher and student and you associate each MasterProfile to either a teacher or a student.

Django Group table can help you define your various roles and allocate users to groups correctly.

like image 35
Rami_H Avatar answered Nov 09 '22 08:11

Rami_H