From 1dbd719db8aaf52d242ac0d00f1cf73e7f742b1d Mon Sep 17 00:00:00 2001 From: proxi <51172302+void-witch@users.noreply.github.com> Date: Tue, 9 Feb 2021 03:03:58 -0500 Subject: [PATCH] rewrite QuillField to inherit JSONField (#27) this allows the db backend to apply any optimizations it might want/need closes #26 Co-authored-by: proxi <51172302+3n-k1@users.noreply.github.com> --- django_quill/fields.py | 4 +++- django_quill/forms.py | 5 ++++- django_quill/quill.py | 7 +++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/django_quill/fields.py b/django_quill/fields.py index 8f987aa..baf9ef8 100644 --- a/django_quill/fields.py +++ b/django_quill/fields.py @@ -1,3 +1,5 @@ +import json + from django.db import models from .forms import QuillFormField @@ -31,7 +33,7 @@ def _require_quill(self): def _get_quill(self): self._require_quill() - self._quill = Quill(self.json_string) + self._quill = Quill(json.loads(self.json_string)) return self._quill def _set_quill(self, quill): diff --git a/django_quill/forms.py b/django_quill/forms.py index 6df7f9f..8799e60 100644 --- a/django_quill/forms.py +++ b/django_quill/forms.py @@ -6,9 +6,12 @@ ) -class QuillFormField(forms.fields.CharField): +class QuillFormField(forms.fields.JSONField): def __init__(self, *args, **kwargs): kwargs.update({ 'widget': QuillWidget(), }) super().__init__(*args, **kwargs) + + def prepare_value(self, value): + return value.json_string diff --git a/django_quill/quill.py b/django_quill/quill.py index 5c40ae6..7457f74 100644 --- a/django_quill/quill.py +++ b/django_quill/quill.py @@ -1,5 +1,4 @@ import json -from json import JSONDecodeError __all__ = ( 'QuillParseError', @@ -18,9 +17,9 @@ def __str__(self): class Quill: def __init__(self, json_string): try: - self.json_string = json_string - json_data = json.loads(json_string) + self.json_string = json.dumps(json_string) + json_data = json_string self.delta = json_data['delta'] self.html = json_data['html'] - except (JSONDecodeError, KeyError, TypeError): + except (json.JSONDecodeError, KeyError, TypeError): raise QuillParseError(json_string)