django无法识别自定义后端的实现

fykwrbwg  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(122)

根据一位djangosMaven通常聪明的answer,我想实现我自己的后端,覆盖SQLite3和Postgres JSON处理方法。
我所做的:
1.在我的应用程序目录中创建了一个backends文件夹:

的数据
1.把base.py放进去

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

字符串
1.修改了settings.py(所以我想):

DATABASES = {
    'default': {
        'ENGINE': "Shelf.backends.sqlite3json",
        'NAME': BASE_DIR.joinpath("db.sqlite3"),
    }
}


这会抛出错误:

NotImplementedError: subclasses of BaseDatabaseOperations may require a quote_name() method

qlckcl4x

qlckcl4x1#

我想你已经很接近了,你应该只子类sqlite3的操作类,因为它已经包含了如何引用名字的逻辑,等等:

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

字符串

相关问题