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