在Django Admin中显示群组成员

ht4b089n  于 2022-12-01  发布在  Go
关注(0)|答案(2)|浏览(131)

是否可以设置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})
qmelpv7a

qmelpv7a1#

CVi的答案做了一些小的调整,但对于我的用例,我真的希望另一个多选小部件来管理组中的用户,就像管理组权限的多选一样。
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)
6ojccjat

6ojccjat2#

我不确定这是否是您要查找的内容,但您可以对ModelAdmin for Group进行如下修改(您admin.py的应用的www.example.com):

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 认可的做事方式,但我相信这并不遥远。*

相关问题