Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a model called FieldMap which is used throughout my project. I decided recently that the physical database for this model will exist on another machine (other than the default).

So I now have two databases setup in my settings: default and dev.

I can use both databases like this:

FieldMap.objects.using('dev/default').all()

Because my project has already been written, I dont want to have to go through code and make the changes every time I read/write from this model.

So after all that, I was wondering if there is a way to tell a model which database to read/write from by default?

share|improve this question
1  
What is your motivation for using a second database? – mikerobi Nov 14 '11 at 23:22
We have a dev and live server. The FieldMap data needs to be kept identical on both – neolaser Nov 14 '11 at 23:31
Perhaps you should have asked: What is the best way to keep my development database in sync with my production database? – mikerobi Nov 14 '11 at 23:46
I didnt want to put any more stress on the live server and figured there was no point in keeping them in sync constantly as it would be redundant. Also having researched this a little bit, I became curious as to whether this was possible. – neolaser Nov 14 '11 at 23:58

1 Answer

Consider writing a custom database router that decides the database to use on the fly.

https://docs.djangoproject.com/en/1.3/topics/db/multi-db/#database-routers

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.