我创建了一个自定义用户,如何将他们添加到组中?
I created a custom User, how do I add them to a group?
我在名为 OUser 的项目中途在一个名为 accounts 的应用程序中创建了一个自定义用户。我在默认的 AuthUserGroups 中定义了一些现有的组。如何将 OUser 添加到组中?当我尝试时,出现错误 Invalid column name 'ouser_id'
。我需要做什么才能允许将 OUser 添加到我的预定义组中?我是否必须创建自定义组模型来放置我的 OUser?无论如何,我可以告诉 Auth_User_groups 它应该寻找 OUsers 吗?
accounts.admin
class OUserAdmin(BaseUserAdmin):
# The forms to add and change user instances
form = UserAdminChangeForm
add_form = UserAdminCreationForm
# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference specific fields on auth.User.
fieldsets = (
('Personal info', {'fields': ('first_name', 'last_name', 'email',)}),
('Permissions', {'fields': ('is_superuser', 'is_active', 'is_staff',)}),
)
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user.
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username',)}
),
)
search_fields = ('username',)
ordering = ('username',)
filter_horizontal = ()
list_filter = ('is_superuser',)
admin.site.register(OUser, OUserAdmin)
'''
There are also other dependencies on things, such as Auth_Group_permission, but I assume that it will be a similar fix to switching Auth_Group to using OUser.
How would I make groups contain references to Ouser. I have tried just swapping the Foreign key of the user_id in AuthUserGroups to contain a reference to OUser, but that didn't work. Maybe I missed something?
Thanks for the help!
AuthUserGroups
是一个 table,它保持 User
和 Group
之间的 ManyToMany
关系。它对这些模型有外键约束。您不能拥有与 OUsers
和 User
都具有外键关系的字段
您需要创建一个不同的 ManyToMany
关系形式 OUsers
到 Group
,通过单独的 table.
这是一个将其添加到 OUser 模型的示例
from django.contrib.auth.models import Group
class OUsers(models.Model):
...
groups = models.ManyToManyField(Group)
Docs 在多对多字段上
我在名为 OUser 的项目中途在一个名为 accounts 的应用程序中创建了一个自定义用户。我在默认的 AuthUserGroups 中定义了一些现有的组。如何将 OUser 添加到组中?当我尝试时,出现错误 Invalid column name 'ouser_id'
。我需要做什么才能允许将 OUser 添加到我的预定义组中?我是否必须创建自定义组模型来放置我的 OUser?无论如何,我可以告诉 Auth_User_groups 它应该寻找 OUsers 吗?
accounts.admin
class OUserAdmin(BaseUserAdmin):
# The forms to add and change user instances
form = UserAdminChangeForm
add_form = UserAdminCreationForm
# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference specific fields on auth.User.
fieldsets = (
('Personal info', {'fields': ('first_name', 'last_name', 'email',)}),
('Permissions', {'fields': ('is_superuser', 'is_active', 'is_staff',)}),
)
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user.
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username',)}
),
)
search_fields = ('username',)
ordering = ('username',)
filter_horizontal = ()
list_filter = ('is_superuser',)
admin.site.register(OUser, OUserAdmin)
'''
There are also other dependencies on things, such as Auth_Group_permission, but I assume that it will be a similar fix to switching Auth_Group to using OUser.
How would I make groups contain references to Ouser. I have tried just swapping the Foreign key of the user_id in AuthUserGroups to contain a reference to OUser, but that didn't work. Maybe I missed something?
Thanks for the help!
AuthUserGroups
是一个 table,它保持 User
和 Group
之间的 ManyToMany
关系。它对这些模型有外键约束。您不能拥有与 OUsers
和 User
您需要创建一个不同的 ManyToMany
关系形式 OUsers
到 Group
,通过单独的 table.
这是一个将其添加到 OUser 模型的示例
from django.contrib.auth.models import Group
class OUsers(models.Model):
...
groups = models.ManyToManyField(Group)
Docs 在多对多字段上