django 无法将用户添加到组

发布于 2024-10-11 07:47:53 字数 916 浏览 0 评论 0原文

我有这个用户类别:

class Parent(User):
    contact_means = models.IntegerField()
    is_active = False
    is_staff = False
    is_superuser = False
    #parent_id = models.AutoField()
    class Meta:
        db_table = 'parent'

在我的管理页面中添加父级期间,我想自动将此父级添加到用户组中:

class ParentAdmin(admin.ModelAdmin):
    def save_model(self,request,modelObj,modelFormIns, change):
        if not change: #new entry
            #save regular parent and user
            parentGroup = Group.objects.get(pk=1)

            modelObj.save()
            modelObj.groups.add(parentGroup)
            #parentGroup.save()
            #save a record under django-registration
            profile_callback = None
            RegistrationProfile.objects.create_inactive_user_from_fk(user = modelObj,send_email = True)

        else:
            modelObj.save()

这不起作用!没有用户添加到组中。为什么?

I have this user class:

class Parent(User):
    contact_means = models.IntegerField()
    is_active = False
    is_staff = False
    is_superuser = False
    #parent_id = models.AutoField()
    class Meta:
        db_table = 'parent'

During adding of Parent in my admin page, I want to auto add this parent into a user group:

class ParentAdmin(admin.ModelAdmin):
    def save_model(self,request,modelObj,modelFormIns, change):
        if not change: #new entry
            #save regular parent and user
            parentGroup = Group.objects.get(pk=1)

            modelObj.save()
            modelObj.groups.add(parentGroup)
            #parentGroup.save()
            #save a record under django-registration
            profile_callback = None
            RegistrationProfile.objects.create_inactive_user_from_fk(user = modelObj,send_email = True)

        else:
            modelObj.save()

This doesn't work! no user is added to the group. why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2024-10-18 07:47:53

我认为可能是 groups 字段导致了问题。在将 exclude = ['groups'] 添加到我的 modeladmin 时,我没有任何问题。可能是在上面的代码中以编程方式添加组后,用户组会被刷新,因为在表单本身上没有选择任何组。

I figured it might the groups field that could be causing the problem. On adding exclude = ['groups'] to my modeladmin, I do not have any problem. Probably it could be that after adding the group programmatically in my code above, the user groups get flushed after that because on the form itself no groups were selected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文