5

I need to be able to determine the DB type used during run time in Django.

MYSQL = False
if <something goes here>:
    MYSQL = True

I saw a related Django ticket, but didn't quite get me to my solution. (https://code.djangoproject.com/ticket/18332)

It'd be nice to have a solution that I can determine if it's a MySQL, PostgreSQL, SQLite, Oracle...whatever.

I've tried digging through django.db but can't see anything that will help.

I'll elaborate on what I'm trying to do as it seems misunderstood. This is for a testing utility project that is independent of any Django project. My utility needs to understand which database is being used to prevent illegal activity.

For example, my package supports PostgreSQL hstore fields, but no such field exists in, say, SQLite. So, I don't want to allow specific functionality if they are using SQLite.

I have access to their Django model used with the utility.

from my_app.models import Foo

testing_utility(Foo, **kwargs)

My testing_utility signature is:

def testing_utility(klass, **kwargs):
    instance = klass(**kwargs)

Now, I don't want to preform hstore specific functionality on this class if isn't a PostgreSQL DB. Does this clear it up some more?

EDIT 1:

Thanks, KevinDTimm, for the link. It looks like Django models do have ._state.db upon instantiation. Of course, my instance has a None type. :(

>> print(instance._state.db)
None
Rico
  • 5,692
  • 8
  • 46
  • 63
  • import settings...... – KevinDTimm Dec 12 '13 at 20:40
  • Then what? What in the settings would definitively give me the database connection? Generically, I do not know what any of the databases are named. It's `from django.conf import settings` for anyone who didn't know. – Rico Dec 12 '13 at 20:41
  • http://stackoverflow.com/questions/10101602/how-to-know-current-name-of-the-database-in-django – KevinDTimm Dec 12 '13 at 20:43
  • 1
    Would `settings.DATABASES['default']['ENGINE']` do? – Rob L Dec 12 '13 at 20:45
  • @Rob L: This would work unless they are using a custom DB that is not named 'default'. – Rico Dec 12 '13 at 20:47
  • @KevinDTimm: Thanks for the link (good to know) but still would not work because `argv` would not lead me to the specific DB connection in use at any given time. As for the other suggested solution - I do not see any connection info given in the field objects I have access to. – Rico Dec 12 '13 at 20:48
  • So iterate all the DBs, and read all the engines. – Rob L Dec 12 '13 at 20:48
  • @RobL: This would not work for my situation. I need to know which DB connection type is in use to prevent possible exceptions. If I loop through all the connection types I wouldn't know which to use. – Rico Dec 12 '13 at 20:51
  • See the 'last' answer, the one that give the name of the connection used for the data retrieval. `To see the database used to fetch a specific object you can do: object._state.db This will give you the database key in config, such as 'default', so if you have multiple databases in config you can check the right one.` – KevinDTimm Dec 12 '13 at 20:55
  • @KevinDTimm: The Django Model objects do not have `_state` anywhere I can see. I've tried `model._meta.fields`...I've dug around and can't find `_state` anywhere. – Rico Dec 12 '13 at 21:02

1 Answers1

5

KevinDTimm lead me to the first step of this solution. Thanks!

https://stackoverflow.com/a/18343919/787716 suggests using the model's ._state.db parameter to determine the database used for the instantiation.

If, however, the database in use is settings.DATABASES['default'] then ._state.db is None.

So, I was able to solve my problem by the following:

db_name = 'default'
if instance._state.db is not None:
    db_name = instance._state.db
db_backend = settings.DATABASES[db_name]['ENGINE'].split('.')[-1]
MYSQL = db_backend == 'mysql'
Community
  • 1
  • 1
Rico
  • 5,692
  • 8
  • 46
  • 63
  • Careful - this doesn't work when testing - if you use specific settings for tests, then `settings.DATABASES`still has the non-testing DBs, it seems – farialima Dec 26 '21 at 18:36