无法使模型 @property def-as-field 与 wagtail 2.0 一起使用 [英] Cannot make a model @property def-as-field work with wagtail 2.0
问题描述
我将 Wagtail 2.0 与具有以下代码的自定义块一起使用:
I'm using Wagtail 2.0 with a custom Block that has the following code:
class LinkButtonBlock(blocks.StructBlock):
label = blocks.CharBlock()
URL = blocks.CharBlock()
styling = blocks.ChoiceBlock(
choices=[
('btn-primary', 'Primary button'),
('btn-secondary', 'Secondary button'),
('btn-success', 'Success button'),
('btn-info', 'Info button'),
('btn-warning', 'Warning button'),
('btn-error', 'Error button'),
],
default='btn-info',
)
outline = blocks.BooleanBlock(
default=False
)
@property
def css(self):
btn_class = self.styling
if self.outline is True:
btn_class = btn_class.replace('btn-', 'btn-outline-')
return btn_class
class Meta:
icon = 'link'
template = 'testapp/blocks/link_button_block.html'
如果我尝试访问这个 css
属性"在我的模板中,似乎什么也没发生.将 print(self)
作为第一行放在 css def 中也不会在控制台上显示任何内容,表明该函数甚至从未被调用.
If I then try to access this css
"property" in my template, nothing seems to happen. Putting a print(self)
as first line inside the css def also shows nothing on the console suggesting the function never even gets called.
使用以下模板:
{% load wagtailcore_tags %}
<a class="btn {{ block.value.css }}" href="{{ block.value.URL }}">{{ block.value.label }}</a>
简单的收益:
<a class="btn " href="actual.url.from.instance">actual.label.from.instance</a>
此外,block.value.styling
和 block.value.outline
自己的工作也很好,所以......我在这里做错了什么?>
Also, block.value.styling
and block.value.outline
on their own work just fine, so... what am I doing wrong here?
推荐答案
让您感到困惑的是,您在遍历 StreamField 时获得的值对象不是 StructBlock 的实例代码>.
StructBlock
和 CharBlock
等块对象充当不同数据表示之间的转换器;他们不会自己保留数据.在这方面,它们的工作方式很像 Django 的表单字段对象;例如,Django 的 forms.CharField
和 Wagtail 的 CharBlock
都定义了如何将字符串呈现为表单字段,以及如何从表单提交中检索字符串.
The thing that's tripping you up is that the value objects you get when iterating over a StreamField are not instances of StructBlock
. Block objects such as StructBlock
and CharBlock
act as converters between different data representations; they don't hold on to the data themselves. In this respect, they work a lot like Django's form field objects; for example, Django's forms.CharField
and Wagtail's CharBlock
both define how to render a string as a form field, and how to retrieve a string from a form submission.
请注意,CharBlock
适用于字符串对象 - 而不是 CharBlock
的实例.同样,从 StructBlock
返回的值不是 StructBlock
的实例 - 它们是 StructValue
类型的类似 dict 的对象,并且 this 是实现 css
属性所需的子类.在文档中有一个这样做的例子:http://docs.wagtail.io/en/v2.0/topics/streamfield.html#custom-value-class-for-structblock.应用于您的代码,这将变成:
Note that CharBlock
works with string objects - not instances of CharBlock
. Likewise, the values returned from StructBlock
are not instances of StructBlock
- they are a dict-like object of type StructValue
, and this is what you need to subclass to implement your css
property. There's an example of doing this in the docs: http://docs.wagtail.io/en/v2.0/topics/streamfield.html#custom-value-class-for-structblock. Applied to your code, this would become:
class LinkButtonValue(blocks.StructValue):
@property
def css(self):
# Note that StructValue is a dict-like object, so `styling` and `outline`
# need to be accessed as dictionary keys
btn_class = self['styling']
if self['outline'] is True:
btn_class = btn_class.replace('btn-', 'btn-outline-')
return btn_class
class LinkButtonBlock(blocks.StructBlock):
label = blocks.CharBlock()
URL = blocks.CharBlock()
styling = blocks.ChoiceBlock(choices=[...])
outline = blocks.BooleanBlock(default=False)
class Meta:
icon = 'link'
template = 'testapp/blocks/link_button_block.html'
value_class = LinkButtonValue
这篇关于无法使模型 @property def-as-field 与 wagtail 2.0 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!