0

Let's say the first page of the app has two links. Is it possible to pick the database depending on which link is clicked? The databases both have the same models, but different data. For example, let's say the application contains students for different colleges A and B. If link for A is clicked, then database for A is used which contains the students for college A. The entire application after this point should use database for college A.

I understand there are ways to work around this problem by just designing the databases differently, i.e. having a college field, and just filtering out students with the particular college affiliation. But I am hoping to find a solution using Django to just use two different databases.

3
  • There is a page in django docs that describes having multiple databases Commented Sep 22, 2016 at 17:18
  • Nothing on that page seems helpful, .using('default') is not a realistic solution. Is there another way to pick databases dynamically? Commented Sep 22, 2016 at 17:24
  • Have you looked at db routers? Only need to figure out how to pass user selection into the router. Commented Sep 23, 2016 at 2:25

2 Answers 2

1

So you need to store the chosen database in session or smth and you can easily pick the database. From the docs

>>> # This will run on the 'default' database.
>>> Author.objects.all()

>>> # So will this.
>>> Author.objects.using('default').all()

>>> # This will run on the 'other' database.
>>> Author.objects.using('other').all()
Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, this is something along the lines of what I'm looking for, but is there an alternative to .using('default')? So basically another way to pick the database dynamically?
@Harvinder why this method doesn't satisfy you? I mean, if this is in the docs it certainly a good choice.
@Harvinder you can also use SQLAlchemy, but that another story and not compatible with django-models.
This method requires significant change to the code. If I rewrote the project this is a good choice but I was hoping for something that requires less code changes.
@Harvinder then you need to specify more precisely what is the code that you have, the way you are trying to achieve, because the .using() method is pretty neat.
0

You could mix together Database Routers with this GlobalRequestMiddleware solution to create a database router which inspects the request to figure out which database to use.

class RequestDatabaseRouter(object):
  def db_for_read(self, model, **hints):
    request = GlobalRequestMiddleware.get_current_request()
    key = self.get_database_key(request)  # Implement get_database_key
    return key

There may be some way to inject the request into **hints, which I think would be preferable over the Middleware solution, however, I'm unaware how to at the moment. You may need to make sure the GlobalRequestMiddleware is called after the AuthenticationMiddleware otherwise you may not have the user on the request to inspect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.