Skip to content Skip to sidebar Skip to footer

Ways To Create Reusable Sets Of Fields In Wagtail?

I'm evaluating Wagtail to see if I can find a place for it along side Wordpress and Drupal in my company. So far I think it's interesting and really like a lot of it, but there's o

Solution 1:

Define your common fields in an abstract base model, and inherit from that in your page classes whenever you want to use them:

from django.db import models
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.core.fields import RichTextField
from wagtail.core.models import Page


class HeroFields(models.Model):
    hero_image = models.ForeignKey('wagtailimages.Image', null=True, blank=True, on_delete=models.SET_NULL, related_name='+')
    hero_url = models.URLField()

    hero_field_panels = [
        FieldPanel('hero_image'),
        FieldPanel('hero_url'),
    ]

    class Meta:
        abstract = True


class HomePage(Page, HeroFields):
    body = RichTextField()

    content_panels = Page.content_panels + HeroFields.hero_field_panels + [
        FieldPanel('body'),
    ]

Post a Comment for "Ways To Create Reusable Sets Of Fields In Wagtail?"