Django:通过 Django Admin 将用户添加到组 [英] Django: Add user to group via Django Admin

查看:69
本文介绍了Django:通过 Django Admin 将用户添加到组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在更改组"的 Django 管理界面中将用户添加到组中?

How can I add users to a group in Django admin interface of "Change Group"?

我已经看到了一些肮脏的技巧来使它适用于旧 django 版本.

I have seen dirty hacks to get this working for older django version.

如何用 Django 1.10 解决这个问题?

How to solve this with Django 1.10?

强调:我希望在更改组"页面上,而不是在更改用户"页面上.

Emphasize: I want this on the page "Change Group", not on "Change User".

我想在 django-admin-style 中有这个:没有编码,只是做一些配置.可能是这样的:

I would like to have this in django-admin-style: No coding, just doing some configuration. Maybe like this:

class GroupAdmin(admin.ModelAdmin):
    show_reverse_many_to_many = ('user',)

推荐答案

你需要写一些代码.请注意,Django 管理站点是普通的 Django 视图和表单!

You need to write some code. Note that the Django admin site is normal Django views and forms!

首先创建一个ModelForm:

First create a ModelForm:

from django import forms
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.admin.widgets import FilteredSelectMultiple    
from django.contrib.auth.models import Group


User = get_user_model()


# Create ModelForm based on the Group model.
class GroupAdminForm(forms.ModelForm):
    class Meta:
        model = Group
        exclude = []

    # Add the users field.
    users = forms.ModelMultipleChoiceField(
         queryset=User.objects.all(), 
         required=False,
         # Use the pretty 'filter_horizontal widget'.
         widget=FilteredSelectMultiple('users', False)
    )

    def __init__(self, *args, **kwargs):
        # Do the normal form initialisation.
        super(GroupAdminForm, self).__init__(*args, **kwargs)
        # If it is an existing group (saved objects have a pk).
        if self.instance.pk:
            # Populate the users field with the current Group users.
            self.fields['users'].initial = self.instance.user_set.all()

    def save_m2m(self):
        # Add the users to the Group.
        self.instance.user_set.set(self.cleaned_data['users'])

    def save(self, *args, **kwargs):
        # Default save
        instance = super(GroupAdminForm, self).save()
        # Save many-to-many data
        self.save_m2m()
        return instance

我们添加了一个自定义组 ModelForm.第二步是取消注册原来的 Group admin 并注册一个新的 Group admin 来显示我们的 ModelForm:

We added a custom Group ModelForm. The second step is to unregister the original Group admin and register a new Group admin that displays our ModelForm:

# Unregister the original Group admin.
admin.site.unregister(Group)

# Create a new Group admin.
class GroupAdmin(admin.ModelAdmin):
    # Use our custom form.
    form = GroupAdminForm
    # Filter permissions horizontal as well.
    filter_horizontal = ['permissions']

# Register the new Group ModelAdmin.
admin.site.register(Group, GroupAdmin)

这篇关于Django:通过 Django Admin 将用户添加到组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆