【问题标题】:Wagtail API - show image URL on json outputWagtail API - 在 json 输出上显示图像 URL
【发布时间】:2017-06-16 17:18:09
【问题描述】:

对 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
}

谢谢

【问题讨论】:

  • 能否请您展示您的 DemoImage 模型和 json 输出?
  • 已在上面进行了编辑 - 请原谅格式错误......

标签: django wagtail


【解决方案1】:

从 Wagtail 1.10 开始,您可以在页面的 api_fields 定义中使用 ImageRenditionField 来包含以您选择的大小呈现的图像的 URL:

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')),
    ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    相关资源
    最近更新 更多