Switching database backends at runtime?

140 views
Skip to first unread message

Oliver Beattie

unread,
Mar 16, 2009, 6:20:39 PM3/16/09
to Django users
Does anyone know if it's possible to switch the database backend in
Django at runtime (let's say I want to use dummy for something, but
then switch it back to the original after I'm done).

Is this possible using django.db.load_backend or something (I'm
doubting it as I see there is some module-level code in there that
takes info directly from settings).

I know it's a bit of a cardinal sin, but would altering settings at
runtime have any effect? Again probably no since there will still be
things pointing to the old backend instance, but worth a shot.

Alex Gaynor

unread,
Mar 16, 2009, 6:25:25 PM3/16/09
to django...@googlegroups.com
It sort of depends on what you're trying to do, if you want to work with a queryset using a different database you can do something like this: http://www.eflorenzano.com/blog/post/easy-multi-database-support-django/ except for if you're using django trunk you can use django.db.load_backend and the new DatabaseWrapper constructor(which takes a dict of settings) to do it, instead of futzing with the global settings.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Oliver Beattie

unread,
Mar 17, 2009, 8:01:48 AM3/17/09
to Django users
Thanks for your quick reply :)

Basically, what I want to do has to apply to every query made to the
database, not just one query. Basically, I want to do a dry-run of
some things before doing them. Since MySQL doesn't support schema
alterations inside a transaction, I can't do it that way.

I'm sadly bound to using the release version of Django… so will this
apply?

On Mar 16, 10:25 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:
> On Mon, Mar 16, 2009 at 6:20 PM, Oliver Beattie <oli...@obeattie.com> wrote:
>
> > Does anyone know if it's possible to switch the database backend in
> > Django at runtime (let's say I want to use dummy for something, but
> > then switch it back to the original after I'm done).
>
> > Is this possible using django.db.load_backend or something (I'm
> > doubting it as I see there is some module-level code in there that
> > takes info directly from settings).
>
> > I know it's a bit of a cardinal sin, but would altering settings at
> > runtime have any effect? Again probably no since there will still be
> > things pointing to the old backend instance, but worth a shot.
>
> It sort of depends on what you're trying to do, if you want to work with a
> queryset using a different database you can do something like this:http://www.eflorenzano.com/blog/post/easy-multi-database-support-djan...

Oliver Beattie

unread,
Mar 17, 2009, 7:08:20 PM3/17/09
to Django users
Nope, seems this is actually impossible (at least using manage.py
shell), since it will load all of the models and hold references to
them on initialization until termination, so the settings-definte
backend is tied to BaseQuery forever :(
Reply all
Reply to author
Forward
0 new messages