LeeHanYeong / django-quill-editor

django-quill-editor makes Quill.js easy to use on Django Forms and admin sites
https://django-quill-editor.readthedocs.io
MIT License
202 stars 49 forks source link

I can't get content of the object, but get this: <django_quill.fields.FieldQuill object at 0x000001A88692ACE0> #81

Open Hiradoras opened 2 years ago

Hiradoras commented 2 years ago

I'm trying to access my Post model's content, which is QuillField(). in views.py I have: post_words = post.content But content's type is QuillField, so I get this <django_quill.fields.FieldQuill object at 0x000001958CAD5C30> Is there any way to convert this to plain text in views, so I can count how many words in post?

shahriar350 commented 2 years ago

me also fetch this type of problem

ktunprasert commented 2 years ago

I had the same problem so I dug through the code and it seems you can serialize it by calling the property html and plain (there's also delta but I'm not sure what it does). See their code at django_quill.fields class FieldQuill.

Here's my working DRF serializer

class ConsentSerializer(serializers.ModelSerializer):
    html = serializers.SerializerMethodField()
    plain = serializers.SerializerMethodField()

    def get_html(self, instance):
        return str(instance.body.html)

    def get_plain(self, instance):
        return str(instance.body.plain)

    class Meta:
        model = Consent
        fields = ["id", "name", "html", "plain"]
Harsh-2503 commented 1 year ago

The above didn't worked for me but this did

class FieldQuillSerializer(serializers.Field):
    def to_representation(self, value):
        # Assuming `value` is an instance of your FieldQuill class
        return {
            'json_string': value.json_string,
            'html': value.html,
            'delta': value.delta,
            'plain': value.plain,
        }

    def to_internal_value(self, data):
        pass

class ContentSerializer(serializers.ModelSerializer):
    heading = FieldQuillSerializer()
    description = FieldQuillSerializer()

    class Meta:
        model = Content
        fields = ["heading","description"]