DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
...
DBMS: PostgreSQL (Version 14.4 (Ubuntu 14.4-1.pgdg20.04+1))
Case sensitive: normal form=lower, delimited form=exact
Driver: PostgreSQL JDBC Driver (version 42.2.5, JDBC4.2)
postgresql jsonfield save data like it
{“a”: “b”}
jango default jsonField get data is dict
This is how I currently solve it.
class CustomJsonField(models.JSONField):
def from_db_value(self, value, expression, connection):
if isinstance(value, dict):
return value
return super().from_db_value(value, expression, connection)
Is it possible to consider solving it this way?
if isinstance(expression, KeyTransform) and not isinstance(value, str):
return value
try:
# PostgreSQL database save json return dict
+ if isinstance(value, dict):
+ return value
return json.loads(value, cls=self.decoder)
except json.JSONDecodeError:
return value