1

I'm hoping this will be similar to a few questions which have been previously answered.

I was wondering what the full processes are for testing the DB connection string (not the DB name) for the current connected DB in Django is? I see a few methods for printing the name, but across local development and staging, these names will be the same. I need to know more, such as the db engine (e.g., is it PostgreSQL, or is it db.sqlite3. etc etc), and various other DB parameters.

I'm currently trying to debug why a Celery beat task is failing to create database entries for the application DB I think I am connected to.

I'm performing a simple obj, created = Object.objects.get_or_create() DB method, and on logging created, some are True and some are False. All good so far.

However, the admin section of the Django CMS is not showing any more entries to the DB.

I believe testing the location and connection strings of the DB the Django application is using might be useful in debugging this ghost object creation...unless someone can advise on similar issues they have had with the Celery daemon not actually persisting a DB create/update etc...

Perhaps, something such as:

from django.db import connection
print(connection.settings_dict)

Would be sufficient?

Micheal J. Roberts
  • 3,735
  • 4
  • 37
  • 76

1 Answers1

2

So, the following was useful for determining the DB engine:

from django.db import connection
print(connection.settings_dict)

For useful guidance, the connection.settings_dict contains the following structure:

{
    'ENGINE': 'django.db.backends.sqlite3', 
    'NAME': '/usr/src/app/db.sqlite3', 
    'ATOMIC_REQUESTS': False, 
    'AUTOCOMMIT': True, 
    'CONN_MAX_AGE': 0, 
    'OPTIONS': {}, 
    'TIME_ZONE': None, 
    'USER': '', 
    'PASSWORD': '', 
    'HOST': '', 
    'PORT': '', 
    'TEST': {
        'CHARSET': None, 
        'COLLATION': None, 
        'NAME': None, 
        'MIRROR': None
    }
}

So, we could work with something like this in a management command:

from django.db import connection
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    help = 'Displays The Currently Connected DB Settings'

    def handle(self, *args, **kwargs):
        print(
            "The current database engine is {ENGINE}".format(**connection.settings_dict)
        )

        print(
            "Currently connected to host {HOST} on port {PORT} ".format(**connection.settings_dict)
        )
Micheal J. Roberts
  • 3,735
  • 4
  • 37
  • 76