Wagtail API - 在json输出上显示图像URL

时间:2017-06-16 17:18:10

标签: django wagtail

Wagtail的新手 - 我目前正在为我的React应用程序创建Wagtail API。已经成功安装并获得了json输出,但没有获取在Wagtail管理面板中上传的图像的URL。我在网上搜索过,但没有多少快乐。

这是我创建的基本主页模型

class BarsHomePage(Previewable, Themable, Page):

    bars_site_homepage_test = models.CharField(max_length=255, blank=True)
    feed_image = models.ForeignKey(
        'DemoImage',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )



    api_fields = ['bars_site_homepage_test','feed_image']

class DemoImage(Image):
    @property
    def fullwidth_url(self):
        return generate_image_url(self, 'width-800')

    @property
    def halfwidth_url(self):
        return generate_image_url(self, 'width-400')

    api_fields = (
        'fullwidth_url',
        'halfwidth_url',
    )

    class Meta:
        proxy = True

Json输出

{
    "id": 504,
    "meta": {
        "type": "wagtailimages.Image",
        "detail_url": "http://www.lv.local/api/v1/images/504/"
    },
    "title": "Lighthouse.jpg",
    "tags": [],
    "width": 1365,
    "height": 2048
}

由于

1 个答案:

答案 0 :(得分:1)

从Wagtail 1.10开始,您可以在页面的api_fields定义中使用ImageRenditionField来包含图片的网址,以您选择的尺寸呈现:

from wagtail.api import APIField
from wagtail.wagtailimages.api.fields import ImageRenditionField

class BarsHomePage(Previewable, Themable, Page):
    # ...

    api_fields = [
        APIField('bars_site_homepage_test'),
        APIField('feed_image_fullwidth', serializer=ImageRenditionField('width-800', source='feed_image')),
    ]