feat(fields): enhance encryption in JSONField with direct processing
Replace parent method call in `get_prep_value` with direct encryption logic to prevent errors caused by incorrect JSON parsing. This ensures the proper handling of JSON strings and avoids reliance on parent class behavior.
This commit is contained in:
parent
f1d74cada0
commit
f9d479c37c
1 changed files with 10 additions and 2 deletions
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Reference in a new issue