ImproperlyConfigured("settings.DATABASES is improperly configured.

django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

this exception is raised only when serializer.save() method is called and the multiple database configuration is used

We’re going to need a lot more detail about this issue.

At a minimum, we’re going to need to see your DATABASES section of your settings file (be sure to remove or obfuscate the password). We’ll probably also need to see the view that is throwing this error, along with the complete traceback from the error.

When posting code here, copy/paste the code into the body of your post, surrounded by lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted, which is critical with Python. (Note, you should also surround the traceback this way.)

Traceback (most recent call last):
File “/home/oem/workspace/kkem_venv/lib/python3.8/site-packages/rest_framework/views.py”, line 506, in dispatch
response = handler(request, *args, **kwargs)
File “/home/oem/workspace/zappyhire_jobfair_project/zappyhire/jobs/views.py”, line 194, in post
job = serializer.save()
File “/home/oem/workspace/kkem_venv/lib/python3.8/site-packages/rest_framework/serializers.py”, line 205, in save
self.instance = self.create(validated_data)
File “/usr/lib/python3.8/contextlib.py”, line 74, in inner
with self._recreate_cm():
File “/home/oem/workspace/kkem_venv/lib/python3.8/site-packages/django/db/transaction.py”, line 175, in enter
if not connection.get_autocommit():
File “/home/oem/workspace/kkem_venv/lib/python3.8/site-packages/django/db/backends/base/base.py”, line 389, in get_autocommit
self.ensure_connection()
File “/home/oem/workspace/kkem_venv/lib/python3.8/site-packages/django/db/backends/dummy/base.py”, line 20, in complain
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

Thanks for the traceback, but we still need to see:

Also:

[emphasis added]

DATABASE_ROUTERS = [
    'myproject.db_router.PrimaryRouter'
]

DATABASES = {
    'default': {},
    'read_db': {
        'NAME': os.getenv("DATABASE"),
        'ENGINE':  os.getenv("ENGINE"),
        'USER': os.getenv("DATABASE_USER"),
        'PASSWORD': os.getenv("DATABASE_PASSWORD"),
        'HOST': os.getenv("READ_DATABASE_HOST"),
        'PORT': '5432',
    },
    'write_db': {
        'NAME': os.getenv("DATABASE"),
        'ENGINE': os.getenv("ENGINE"),
        'USER': os.getenv("DATABASE_USER"),
        'PASSWORD': os.getenv("DATABASE_PASSWORD"),
        'HOST': os.getenv("WRITE_DATABASE_HOST"),
        'PORT': '5432',
    }
}

this is my router

class PrimaryRouter:
    """
    A router to control read and write database operations.
    """

    def db_for_read(self, model, **hints):
        """
        Read actions go to read_db.
        """
        return 'read_db'


    def db_for_write(self, model, **hints):
        """
        write actions go to write_db.
        """
        return 'write_db'

    def allow_relation(self, obj1, obj2, **hints):
        """
        Relations between objects are allowed if both objects are
        in the primary/replica pool.
        """
        db_set = {'write_db', 'read_db'}
        if obj1._state.db in db_set and obj2._state.db in db_set:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        All models end up in this pool.
        """
        return True

engine value provided

ENGINE="django.db.backends.postgresql"

You must have a “default” database - Django itself requires it.

See DATABASES

2 Likes