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

北战南征 提交于 2019-12-03 03:31:21

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.

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

That's the standard solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!