在django中为不同用户设置不同的配置文件的最佳方式是什么? [英] What's the best way to have different profiles for different kinds of users in django?
问题描述
使用目前处于测试阶段的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屋!