添加 django admin 多对多小部件
默认情况下,多对多关系的管理小部件将显示在包含对 ManyToManyField 的实际引用的模型上。
有没有办法让类似的小部件出现在另一个模型(未定义关系的模型)的管理页面上?
In the django admin documentation, it says the following:
By default, admin widgets for many-to-many relations will be displayed on whichever model contains the actual reference to the ManyToManyField.
Is there a way to get a similar widget to appear on the admin page for the other model, the one where the relationship isn't defined?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种不同的方法可以达到您想要的效果。
这是一种方法,它会给你带来类似(但不相同)的效果,并且可能需要最少的编码。 (示例将使用类
A
和B
,假设前者具有显式定义的多对多关系)最快的方法:您可以使用
InlineModelAdmin 对象:
如果您想要获得
There's a couple different ways to get the effect that you're after.
Here's one way, which will get you a similar (but not identical) effect, and probably requires the least coding. (Examples will use classes
A
andB
, assuming that the former has the many to many relationship explicitly defined)The quickest way: you can use an
InlineModelAdmin
object:If you want the exact effect of getting a
<select multiple>
, the way you can do it is to use a customForm
class, and assign it toBAdmin.form
.