本文整理汇总了Python中wagtail.core.fields.StreamField方法的典型用法代码示例。如果您正苦于以下问题:Python fields.StreamField方法的具体用法?Python fields.StreamField怎么用?Python fields.StreamField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wagtail.core.fields
的用法示例。
在下文中一共展示了fields.StreamField方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_lazy_load
# 需要导入模块: from wagtail.core import fields [as 别名]
# 或者: from wagtail.core.fields import StreamField [as 别名]
def test_lazy_load(self):
"""
Getting a single item should lazily load the StreamField, only
accessing the database once the StreamField is accessed
"""
with self.assertNumQueries(1):
# Get the instance. The StreamField should *not* load the image yet
instance = StreamModel.objects.get(pk=self.with_image.pk)
with self.assertNumQueries(0):
# Access the body. The StreamField should still not get the image.
body = instance.body
with self.assertNumQueries(1):
# Access the image item from the stream. The image is fetched now
body[0].value
with self.assertNumQueries(0):
# Everything has been fetched now, no further database queries.
self.assertEqual(body[0].value, self.image)
self.assertEqual(body[1].value, 'foo')
示例2: editor_css
# 需要导入模块: from wagtail.core import fields [as 别名]
# 或者: from wagtail.core.fields import StreamField [as 别名]
def editor_css():
# Make 'heading' StreamField blocks look like h2 in RichTextBlocks in the
# Wagtail Admin.
return (
'''
<style>
.fieldname-heading input {
color: #666;
font-family: Roboto Slab, Georgia, serif;
font-size: 2.4em;
font-weight: bold;
}
</style>
'''
)
示例3: test_lazy_load_no_images
# 需要导入模块: from wagtail.core import fields [as 别名]
# 或者: from wagtail.core.fields import StreamField [as 别名]
def test_lazy_load_no_images(self):
"""
Getting a single item whose StreamField never accesses the database
should behave as expected.
"""
with self.assertNumQueries(1):
# Get the instance, nothing else
instance = StreamModel.objects.get(pk=self.no_image.pk)
with self.assertNumQueries(0):
# Access the body. The StreamField has no images, so nothing should
# happen
body = instance.body
self.assertEqual(body[0].value, 'foo')
示例4: test_lazy_load_queryset
# 需要导入模块: from wagtail.core import fields [as 别名]
# 或者: from wagtail.core.fields import StreamField [as 别名]
def test_lazy_load_queryset(self):
"""
Ensure that lazy loading StreamField works when gotten as part of a
queryset list
"""
with self.assertNumQueries(1):
instances = StreamModel.objects.filter(
pk__in=[self.with_image.pk, self.no_image.pk])
instances_lookup = {instance.pk: instance for instance in instances}
with self.assertNumQueries(1):
instances_lookup[self.with_image.pk].body[0]
with self.assertNumQueries(0):
instances_lookup[self.no_image.pk].body[0]
示例5: test_lazy_load_queryset_bulk
# 需要导入模块: from wagtail.core import fields [as 别名]
# 或者: from wagtail.core.fields import StreamField [as 别名]
def test_lazy_load_queryset_bulk(self):
"""
Ensure that lazy loading StreamField works when gotten as part of a
queryset list
"""
file_obj = get_test_image_file()
image_1 = Image.objects.create(title='Test image 1', file=file_obj)
image_3 = Image.objects.create(title='Test image 3', file=file_obj)
with_image = StreamModel.objects.create(body=json.dumps([
{'type': 'image', 'value': image_1.pk},
{'type': 'image', 'value': None},
{'type': 'image', 'value': image_3.pk},
{'type': 'text', 'value': 'foo'}]))
with self.assertNumQueries(1):
instance = StreamModel.objects.get(pk=with_image.pk)
# Prefetch all image blocks
with self.assertNumQueries(1):
instance.body[0]
# 1. Further image block access should not execute any db lookups
# 2. The blank block '1' should be None.
# 3. The values should be in the original order.
with self.assertNumQueries(0):
assert instance.body[0].value.title == 'Test image 1'
assert instance.body[1].value is None
assert instance.body[2].value.title == 'Test image 3'
示例6: test_lazy_load_get_prep_value
# 需要导入模块: from wagtail.core import fields [as 别名]
# 或者: from wagtail.core.fields import StreamField [as 别名]
def test_lazy_load_get_prep_value(self):
"""
Saving a lazy StreamField that hasn't had its data accessed should not
cause extra database queries by loading and then re-saving block values.
Instead the initial JSON stream data should be written back for any
blocks that have not been accessed.
"""
with self.assertNumQueries(1):
instance = StreamModel.objects.get(pk=self.with_image.pk)
# Expect a single UPDATE to update the model, without any additional
# SELECT related to the image block that has not been accessed.
with self.assertNumQueries(1):
instance.save()
示例7: test_can_read_non_json_content
# 需要导入模块: from wagtail.core import fields [as 别名]
# 或者: from wagtail.core.fields import StreamField [as 别名]
def test_can_read_non_json_content(self):
"""StreamField columns should handle non-JSON database content gracefully"""
self.assertIsInstance(self.nonjson_body.body, StreamValue)
# the main list-like content of the StreamValue should be blank
self.assertFalse(self.nonjson_body.body)
# the unparsed text content should be available in raw_text
self.assertEqual(self.nonjson_body.body.raw_text, "<h1>hello world</h1>")
示例8: test_non_blank_field_is_required
# 需要导入模块: from wagtail.core import fields [as 别名]
# 或者: from wagtail.core.fields import StreamField [as 别名]
def test_non_blank_field_is_required(self):
field = StreamField([('paragraph', blocks.CharBlock())], blank=False)
self.assertTrue(field.stream_block.required)
示例9: test_blank_field_is_not_required
# 需要导入模块: from wagtail.core import fields [as 别名]
# 或者: from wagtail.core.fields import StreamField [as 别名]
def test_blank_field_is_not_required(self):
field = StreamField([('paragraph', blocks.CharBlock())], blank=True)
self.assertFalse(field.stream_block.required)
示例10: _add_streamfields
# 需要导入模块: from wagtail.core import fields [as 别名]
# 或者: from wagtail.core.fields import StreamField [as 别名]
def _add_streamfields(cls: wagtailPage, node: str, dict_params: dict, app: str, prefix: str) -> None:
from .types.streamfield import (
block_handler,
stream_field_handler,
)
for field in cls._meta.fields:
if isinstance(field, StreamField):
field_name = field.name
stream_field_name = f"{node}{string.capwords(field_name, sep='_').replace('_', '')}"
blocks = field.stream_block.child_blocks
handlers = dict(
(name, block_handler(block, app, prefix))
for name, block in blocks.items()
)
f, resolve = stream_field_handler(
stream_field_name,
field_name,
handlers
)
dict_params.update({
field.name: f,
"resolve_" + field.name: resolve
})