如何在页面 API 中获取图像 url 或下载图像的 url,其中图像是由流场创建的?

Lal*_*s M 5 django django-rest-framework wagtail

在我的 wagtail 应用程序中,我有一个流字段,用于使用 ImageChooserBlock 上传图像以及标题和文本。这意味着在单个流字段中我有一个标题、一个文本和一个图像上传输入。我正在尝试在其余框架的页面 API ( ) 中获取图像 url localhost:8000/api/v2/pages/[page-id]。但是这个pages api只给出了上传图片的image id,如下

{
    "type": "avengers",
    "value": {
        "title": "Tony Stark",
        "avengers": [
            {
                "image": 1,            /******* this is the image id returned ********/
                "title": "Iron Man",
                "text": "Iron man is now in framework"
            }
        ]
    },
    "id": "2f27cb24"
} 
Run Code Online (Sandbox Code Playgroud)

如果我访问 images api( http://localhost:8000/api/v2/images/1/) 我会得到download_url如下信息

{
    "id": 1,
    "meta": {
        "type": "wagtailimages.Image",
        "detail_url": "http://localhost/api/v2/images/1/",
        "tags": [],
        "download_url": "/media/original_images/avenger.jpeg"
    },
    "title": "avenger.jpeg",
    "width": 400,
    "height": 400
}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何获取download_url页面 API 中的 或图像 url ( localhost:8000/api/v2/pages/[page-id])

我的复仇者块的streamfieldsblocks.py如下

class AvengersBlock(blocks.StructBlock):

    title = blocks.CharBlock(required=True, help_text="Add your title")

    Avengers = blocks.ListBlock(
        blocks.StructBlock(
            [
                ("image", ImageChooserBlock(required=True)),
                ("title", blocks.CharBlock(required=True, max_length=40)),
                ("text", blocks.TextBlock(required=True, max_length=200))
            ]
        )
    )

    class Meta:  # noqa
        template = "streams/Avengers_block.html"
        icon = "placeholder"
        label = "Avengers"
Run Code Online (Sandbox Code Playgroud)

该流字段在内容类型 model.py 中使用,如下所示

from django.db import models
from wagtail.admin.edit_handlers import FieldPanel, StreamFieldPanel
from wagtail.core.fields import StreamField
from wagtail.core.models import Page
from wagtail.api import APIField

from apps.common.streams import blocks

class AvengersPage(Page):

    tempalte = "avengers/avengers_page.html"  

    content = StreamField(
        [
            ("avengers", blocks.AvengersBlock())
        ],
        null=True,
        blank=True,
    )

    subtitle = models.CharField(max_length=100, null=True, blank=True)

    content_panels = Page.content_panels + [
        FieldPanel("subtitle"),
        StreamFieldPanel("content"),
    ]

    api_fields = [
        APIField("subtitle"),
        APIField("content")
    ]

    class Meta:  # noqa

        verbose_name = "Avengers Page"   
Run Code Online (Sandbox Code Playgroud)

小智 2

将其添加到您的 AvengersBlock 中,当您调用 API 时

/api/v2/pages/?type=home.AvengersPage&fields=content

您应该会看到您正在寻找的 JSON。


def get_api_representation(self, value, context=None):
        """ Recursively call get_api_representation on children and return as a plain dict """
        dict_list = []
        for item in value["Avengers"]:
            temp_dict = {
                'title': item.get("title"),
                'text': item.get("text"),
                'image_url': item.get("image").file.url
                # any other relevant fields of your model...
            }
            dict_list.append(temp_dict)

        return dict_list

Run Code Online (Sandbox Code Playgroud)