I am looking for a way to implement bidirectional m2m models in wagtail.io.
- One author can write multiple posts and
- one post can have multiple authors.
- I can set/unset a relation between the two models both on the author page and on the post page
- A relation set on the Author page shows on the Post page and vice versa.
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)
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.
This is how far I got in wagtail:
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.