當前位置: 首頁>>代碼示例>>Python>>正文


Python fields.StreamField方法代碼示例

本文整理匯總了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') 
開發者ID:wagtail,項目名稱:wagtail,代碼行數:23,代碼來源:test_streamfield.py

示例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>
        '''
    ) 
開發者ID:freedomofpress,項目名稱:securethenews,代碼行數:17,代碼來源:models.py

示例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') 
開發者ID:wagtail,項目名稱:wagtail,代碼行數:16,代碼來源:test_streamfield.py

示例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] 
開發者ID:wagtail,項目名稱:wagtail,代碼行數:17,代碼來源:test_streamfield.py

示例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' 
開發者ID:wagtail,項目名稱:wagtail,代碼行數:31,代碼來源:test_streamfield.py

示例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() 
開發者ID:wagtail,項目名稱:wagtail,代碼行數:16,代碼來源:test_streamfield.py

示例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>") 
開發者ID:wagtail,項目名稱:wagtail,代碼行數:9,代碼來源:test_streamfield.py

示例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) 
開發者ID:wagtail,項目名稱:wagtail,代碼行數:5,代碼來源:test_streamfield.py

示例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) 
開發者ID:wagtail,項目名稱:wagtail,代碼行數:5,代碼來源:test_streamfield.py

示例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
            }) 
開發者ID:tr11,項目名稱:wagtail-graphql,代碼行數:29,代碼來源:actions.py


注:本文中的wagtail.core.fields.StreamField方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。