如何更改 Wagtail 通过 API 为媒体提供服务的方式?
How do I change how Wagtail serves media over it's API?
我目前正在尝试创建一个 Web 应用程序,该应用程序使用 Django + Wagtail 作为后端内容,并使用 React 作为前端。截至目前,在页面加载时,我通过 http GET 请求从 Wagtail 请求所有 'articles'。然后我在我的前端组件中显示这些数据。
除我遇到的一个问题外,此方法运行良好,即文章正文中的媒体表示为具有本地来源的 <embed />
标记。我想要的是一个 <img />
标签,其中 src 指向存储图像的 url。我怎样才能在后端改变它?我似乎找不到关于此问题的任何类型的文档。
这是我的获取请求响应当前的样子:
{
"id": 9,
...
"title": "Child page",
"date": null,
"body": "<p>Here is a test image:</p><embed alt=\"testimg\" embedtype=\"image\" format=\"fullwidth\" id=\"5\"/><p></p>",
"author": "Isaac"
}
这是我想要的样子:
{
"id": 9,
...
"title": "Child page",
"date": null,
"body": "<p>Here is a test image:</p><img src="image-location-url"/><p></p>",
"author": "Isaac"
}
我该怎么办?这可以通过 Wagtail 设置配置来控制吗?或者我应该以某种方式改变我的内容模型吗?谢谢。
直接从 Github 页面 (https://github.com/wagtail/wagtail/issues/2695#issuecomment-373002412) 上的评论复制:
from wagtail.wagtailcore.rich_text import expand_db_html
class APIRichTextField(APIField):
def __init__(self, name):
serializer = serializers.APIRichTextSerializer()
super().__init__(name=name, serializer=serializer)
class APIRichTextSerializer(fields.CharField):
def to_representation(self, instance):
representation = super().to_representation(instance)
return expand_db_html(representation)
class MyModel(Page):
body = RichTextField()
api_fields = [
APIRichTextField('body'),
]
现在我的 body 被直接转换成 html 将显示在 Wagtail 的一端。感谢@solarissmoke 的指导。如果此 answer/question 不符合准则,请告诉我,我会很乐意将其删除。
我目前正在尝试创建一个 Web 应用程序,该应用程序使用 Django + Wagtail 作为后端内容,并使用 React 作为前端。截至目前,在页面加载时,我通过 http GET 请求从 Wagtail 请求所有 'articles'。然后我在我的前端组件中显示这些数据。
除我遇到的一个问题外,此方法运行良好,即文章正文中的媒体表示为具有本地来源的 <embed />
标记。我想要的是一个 <img />
标签,其中 src 指向存储图像的 url。我怎样才能在后端改变它?我似乎找不到关于此问题的任何类型的文档。
这是我的获取请求响应当前的样子:
{
"id": 9,
...
"title": "Child page",
"date": null,
"body": "<p>Here is a test image:</p><embed alt=\"testimg\" embedtype=\"image\" format=\"fullwidth\" id=\"5\"/><p></p>",
"author": "Isaac"
}
这是我想要的样子:
{
"id": 9,
...
"title": "Child page",
"date": null,
"body": "<p>Here is a test image:</p><img src="image-location-url"/><p></p>",
"author": "Isaac"
}
我该怎么办?这可以通过 Wagtail 设置配置来控制吗?或者我应该以某种方式改变我的内容模型吗?谢谢。
直接从 Github 页面 (https://github.com/wagtail/wagtail/issues/2695#issuecomment-373002412) 上的评论复制:
from wagtail.wagtailcore.rich_text import expand_db_html
class APIRichTextField(APIField):
def __init__(self, name):
serializer = serializers.APIRichTextSerializer()
super().__init__(name=name, serializer=serializer)
class APIRichTextSerializer(fields.CharField):
def to_representation(self, instance):
representation = super().to_representation(instance)
return expand_db_html(representation)
class MyModel(Page):
body = RichTextField()
api_fields = [
APIRichTextField('body'),
]
现在我的 body 被直接转换成 html 将显示在 Wagtail 的一端。感谢@solarissmoke 的指导。如果此 answer/question 不符合准则,请告诉我,我会很乐意将其删除。