Mario
Mario

Reputation: 2639

wagtail: How to make bidirectional page model relations?

I am looking for a way to implement bidirectional m2m models in wagtail.io.

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.

Upvotes: 1

Views: 1403

Answers (1)

Frank Wiles
Frank Wiles

Reputation: 1608

Because of some restrictions around django-modelcluster you can't use M2M fields with Wagtail. You have to specifically setup the "through" model basically.

You can find all the info you need here http://www.tivix.com/blog/working-wagtail-i-want-my-m2ms/

Upvotes: 0

Related Questions