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

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

问题描述

在我的申请中,我有学生,教授和工作人员。员工不需要个人资料,但是教授和学生们都需要不同的个人资料。我宁可不自己实现它(中间件和whatnot),所以有没有根据用户的角色,get_profile()返回不同的配置文件?

解决方案

使用目前处于测试阶段的Django 1.1,我将实现一个代理模型

  class MyUser(User):

class Meta:
proxy = True

def get_profile(self):
如果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需要原始的缓存代码等等。但是实际上你可以这样做。



使用Django 1.0.x,您可以根据User实现派生类,但这可能会破坏其他地方的代码。对于像我这样的东西我喜欢代理类,它只是添加python功能而不改变数据库模型。


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?

解决方案

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.

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

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