2

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?

4
  • 1
    What is your motivation for using a second database? Commented Nov 14, 2011 at 23:22
  • We have a dev and live server. The FieldMap data needs to be kept identical on both Commented Nov 14, 2011 at 23:31
  • Perhaps you should have asked: What is the best way to keep my development database in sync with my production database? Commented Nov 14, 2011 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. Commented Nov 14, 2011 at 23:58

1 Answer 1

3

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

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

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.