2

Is it possible to change active database dynamically in Django?
For example, use one DB for inserts and updates and switch to other for readonly operations.

1

2 Answers 2

7

This is possible by configuring multiple databases in your settings and then using a router to specify which database configuration should be used for read and write.

Go to https://docs.djangoproject.com/en/1.3/topics/db/multi-db/#database-routers and look for "MasterSlaveRouter", which has example code for exactly what you are requesting.

Sign up to request clarification or add additional context in comments.

Comments

1

It's also possible to manually select databases in queries and saves as explain in the Django Docs on multiple databases

Essentially one uses the "using" keyword argument, as in

obj.save(using='alias')

and the "using" method of the QuerySet like this:

Model.objects.using('alias').all()

alias is the name given to the database in the DATABASES item of the settings.

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.