在Django管理员中编辑组对象时,将用户对象分配给组 [英] Assign User-objects to a Group while editing Group-object in Django admin

查看:970
本文介绍了在Django管理员中编辑组对象时,将用户对象分配给组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户对象(编辑用户)的默认Django管理视图中,可以编辑用户的组成员资格。如果我想要这样做呢另外呢?即在组编辑页面中,可以选择属于正在编辑的组的用户。

In the default Django admin view for user-object (edit user) one can edit the user's group memberships. What if I wanted this the other way around also? I.e. in the group editing page one could select the users that belong to the group being edited.

如我所见,Django没有从组到用户的ManyToMany映射对象,这使得不可能(?)为这种特定情况实现一个ModelAdmin类。如果我可以创建一个额外的UsersOfGroup模型类,并在Django的Group模型的ManyToMany字段中使用它作为一个通过属性,可能会有一种方法。

As I see this, Django doesn't have a ManyToMany mapping from Group to User object which makes it impossible(?) to implement a ModelAdmin class for this particular case. If I could make an additional UsersOfGroup model class and use it in the Django's Group model's ManyToMany field as a through-attribute, there could be a way.

任何想法,是这可能使用ModelAdmin技巧来实现,或者我只需要为编辑组自定义视图?

Any ideas, is this possible to implement using ModelAdmin tricks or do I just have to make a custom view for editing groups?

我已经检查了这两个其他问题,但是他们不太做同样的事情:

I have checked these two other questions, but they don't quite do the same thing:

在管理员中添加用户时分配组

< a href =https://stackoverflow.com/questions/1180628/django-users-and-groups-show-group-membership-in-admin>在管理员中显示组成员资格

更新:
Chris的答案几乎在那里。 :)该组对用户设置的引用,但它被称为 user_set ,而不是用户。所以这些是我所做的改变:

Updated: The answer from Chris was almost there. :) The group has a reference to the users set, but it's called user_set, not users. So these are the changes I made:

if self.instance and self.instance.pk:
    self.fields['users'].initial = self.instance.user_set.all()

if group.pk:
    group.user_set = self.cleaned_data['users']

谢谢!

推荐答案

yourapp / admin.py

from django import forms
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin.widgets import FilteredSelectMultiple

from django.contrib.auth.models import User, Group

class GroupAdminForm(forms.ModelForm):
    users = forms.ModelMultipleChoiceField(
        queryset=User.objects.all(), 
        required=False,
        widget=FilteredSelectMultiple(
            verbose_name=_('Users'),
            is_stacked=False
        )
    )

    class Meta:
        model = Group

    def __init__(self, *args, **kwargs):
        super(GroupAdminForm, self).__init__(*args, **kwargs)

        if self.instance and self.instance.pk:
            self.fields['users'].initial = self.instance.users.all()

    def save(self, commit=True):
        group = super(GroupAdminForm, self).save(commit=False)

        if commit:
            group.save()

        if group.pk:
            group.users = self.cleaned_data['users']
            self.save_m2m()

        return group

class GroupAdmin(admin.ModelAdmin):
    form = GroupAdminForm

admin.site.unregister(Group)
admin.site.register(Group, GroupAdmin)

这篇关于在Django管理员中编辑组对象时,将用户对象分配给组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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