Ways to create reusable sets of fields in Wagtail?

最后都变了- 提交于 2021-01-27 10:19:50

问题


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 one thing I would really like and can't find a way to do it.

My shop uses a pattern library (a la Atomic Design) and initially I was excited by the StreamField and it's ability to tie directly in to a pattern library, including creating nested patterns (a reusable button class that can be put in a CTA and a Hero Widget. But the stream field doesn't work for required page elements that have to be in a certain location, possibly outside the content flow (hero content, calls to action...).

I found this issue: https://github.com/wagtail/wagtail/issues/2048 But it doesn't seem to be resolved and hasn't had activity in a long time.

Right now I've found two possible solutions:

  1. A stream field with only one block possible, and a min and max of one of them.

The drawback is that the UI doesn't seem to be aware of the min/max and you have to save the page before you're told. Also, the form isn't automatically visible, you still have to click the CTA button. This would be too confusing for my users (and I can't make the case to my shop since this isn't a problem in WP and Drupal).

  1. A Foreign Key to a Django Model, using the InlinePanel and requiring a single instance.

The drawback here is that the user still sees a grayed out add button and still sees the delete button for the field data, which they can do, then the page doesn't validate on save.

I've seen a BlockField in the source code, but there's absolutely no docs on how to use it, it seems like such a thing would solve my problems, but I can't get it to work.

Am I missing something? Is there a way to create a reusable set of fields that I can easily attach to page types, and cause the fields to be visible on page load and presented in a way that users will understand right away?


回答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'),
    ]


来源:https://stackoverflow.com/questions/57578250/ways-to-create-reusable-sets-of-fields-in-wagtail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!