保存后的用户基于分配的user_group处于非活动状态-Django Allauth [英] post save user is inactive based on assigned user_group - Django Allauth

查看:63
本文介绍了保存后的用户基于分配的user_group处于非活动状态-Django Allauth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的用户组group_a和group_b.在注册时,用户可以选择他/她所属的组.我使用Django Allauth处理所有用户内容,因此我制作了一个自定义帐户适配器来处理额外的逻辑:

I have two different user groups group_a and group_b. On register a user can choose the group he/she belongs to. I am using Django Allauth to handle all the user stuff so I've made a custom account adapter to handle the extra logic:

custom_adapter.py

class UserAccountAdapter(DefaultAccountAdapter):
    def save_user(self, request, user, form, commit=True):

        user = super(UserAccountAdapter, self).save_user(request, user, form, commit=False)
        user.voornaam = form.cleaned_data.get('first_name')
        user.achternaam = form.cleaned_data.get('last_name')
        user.user_group = form.cleaned_data.get('user_group')
        user.save()
        user.groups.add(user.user_group)
        user.save()
        if user.groups == 'group_b':
            user.is_active = False
        else:
            user.is_active = True
        user.save()
        return user

在注册时,用户将被分配到它按预期选择的组.之后,我想将user.is_active = False分配给选择group_b的人.在上面的示例中,我正在使用3种保存方法(我认为这在DRY原理下是很多方法),但是我尝试了每种可能的组合.当我打印出user.groups时,在上例中的每个保存方法之后,它都设置为None.但是当我在管理面板中查看时,它仍然被分配了正确的方式.

On register the user gets assigned to the group it selects as intended. Afterwards I want to assign user.is_active = False to the people who select group_b. In the above example I am using 3 save methods (I think this is way to much under DRY principle) but I tried every possible combination. When I am printing out the user.groups it is set to None after every save method in the above example. But it still gets assigned the right way when i look in the admin panel.

解决此问题的最佳方法是什么?

What is the best method to tackle this?

** 编辑

为方便起见,我在下面添加了我的用户模型:

I've added my User model below for extra clearance:

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=254, unique=True)
    voornaam = models.CharField(max_length=254, null=True, blank=True)
    achternaam = models.CharField(max_length=254, null=True, blank=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    last_login = models.DateTimeField(null=True, blank=True)
    date_joined = models.DateTimeField(auto_now_add=True)

    USERNAME_FIELD = 'email'
    EMAIL_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = UserManager()

推荐答案

已修复!

我缺少用于过滤组名称的filter属性,也缺少.exists().

I was missing the filter attribute to filter on the name of the group and was missing the .exists().

对于感兴趣的人,下面的代码可以解决问题:

For anyone interested, below code did the trick:

class UserAccountAdapter(DefaultAccountAdapter):
    def save_user(self, request, user, form, commit=True):

        user = super(UserAccountAdapter, self).save_user(request, user, form, commit=False)
        user.voornaam = form.cleaned_data.get('first_name')
        user.achternaam = form.cleaned_data.get('last_name')
        user.user_group = form.cleaned_data.get('user_group')
        user.save()
        user.groups.add(user.user_group)
        if user.groups.filter(name='zakelijk').exists():
            user.is_active = False
        else:
            user.is_active = True
        user.save()
        return user

这篇关于保存后的用户基于分配的user_group处于非活动状态-Django Allauth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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