在 Django Admin 中显示组成员
Show group members in Django Admin
是否可以设置Django Admin
页面显示哪个用户在哪个组?是否可以使用 Django Admin
页面将用户添加到组中?如果是,如何?
现在,我正在以编程方式将客户添加到 customers group
和卖家 sellers group
,但我在我的管理中看不到任何信息。
这是我的注册view
:
def register_customer(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
customer_registration_form = forms.CustomerRegistrationForm(request.POST)
if form.is_valid() and customer_registration_form.is_valid():
new_user = form.save()
new_customer_profile = UserCustomerProfile(user=new_user)
new_customer_profile.save()
customers_group = Group.objects.get(name='Customers')
new_user.groups.add(customers_group)
return render(request, 'registration/complete.html')
else:
#handle errors
customer_registration_form = forms.CustomerRegistrationForm()
form = UserCreationForm()
return render(request, "registration/register.html",
{'form': form, 'customer_registration_form': customer_registration_form})
我不确定这是否是您要查找的内容,但您可以像这样修改 Group 的 ModelAdmin(您应用的 admin.py):
from django.contrib.auth.admin import GroupAdmin
from django.contrib.auth.models import Group
admin.site.unregister(Group)
class UserInLine(admin.TabularInline):
model = Group.user_set.through
extra = 0
@admin.register(Group)
class GenericGroup(GroupAdmin):
inlines = [UserInLine]
Code based on this answer
现在您可以根据需要编辑内联。
我不认为这正是您要找的东西,但它是一个快速而肮脏的修复程序,可以完成工作。
免责声明:我不知道这是否是经过批准的 Django 做事方式,但我相信它不会太远。
CVi 的回答做了一些小的调整,但对于我的用例,我真的想要另一个 multi-select 小部件来管理组中的用户,就像 multi-select 用于管理组的权限。
以this answer为灵感,我想到了这个:
from django import forms
from django.contrib import admin
from django.contrib.auth.admin import GroupAdmin as origGroupAdmin
from django.contrib.auth.models import Group, User
class GroupAdminForm(forms.ModelForm):
"""
ModelForm that adds an additional multiple select field for managing
the users in the group.
"""
users = forms.ModelMultipleChoiceField(
User.objects.all(),
widget=admin.widgets.FilteredSelectMultiple('Users', False),
required=False,
)
def __init__(self, *args, **kwargs):
super(GroupAdminForm, self).__init__(*args, **kwargs)
if self.instance.pk:
initial_users = self.instance.user_set.values_list('pk', flat=True)
self.initial['users'] = initial_users
def save(self, *args, **kwargs):
kwargs['commit'] = True
return super(GroupAdminForm, self).save(*args, **kwargs)
def save_m2m(self):
self.instance.user_set.clear()
self.instance.user_set.add(*self.cleaned_data['users'])
class GroupAdmin(origGroupAdmin):
"""
Customized GroupAdmin class that uses the customized form to allow
management of users within a group.
"""
form = GroupAdminForm
# Register the modified GroupAdmin with the admin site
admin_site = admin.AdminSite(name='my_admin')
admin_site.register(Group, GroupAdmin)
是否可以设置Django Admin
页面显示哪个用户在哪个组?是否可以使用 Django Admin
页面将用户添加到组中?如果是,如何?
现在,我正在以编程方式将客户添加到 customers group
和卖家 sellers group
,但我在我的管理中看不到任何信息。
这是我的注册view
:
def register_customer(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
customer_registration_form = forms.CustomerRegistrationForm(request.POST)
if form.is_valid() and customer_registration_form.is_valid():
new_user = form.save()
new_customer_profile = UserCustomerProfile(user=new_user)
new_customer_profile.save()
customers_group = Group.objects.get(name='Customers')
new_user.groups.add(customers_group)
return render(request, 'registration/complete.html')
else:
#handle errors
customer_registration_form = forms.CustomerRegistrationForm()
form = UserCreationForm()
return render(request, "registration/register.html",
{'form': form, 'customer_registration_form': customer_registration_form})
我不确定这是否是您要查找的内容,但您可以像这样修改 Group 的 ModelAdmin(您应用的 admin.py):
from django.contrib.auth.admin import GroupAdmin
from django.contrib.auth.models import Group
admin.site.unregister(Group)
class UserInLine(admin.TabularInline):
model = Group.user_set.through
extra = 0
@admin.register(Group)
class GenericGroup(GroupAdmin):
inlines = [UserInLine]
Code based on this answer
现在您可以根据需要编辑内联。
我不认为这正是您要找的东西,但它是一个快速而肮脏的修复程序,可以完成工作。
免责声明:我不知道这是否是经过批准的 Django 做事方式,但我相信它不会太远。
CVi 的回答做了一些小的调整,但对于我的用例,我真的想要另一个 multi-select 小部件来管理组中的用户,就像 multi-select 用于管理组的权限。
以this answer为灵感,我想到了这个:
from django import forms
from django.contrib import admin
from django.contrib.auth.admin import GroupAdmin as origGroupAdmin
from django.contrib.auth.models import Group, User
class GroupAdminForm(forms.ModelForm):
"""
ModelForm that adds an additional multiple select field for managing
the users in the group.
"""
users = forms.ModelMultipleChoiceField(
User.objects.all(),
widget=admin.widgets.FilteredSelectMultiple('Users', False),
required=False,
)
def __init__(self, *args, **kwargs):
super(GroupAdminForm, self).__init__(*args, **kwargs)
if self.instance.pk:
initial_users = self.instance.user_set.values_list('pk', flat=True)
self.initial['users'] = initial_users
def save(self, *args, **kwargs):
kwargs['commit'] = True
return super(GroupAdminForm, self).save(*args, **kwargs)
def save_m2m(self):
self.instance.user_set.clear()
self.instance.user_set.add(*self.cleaned_data['users'])
class GroupAdmin(origGroupAdmin):
"""
Customized GroupAdmin class that uses the customized form to allow
management of users within a group.
"""
form = GroupAdminForm
# Register the modified GroupAdmin with the admin site
admin_site = admin.AdminSite(name='my_admin')
admin_site.register(Group, GroupAdmin)