On Thu, May 13, 2010 at 10:27 PM, GRoby <gregory.r...@gmail.com> wrote:
> Hello,
>
> I am using Django 1.2 RC 1 and have setup multiple databases.  I have
> everything working if I manually specify .using, for example:
>
> SomeObjects = ModelInDefaultDatabase.objects.all()  #This Works
>
> SomeObjects = ModelInDatabase2Name.objects.using('Database2').all()
> #This also Works
>
>
> How can I make it so my models that are in Database 2 automatically
> default to that database, so I do not have to use "using" on every
> query?  I tried adding "using" to the model and to Meta but that did
> not work, so I assume I have to setup a database router, but I do not
> understand how to apply the documentation example to what I assume is
> this simple use case for multiple databases [all models should read/
> write to the default database except for the models that ones are
> defined to exist in other databases].
>
> Anyone have a simple example of how to do this (or a link to one)?

You're correct that the solution here is to use a router.

The example presented in the docs [1] is actually pretty close to what
you will need. In the documented example MyAppRouter, any model in
myapp will be directed to the 'other' database. If you want to narrow
this so that only a specific model (say, myapp.ModelInDatabase2Name)
is directed to 'other', then just replace the if statements that say:

if model._meta.app_label == 'myapp':

with

if model._meta.app_label == 'myapp' and model._meta.object_name ==
'ModelInDatabase2Name':

That is - instead of just checking the app, check the app and the
model name. Install the router, and any query involving
ModelInDatabase2Name will be directed to the 'other' database.

[1] http://docs.djangoproject.com/en/dev/topics/db/multi-db/#an-example

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to