问题描述
我正在使用无头CMS后端,前端是JavaScript。我正在使用RichTextField,并且在API上显示的图像如下: “ ” 因此,由于其嵌入类型,因此无法在我们的页面上显示。我需要使用src将类型更改为img。我什么都没尝试,因为我什至都不知道该如何开始。 这是带有RichTextField的模型
class ValueTagMotivation(Orderable,ClusterableModel):
"""Teams motivate why a tag was picked"""
team = ParentalKey(
TeamPage,on_delete=models.CASCADE,related_name="value_tag_motivations"
)
value_tag = models.ForeignKey(ValueTag,related_name="+")
tagline = models.CharField(max_length=255)
motivation = RichTextField(
features=["bold","italic","ol","ul","link","image","embed"]
)
panels = [FieldPanel("value_tag"),FieldPanel("tagline"),FieldPanel("motivation")]
class Meta:
# a team can only pick each tag once
unique_together = (("team","value_tag"),)
解决方法
我设法弄清楚了。我放
class APIRichTextSerializer(fields.CharField):
def to_representation(self,instance):
representation = super().to_representation(instance)
return expand_db_html(representation)
在我的serializers.py中,并在我的主序列化器中将其调用
class ValueTagMotivationsSerializer(serializers.ModelSerializer):
motivation = APIRichTextSerializer()
class Meta:
model = ValueTagMotivation
fields = ['value_tag','tagline','motivation']
现在我明白了 “ ”