Django 自定义数据库后端

问题描述 投票:0回答:1

我正在尝试使用第三方数据库后端并理解我遵循此链接下描述的方法的主题:https://python.plainenglish.io/how-to-create-custom-django-database-backends-开发人员综合指南-c8fb7e6a49ed 所以我使用了 python:latest (3.13.0) 的 docker 实例并安装了 django (5.1.13)。

我关注了这篇文章

完整的实际演示:创建和使用自定义 Django 数据库 后端

在上面的文章中。当我到达迁移部分时,我收到错误消息:

ModuleNotFoundError: No module named 'custom_backend.base.base'; 'custom_backend.base' is not a package

所以我将设置更改为:

DATABASES = {
    'default': {
        'ENGINE': 'custom_backend',  # .base removed
        'NAME': 'my_custom_db',
        'USER': 'custom_user',
        'PASSWORD': 'custom_password',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

我仍然收到错误:

File "/project/custom_backend_project/manage.py", line 22, in <module>
    main()
    ~~~~^^
  File "/project/custom_backend_project/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
    ~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/django/core/management/__init__.py", line 416, in execute
    django.setup()
    ~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/django/apps/registry.py", line 116, in populate
    app_config.import_models()
    ~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/django/apps/config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
                         ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/importlib/__init__.py", line 88, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 1022, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/usr/local/lib/python3.13/site-packages/django/contrib/auth/models.py", line 5, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/usr/local/lib/python3.13/site-packages/django/contrib/auth/base_user.py", line 40, in <module>
    class AbstractBaseUser(models.Model):
    ...<123 lines>...
            )
  File "/usr/local/lib/python3.13/site-packages/django/db/models/base.py", line 143, in __new__
    new_class.add_to_class("_meta", Options(meta, app_label))
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/django/db/models/base.py", line 371, in add_to_class
    value.contribute_to_class(cls, name)
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/django/db/models/options.py", line 231, in contribute_to_class
    self.db_table, connection.ops.max_name_length()
                   ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
                   ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/usr/local/lib/python3.13/site-packages/django/db/utils.py", line 194, in create_connection
    return backend.DatabaseWrapper(db, alias)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/django/db/backends/base/base.py", line 114, in __init__
    self.client = self.client_class(self)
                  ~~~~~~~~~~~~~~~~~^^^^^^
TypeError: 'NoneType' object is not callable

python django database
1个回答
0
投票

让我们看看,问题似乎出在您的自定义后端的结构上。检查

custom_backend
及其子文件夹是否有
__init__.py
文件以使它们成为正确的 Python 包。在您的数据库配置中,如果
'ENGINE': 'custom_backend.base'
位于
DatabaseWrapper
中,则设置
base.py

此外,确保

DatabaseWrapper
正确定义所需的属性(如
client_class
)和方法(如
get_new_connection
)。测试导入您的后端
(from custom_backend.base import DatabaseWrapper)
以发现任何路径问题。

有关详细信息,https://docs.djangoproject.com/en/5.1/howto/custom-db-backend/。让我知道它是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.