Reputation: 19
I am using Django rest framework. The CkEditor tags I've used for the web appear in the API part. How do I remove these tags from the API
I had not used Ckeditor before, I was using the Django TextField field
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',...)
output(Json Data)
"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
Upvotes: 1
Views: 1963
Reputation: 5958
You can try overriding to_representation
method in your serializer class and use 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
Upvotes: 4