在 Django 管理中编辑组对象时将用户对象分配给组
在用户对象(编辑用户)的默认 Django 管理视图中,可以编辑用户的组成员身份。如果我也想反过来怎么办?即,在组编辑页面中,可以选择属于正在编辑的组的用户。
正如我所看到的,Django 没有从 Group 到 User 对象的 ManyToMany 映射,这使得不可能(?)针对这种特殊情况实现 ModelAdmin 类。如果我可以创建一个额外的 UsersOfGroup 模型类并将其在 Django 的 Group 模型的 ManyToMany 字段中用作直通属性,那么可能有一种方法。
有什么想法吗?这是否可以使用 ModelAdmin 技巧来实现,或者我只需要为编辑组创建自定义视图?
我已经检查了另外两个问题,但它们并没有完全做同样的事情:
和
更新: 克里斯的答案差不多就到了。 :) 该组有一个对用户集的引用,但它被称为user_set,而不是users。这些是我所做的改变:
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']
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.
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.
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:
Assigning a group while adding user in admin
and
Show group membership in admin
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()
and
if group.pk:
group.user_set = self.cleaned_data['users']
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您添加新组并同时将用户添加到组中,则上述保存方法将不起作用。问题是新组不会被保存(管理员使用 commit=False)并且没有主键。由于 save_m2m() 的目的是允许调用视图处理保存 m2m 对象,因此我创建了一个保存对象,将旧的 save_m2m 方法包装在新方法中。
The save method above won't work if you add a new group and simultaneously add users to the group. The problem is that the new group won't get saved (the admin uses commit=False) and won't have a primary key. Since the purpose of save_m2m() is to allow the calling view to handle saving m2m objects, I made a save object that wraps the old save_m2m method in a new method.
yourapp/admin.py
yourapp/admin.py
这是一种使用 Django 的 InlineModelAdmin 对象的更简单的方法(在这里回答曲伴诗.cc)
Here is a simpler approach that uses Django's InlineModelAdmin objects (answered here on Qubanshi.cc)
我使用的是 Django 2.1,并且正在使用 Chris 发布的解决方案,但正如 Cedric 所解释的那样,当添加新组并且同时将用户添加到新组时,它不会工作。不幸的是,他的代码也没有帮助,但我可以使用下面的修改版本让它工作。
编辑:我采纳了用户am70的建议(谢谢!)并使用了
get_user_model()
。此代码继续适用于 Django 3.1,并已使用 Python 3.6、3.7 和 3.8 进行了测试。I am on Django 2.1 and was using the solution posted by Chris, but like explained by Cedric, it wouldn't work when when a new group was added and simultaneously users were added to the new group. Unfortunately, his code didn't help either, but I could get it to work using this modified version below.
Edit: I included the suggestion of user am70 (thanks!) and made use of
get_user_model()
. This code continues to work with Django 3.1 and has been tested with Python 3.6, 3.7 and 3.8.