如何在Django Admin中双向访问ManyToManyField?
Django 管理 filter_horizontal
设置提供了一个很好的小部件,用于编辑多对多关系。但这是一个需要字段列表的特殊设置,因此它仅在定义 ManyToManyField
的(管理员)模型上可用;我怎样才能在(管理员的)其他模型上获得相同的小部件,向后读取关系?
我的模型看起来像这样(请随意忽略 User
/UserProfile
复杂性;尽管这是真正的用例):
class Site(models.Model):
pass
class UserProfile(models.Model):
user = models.OneToOneField(to=User,unique=True)
sites = models.ManyToManyField(Site,blank=True)
我可以在管理表单上获得一个不错的小部件 < code>UserProfile 与
filter_horizontal = ('sites',)
但无法看到如何在 Site
管理员上获取等效内容。
我还可以通过向 SiteAdmin
添加内联来获得部分方式,定义为:
class SiteAccessInline(admin_module.TabularInline):
model = UserProfile.sites.through
虽然它是迂回且不方便的;该小部件对于简单管理多对多关系来说一点也不直观。
最后,有一个技巧此处描述,其中涉及定义另一个Site
上的 ManyToManyField
并确保它指向同一个数据库表(并跳过一些麻烦,因为 Django 并不是真正设计为在描述相同内容的不同模型上拥有不同字段数据)。我希望有人能给我展示一些更干净的东西。
The Django admin filter_horizontal
setting gives a nice widget for editing a many-to-many relation. But it's a special setting that wants a list of fields, so it's only available on the (admin for the) model which defines the ManyToManyField
; how can I get the same widget on the (admin for the) other model, reading the relationship backwards?
My models look like this (feel free to ignore the User
/UserProfile
complication; it's the real use case though):
class Site(models.Model):
pass
class UserProfile(models.Model):
user = models.OneToOneField(to=User,unique=True)
sites = models.ManyToManyField(Site,blank=True)
I can get a nice widget on the admin form for UserProfile
with
filter_horizontal = ('sites',)
but can't see how to get the equivalent on the Site
admin.
I can also get part-way by adding an inline to SiteAdmin
, defined as:
class SiteAccessInline(admin_module.TabularInline):
model = UserProfile.sites.through
It's roundabout and unhandy though; the widget is not at all intuitive for simply managing the many-to-many relationship.
Finally, there's a trick described here which involves defining another ManyToManyField
on Site
and making sure it points to the same database table (and jumping through some hoops because Django isn't really designed to have different fields on different models describing the same data). I'm hoping someone can show me something cleaner.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个(或多或少)整洁的解决方案,感谢 http://blog.abiss.gr/mgogoulos/entry/many_to_many_relationships_and< /a> 并修复了来自 http://code.djangoproject.com/ticket/5247 的 Django bug
它使用与
filter_horizontal
设置相同的小部件,但硬编码到表单中。Here's a (more or less) tidy solution, thanks to http://blog.abiss.gr/mgogoulos/entry/many_to_many_relationships_and and with a fix for a Django bug taken from http://code.djangoproject.com/ticket/5247
This uses the same widget as the
filter_horizontal
setting, but hard-coded into the form.