Following the usually smart answer from one of djangos experts I want to implement my own backend, overwriting SQLite3 and Postgres JSON handling method.
What I did:
-
put base.py in there
from django.db.backends.base.operations import BaseDatabaseOperations from django.db.backends.sqlite3.base import DatabaseWrapper as DBW import json class BaseDatabaseOperationsJSON(BaseDatabaseOperations): def adapt_json_value(self, value, encoder): print("should be called") return json.dumps(value, ensure_ascii = False, cls = encoder) class DatabaseWrapper(DBW): ops_class = BaseDatabaseOperationsJSON -
changed
settings.pyaccordingly (so I thought):DATABASES = { 'default': { 'ENGINE': "Shelf.backends.sqlite3json", 'NAME': BASE_DIR.joinpath("db.sqlite3"), } }
This throws the error:
NotImplementedError: subclasses of BaseDatabaseOperations may require a quote_name() method
>Solution :
I think you were quite close, you should only subclass the operations class of sqlite3 since it contains already logic how to quote names, etc:
import json
from django.db.backends.sqlite3.base import DatabaseWrapper as DBW
from django.db.backends.sqlite3.operations import DatabaseOperations
class BaseDatabaseOperationsJSON(DatabaseOperations):
def adapt_json_value(self, value, encoder):
return json.dumps(value, ensure_ascii=False, cls=encoder)
class DatabaseWrapper(DBW):
ops_class = BaseDatabaseOperationsJSON
