Django 如何在streamfield创建图像的API页面中获取图像url或下载图像url?

Django 如何在streamfield创建图像的API页面中获取图像url或下载图像url?,django,django-rest-framework,wagtail,Django,Django Rest Framework,Wagtail,在我的wagtail应用程序中,我有一个streamfield,用于使用ImageChooserBlock以及标题和文本上传图像。这意味着在单个streamfield中,我有一个标题、一个文本和一个图像上传输入。我试图在rest框架的PagesAPI中获取图像url(localhost:8000/API/v2/pages/[page id])。但是这个页面api只提供上传图像的图像id,如下所示 { "type": "avengers", "value": { "

在我的wagtail应用程序中,我有一个streamfield,用于使用ImageChooserBlock以及标题和文本上传图像。这意味着在单个streamfield中,我有一个标题、一个文本和一个图像上传输入。我试图在rest框架的PagesAPI中获取图像url(
localhost:8000/API/v2/pages/[page id]
)。但是这个页面api只提供上传图像的图像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"
} 
{
    "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
}
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"
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"   
如果我访问图像api(
http://localhost:8000/api/v2/images/1/
)我得到了如下
下载url

{
    "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"
} 
{
    "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
}
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"
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"   
我的问题是如何在pages API(
localhost:8000/API/v2/pages/[page id]
)中获取
下载\u url
或图像url

复仇者块的streamfields blocks.py如下所示

{
    "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"
} 
{
    "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
}
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"
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"   
此流字段在content types model.py中使用,如下所示

{
    "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"
} 
{
    "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
}
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"
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"   

将此添加到AvengersBlock中,并在

/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