Search code examples
djangomodelforeign-keysmany-to-manywagtail

Wagtail page model: foreign key entity + multiple parameters


i'm using Wagtail CMS to create product catalogue. I created basic page type for product:

class Product(Page):    

It has basic fields like title, description, image aso. But i need "something special":

There is special part available in many variants and each product can have some of them. So I created another model, very simple by:

@register_snippet
class Variant(models.Model):

to store all variants. Variant has name and image. There are about 200 products and 30 variants.

My problem is and I don't know how to manage in Wagtail two tasks:

  1. to link Product with Variants (foreign key) with many-to-many relation to select product related variants in same page as other page entities

  2. each relations has additional parameters (2 params) which are relation specific (material and diameter) and again I haven't found how to display and manage such relations in page editor

I know that Django can handle it by inline formsets (django admin supports it out of box), but is there Wagtail-way to get this done and editable by Wagtail editor? I prefer to manage whole product in the same place, not relations separated in django-admin.

Thanks for any help or advice.


Solution

  • InlinePanel is the Wagtail equivalent of Django admin's inline formsets. An example of this is given in Wagtail's tutorial: https://docs.wagtail.io/en/stable/getting_started/tutorial.html#images

    In this case, it's setting up a many-to-many relation between pages and images, with an additional parameter (caption) on the relation; your Product -> Variant relation could be set up in the same way.