如何在Wa中创建可重用的字段集? [英] Ways to create reusable sets of fields in Wagtail?

查看:41
本文介绍了如何在Wa中创建可重用的字段集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在评估Wagtail,以查看是否可以在公司的Wordpress和Drupal旁边找到它的位置.到目前为止,我认为它很有趣并且非常喜欢它,但是我真的很想做一件事,却找不到解决方法.

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.

我的商店使用模式库(la Atomic Design),最初,我对StreamField感到兴奋,它能够直接绑定到模式库,包括创建嵌套模式(可放在按钮中的可重复使用的按钮类).CTA和Hero Widget.但是流字段不适用于必须位于特定位置(可能位于内容流(英雄内容,号召性用语...)之外)的必需页面元素.

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...).

我发现了这个问题: https://github.com/wagtail/wagtail/issues/2048 但这似乎尚未解决,而且很长一段时间都没有活动.

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. 一个流字段,只能有一个块,并且其中一个的最小值和最大值.

缺点是UI似乎不知道最小/最大,因此您必须先保存页面,然后才被告知.此外,该表单也不会自动显示,您仍然必须单击CTA按钮.这对我的用户来说太混乱了(因为在WP和Drupal中这不是问题,所以我无法在我的商店中辩护).

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. 使用InlinePanel并需要单个实例的Django模型外键.

这里的缺点是用户仍然可以看到字段数据显示为灰色的添加"按钮,仍然看到删除"按钮,然后他们无法保存,因此页面在保存时不生效.

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.

我已经在源代码中看到了BlockField,但是绝对没有关于如何使用它的文档,看来这样的事情可以解决我的问题,但是我无法使其工作.

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?

推荐答案

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

这篇关于如何在Wa中创建可重用的字段集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆