django 无法将用户添加到组
我有这个用户类别:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为可能是 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.