Dynamic database selection based on URL in Django
<p>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 <code>A</code> and <code>B</code>. If link for <code>A</code> is clicked, then database for <code>A</code> is used which contains the students for college <code>A</code>. The entire application after this point should use database for college <code>A</code>.</p>

<p>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. </p>

<p>So you need to store the chosen database in <code>session</code> or smth and you can easily pick the database. From the <a href="https://docs.djangoproject.com/en/1.10/topics/db/multi-db/#manually-selecting-a-database-for-a-queryset" rel="nofollow">docs</a></p>

<pre><code>&gt;&gt;&gt; # This will run on the 'default' database.
&gt;&gt;&gt; Author.objects.all()

&gt;&gt;&gt; # So will this.
&gt;&gt;&gt; Author.objects.using('default').all()

&gt;&gt;&gt; # This will run on the 'other' database.
&gt;&gt;&gt; Author.objects.using('other').all()
</code></pre>

python django django-models