diff --git a/schon/fields.py b/schon/fields.py index c98e9d2d..2dd7a482 100644 --- a/schon/fields.py +++ b/schon/fields.py @@ -21,9 +21,17 @@ class EncryptedJSONTextField(EncryptedTextField): return super().formfield(**{"form_class": forms.JSONField, **kwargs}) def get_prep_value(self, value): - if value is not None and not isinstance(value, str): + # Encrypt directly instead of delegating to super().get_prep_value(). + # The parent chain (EncryptedFieldMixin → CharField) calls + # self.to_python() which parses the JSON string back to a dict, + # then str(dict) produces Python repr with single quotes — not JSON. + if value is None: + return None + if not isinstance(value, str): value = orjson.dumps(value, default=str).decode("utf-8") - return super().get_prep_value(value) + if not value: + return None + return self.f.encrypt(value.encode("utf-8")).decode("utf-8") def from_db_value(self, value, expression, connection): if value is None: