首页 文章

使用django-userena处理各种用户配置文件组

提问于
浏览
1

我已成功实现了一种方法,使用userena为每个注册URL使用不同的表单创建属于不同组的用户,继承userena注册表单并覆盖save方法以将用户包括在一个组或另一个组中 .

例如在我的/ brands / urls中我有:

url(r'^signup/$',
'userena.views.signup',
{'template_name': 'userena/signup_form_brands.html', 'signup_form': SignupFormBrands}
),

我以这种形式:

from userena.forms import SignupForm
from django.contrib.auth.models import Group


class SignupFormBrands(SignupForm):

def save(self):
    # First save the parent form and get the user.
    new_user = super(SignupFormBrands, self).save()
    new_user.groups.add(Group.objects.get(name='Brands'))
    return new_user

所以我用userena中包含的电池得到了我需要的东西 . 但现在我想继续使用includerena的配置文件编辑/查看功能,但有两种不同的配置文件 . 我想创建2个不同的配置文件模型,一个用于我的默认用户,一个用于品牌 . 然后我希望userena能够根据属于一个组或另一个组的用户编辑正确类型的配置文件模型 . 我不确定这是如何工作的,我怎么做 .

编辑:userena使用 profile = user.get_profile() 编辑配置文件,因此我将尝试通过编辑此类来分配不同的配置文件对象 .

1 回答

  • 0

    您可以通过以下代码覆盖User.get_profile:

    original_get_profile = User.get_profile
    def get_profile(self):
        if getattr(settings, 'AUTH_PROFILE_MODULE', None) != 'profiles.Profile':
            return original_get_profile(self)
        if not hasattr(self, '_profile_cache'):
            self._profile_cache = self.profile
            self.profile.user = self
        return self._profile_cache
    User.get_profile = get_profile
    

    将它放在models.py文件中的某个位置 . 代码从这里复制:http://pastebin.com/MP6bY8H9

相关问题