Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to have different profiles for different kinds of users in django?

In my application I have students, professors and staff. Staff members do not need a profile but professors and students each need a different profile. I'd rather not implement it all myself (middleware and whatnot), so is there anyway to just have get_profile() return a different profile depending on a user's role?

like image 500
Marcos Marin Avatar asked Mar 24 '09 17:03

Marcos Marin


1 Answers

With Django 1.1, which is currently in beta, I would implement a proxy model.

class MyUser(User):

  class Meta:
    proxy = True

  def get_profile(self):
    if self.role == 'professor':
      return ProfessorProfile._default_manager.get(user_id__exakt=self.id)
    elif self.role == 'student':
      return StudentProfile._default_manager.get(user_id__exakt=self.id)
    else:
      # staff
      return None

get_profile needs the caching code from the original and so on. But essentially you could do something like that.

With Django 1.0.x you could implement derived classes based on User, but this might break code in other places. For stuff like that I love proxy classes, which just add python functionality without changing the database models.

like image 185
Oliver Andrich Avatar answered Dec 07 '22 20:12

Oliver Andrich