问题描述
我正在寻找一种在 wagtail.io 中实现双向 m2m 模型的方法.
I am looking for a way to implement bidirectional m2m models in wagtail.io.
- 一个作者可以写多篇文章和
- 一篇文章可以有多个作者.
- 我可以在作者页面和帖子页面上设置/取消设置两个模型之间的关系
- 在作者页面上设置的关系显示在发布页面上,反之亦然.
在 Django Admin 中,我使用普通的 filter_horizontal m2m 小部件和自定义通过参数解决了这个问题:
In Django Admin I solved this using the normal filter_horizontal m2m widget and a custom through parameter:
models.py:
class Author(models.Model):
posts = models.ManyToManyField('app.Post', blank=True, through=Post.authors.through)
class Post(models.Model):
authors = models.ManyToManyField('app.Author', blank=True)
我偶然发现了一种至少可以实现-way 关系使用内联 但是我看不出如何扭转这种局面来解决我的双向问题.
I stumbled upon an approach that at least enables a one-way relation using inlines however I cannot see how to turn this around to solve my bidirectional problem.
这就是我在 wagtail 中的进展:
This is how far I got in wagtail:
在 models.py 类 PostPage(Page) 中我定义了一个 InlinePanel:
In models.py class PostPage(Page) I defined an InlinePanel:
InlinePanel('related_agents', label="Related Agents"),
然后通过模型进一步向下定制(比较这篇博文):
and then further down a custom through model (compare to this blog post):
class PostPageRelatedAuthorItem(Orderable):
page = ParentalKey('PostPage', related_name='related_authors')
# one-to-one is the same as ForeignKey with unique=True
author = models.OneToOneField('thoughts.AgentPage')
panels = [
PageChooserPanel('author', 'app.AuthorPage'),
]
是否有双向方式,如果是,您能否帮我提供一些提示 - 非常感谢.
Is there a bidirectional way and if yes could you help me along with some hints - many thanks in advance.
推荐答案
由于 django-modelcluster 的一些限制,您不能将 M2M 字段与 Wagtail 一起使用.您必须基本上专门设置通过"模型.
Because of some restrictions around django-modelcluster you can't use M2M fields with Wagtail. You have to specifically setup the "through" model basically.
您可以在此处找到您需要的所有信息 http://www.tivix.com/blog/working-wagtail-i-want-my-m2ms/
You can find all the info you need here http://www.tivix.com/blog/working-wagtail-i-want-my-m2ms/
这篇关于wagtail:如何建立双向页面模型关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!