[Answered ]-Django modify DATABASE_HOST at runtime

1👍

My guess, from the information, would be a potential error in your set DATABASE_HOST lines (in yor pseudo code above). read: “setattr(settings…”

Other than that, I’m not sure how you’ve configured your database to switch based on your criteria, as you’ve not explained this. If you are doing it by model, it may be worth considering how Django knows this, or even using external connections (manually loading the database driver and running commands by hand prior to the render stage), and using the main.

I’d query the whole approach, but mostly because I’m not sure how you’re actually differentiating the two databases, or why. Could you provide a bit more information on how you’re doing this? I assume the variables you’re pulling in dot-points 2 and 5 above are different. I don’t need the values, I’m just making sure you’ve not used the old code duplication and forgotten to edit it (we’ve all been there).

Note: I’d post this as a comment if I could, but I think the solution may be in how you’re pulling the variables. Finally, you could try adding the database name (just the server IP or whatever) to the output, if you’re in ‘dev’/debug (offline/non-production) mode, to check if it’s actually making it to the second server.

1👍

For reference, the Django documentation explicitly states you shouldn’t do this — Altering settings at runetime.

There is a lot of talk within the Django community about the ORM supporting multiple connections/databases at once. There’s a lot of good reference info out there on it. Check out this blog post: Easy Multi-Database Support for Django and this Django wiki page Multiple Database Support.

In the blog post, Eric Florenzano does something like this in his settings.py file:

DATABASES = dict(
    primary = dict(
        DATABASE_NAME=DATABASE_NAME,
        # ...
    ),
    secondary = dict(
        DATABASE_NAME='secondary.db',
        # ...
    ),
)

Leave a comment