I try to make an API Rest Application in Django and I try to make several domains (example1.com, example2.com, example3.com) at the time of making requests are directed with the database of each one.
Each domain has its database in postgresql.
Django allows to use Multi Databases and to be able to use that strategy there are 2 ways:
1.- At the moment of making a query in the ORM
Articulos.object.using('database1').get(id=id) #por ejemplo
2.- Create a file called routers.py, where a class is defined by overwriting some methods
class DatabaseRouter(object):
def db_for_read(self, model, **hints):
request = HttpRequest()
if request.META.get('HTTP_HOST) == 'domain1':
return 'database1'
if request.META.get('HTTP_HOST) == 'domain2':
return 'database2'
return None
def db_for_write(self, model, **hints):
request = HttpRequest()
if request.META.get('HTTP_HOST) == 'domain1':
return 'database1'
if request.META.get('HTTP_HOST) == 'domain2':
return 'database2'
return None
def allow_syncdb(self, db, model):
if db == 'database1' or db == 'database2':
return model._meta.app_label == 'app'
elif model._meta.app_label == 'app':
return False
return None
that class is integrated into Settings.py
DATABASE_ROUTERS = ['APIapp.routers.DatabaseRouter',]
So far the information is good.
Now, the question is: How can I make it so that when I enter the domain, routers.py knows which domain and therefore what database to choose?
According to what I have read, doing it through a middleware but I could not, then I think of recovering it in routers.py the domain. The Django views contain a request argument where it includes the hostname, but I could not retrieve that Django object and send it to routers.py
What can I do?