[Python] Django Custom Database Backend

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use a third party database backend and to understand the topic I followed the approach described under this link: https://python.plainenglish.io/how-...mprehensive-guide-for-developers-c8fb7e6a49ed So I used a docker instance of python:latest (3.13.0) and installed django (5.1.13).

    I followed the article


    Complete Real-World Demo: Creating and Using a Custom Django Database Backend
    ​

    in the article above. When I reached the migration part, I received the error message:

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


    So I changed the settings to:

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


    I still get an error:


    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

    The structure of the project is shown here: [​IMG]

    Continue reading...

Compartilhe esta Página