is it possible to get httpreuest info in DatabaseWrapper ?

I am writing a customized DatabaseWrapper as DB engine for Databases.

class MyDatabaseWrapper(DatabaseWrapper):
def __init__(self, *args, **kwargs):
        super(DatabaseWrapper, self).__init__(*args, **kwargs)


DATABASES = {"default": {
    'ENGINE': 'lib.MyDatabaseWrapper',
    'HOST': '***',
    'CONN_MAX_AGE': 600,

now I need to get the user’s email in MyDatabaseWrapper class. is it possible to retrieve httprequest object in the ? is there any good suggestion ?

I think this is a good example of the XY problem - why are you trying to do that?

(Perhaps custom database instrumentation would fit your needs)

thanks for the reply

because we have several database behind django. each of them has its own connetion pool (which is stored in
when user send a request to view to get data from db. I need to get his email first, then I will return him a db connection from MyDatabaseWrapper according to his email address. the view can use this connection to get data fro db.

If you have several databases, the right way to connect to them is to use the multi-database features and have them all defined in DATABASES and use a custom database router to route based on the user.

If you’re looking at one-database-per-user, try django-tenants.

the databases are created dynamically (like when user first login), so I cannot define them in DATABASES in
is there a good way to add db define dynamically ?

I’m going to go with the most common answer I’ve seen when investigating this type of situation.

Choose a different framework.

I’ve never seen anything giving me any impression that Django is designed or engineered to do anything like that.

While it may be possible to engineer something that usually works with the current version, there are no public APIs for adding database connections in the running system. Some of the answers I’ve seen involve altering a secondary settings file that is read by the primary file at start up, and then restarting the app.

Keep in mind that in a production-style environment, you would typically have multiple instances of your application running and serving responses - and those individual instances could possibly be restarted at just about any time. What this means to your app is that you would need to have some way to establish this connection across all instances - and make that change persistent across restarts - every time a new database is created.

(I’d love to be shown that I’m wrong here. It would be a neat feature - I’d have some uses for it myself.)


Ken may be right but a couple points…

  1. Check the source of django-tenants. That creates arbitrary database connections and runtime and routes Django’s ORM to them. it’s definitely possible.
  2. I’d again question: WHY are you creating databases per user? That becomes very tricky to manage at scale with e.g. migrations, multiple web servers as Ken says, etc.

actually not db per user but per user’s email address.
the same email address will use the same database

I will take a look at django-tenants. thanks

Just want to confirm - is django-tenants the project you’re referring to?

If so, then one of us is misunderstanding what it does. None of the documentation that I’m finding refers to dynamically making a database connection. What I’m reading is that it will dynamically change the PostgreSQL schema being used, but still using the same database connection.

Am I the one missing something here?

Okay - I was a little confused. MySQL’s databases are PostgreSQL’s schemas, and for some reason I though django-tenants needed to create a new connection to change schema.

Yea, it happens, especially when different databases have different semantics around the concept of a “schema”, and some databases (<cough>, DB2, <cough>) allow for a 3-tier reference to a table as “database.schema.table” and define a higher layer of organization that can be thought of as “database engine instance”. It’s all so much fun!

Ugh, databases

Anyway back to:

I still feel this didn’t answer the question - why do you want to have one db per user’s email address? How many users will you have?