我正在使用Django rest框架。我用于网络的CkEditor标记显示在API部分中。如何从API中删除这些标签
我以前没有使用过Ckeditor,我使用的是Django TextField字段
models.py
class Lesson(models.Model):
...
lesson_content = RichTextField(
verbose_name=_('Ders İçeriği'),
blank=True
)
...
seriaizer.py
class LessonSerializer(serializers.ModelSerializer):
class Meta:
model = Lesson
fields = (...,'lesson_content',...)
输出(Json数据)
"id":1,
"user":2,
"lesson_name":"Microprocessors","lesson_content":"/<p>Merkezi .</p>",
"lesson_notes":"<p> </p>\r\n\r\n<p>Yazıcı, R., 1998, Mikrobilgisayar Donanım ve Yazılımı, KTÜ Yayınları, Trabzon, 345 s.</p>\r\n\r\n<p>Brey, B., B., 1984, Microprocessor/Hardware Interfacing and Applications, Merrill, 414 p.</p>\r\n\r\n<p>Leventhal, L., A., 1979, Z80 Assebly Language Programming, Osborne/McGraw-Hill, 612 p.</p>\r\n\r\n<p>Uffenbeck, J., 1985, Microcomputers and Microprocessors: The 8080, 8085, and Z80 Programming, Interfacing, and Troubleshooting, Prentice-Hall, 670 p.</p>\r\n\r\n<p> </p>"
<p> & nbsp; </p> \ r \ n \ r \ n <p> I don't want the tags to appear
答案 0 :(得分:0)
您可以尝试在序列化器类中覆盖to_representation
方法,并使用strip_tags
:
from django.utils.html import strip_tags
class LessonSerializer(serializers.ModelSerializer):
class Meta:
model = Lesson
fields = (...,'lesson_content',...)
def to_representation(self, instance):
data = super().to_representation(instance)
data['lesson_content'] = strip_tags(instance.lesson_content)
return data