Implementing a Custom Database Backend in Django

I’m working on setting up a custom database backend for my Django project. I’m using Python 3.13.0 and Django 5.1.13 in a Docker container.

When I try to run migrations, I get this error:

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

I tried changing my database settings to:

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

But now I’m getting a different error:

TypeError: 'NoneType' object is not callable

This happens when Django tries to set up the database connection. My project structure looks like a typical Django setup with a custom_backend folder at the root level.

What am I doing wrong? How can I properly set up my custom database backend?

hey lucaspixel23, i’m curious abt your custom backend. did u check if the ‘custom_backend’ module is in your PYTHONPATH?

how’s your backend structured? maybe there’s a config issue. what other errors have u seen?

yo lucaspixel23, sounds like a tricky situation. have u tried adding ‘init.py’ files to ur custom_backend folders? that might help with the package issue. for the NoneType error, double-check ur backend’s connection method. it might be returning None somewhere. debuggin’s gonna be ur best friend here, mate.

Implementing a custom database backend in Django is a complex task. From your error messages, it appears there might be issues with your backend’s structure and implementation. Ensure your custom_backend folder contains all necessary files, including ‘init.py’, ‘base.py’, and any required database-specific modules. The ‘ENGINE’ setting should point to the full path of your backend class, not just the folder. For example: ‘ENGINE’: ‘custom_backend.base.DatabaseWrapper’. Additionally, verify that your custom backend implements all required methods and attributes of Django’s database API. Consider studying Django’s built-in database backends as reference for proper implementation structure.