在Django Admin中显示组成员

时间:2016-01-02 16:54:59

标签: python django django-models django-admin django-permissions

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

2 个答案:

答案 0 :(得分:6)

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)

答案 1 :(得分:3)

我不确定这是否是您要找的,但您可以像这样修改ModelAdmin for Group(您应用的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做事方式,但我相信它并不太遥远。