Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

implementing custom backend is not recognized by django

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:

  1. created a backends folder in my app directory:
    dir structure

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

  2. 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
    
  3. changed settings.py accordingly (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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading