如何不转义 Django Rest Framework 中的字符

How to not escape characters in Django Rest Framework

我有一个博客模型。我有一个要在 HTML 中写入的文本字段,例如包含一个 a 标记。用于不安全方法的权限 class 是 isAdminUser。我不一定要逃避角色。有什么办法可以 not escape 字符。

这段代码很有帮助。我基本上不得不重写 to_representation.

def to_representation(self, instance):
    ret = super().to_representation(instance)
    ret['text'] = html.unescape(ret['text']) 
    return ret