在django中为不同类型的用户拥有不同配置文件的最佳方法是什么? [英] What's the best way to have different profiles for different kinds of users in django?

查看:52
本文介绍了在django中为不同类型的用户拥有不同配置文件的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的申请中,我有学生,教授和工作人员.工作人员不需要个人资料,但教授和学生都需要不同的个人资料.我不想自己全部实现(中间件和其他工具),所以有什么方法可以让get_profile()根据用户角色返回不同的配置文件?

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?

推荐答案

对于目前处于测试阶段的Django 1.1,我将实现

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需要原始文件中的缓存代码,依此类推.但基本上您可以做类似的事情.

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

使用Django 1.0.x,您可以实现基于User的派生类,但这可能会在其他地方破坏代码.对于类似这样的东西,我喜欢代理类,它们仅添加python功能而无需更改数据库模型.

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.

这篇关于在django中为不同类型的用户拥有不同配置文件的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆