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

2

6

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?

Lamarckian answered 24/3, 2009 at 17:50 Comment(0)
G
7

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.

Gamopetalous answered 24/3, 2009 at 18:23 Comment(2)
These look useful but this still poses the exact same problem I have with my own use of multiple user/profile types: existing apps don't know about it. They just create User instances, so this nice method goes completely ignored.Clothesline
Yes, multiple user models is still somewhat problematic, but the different profiles based on roles can be solved.Gamopetalous
M
0

Have you rad http://docs.djangoproject.com/en/dev/topics/auth/#auth-profiles?

That's the standard solution.

Magel answered 24/3, 2009 at 18:13 Comment(2)
I have read that. What I need is different kinds of profiles for different kinds of users, that solution will only give one kind of profile for all users.Lamarckian
Please update your question with an example of "different kinds of profiles". Usually, we define profiles with multiple fields, some of which are optional. What are you talking about?Magel

© 2022 - 2024 — McMap. All rights reserved.