-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add database router for read-only replica
- Loading branch information
Showing
2 changed files
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import random | ||
|
||
class HotStandbyRouter: | ||
def db_for_read(self, model, **hints): | ||
return random.choice(["default", "replica"]) | ||
|
||
def db_for_write(self, model, **hints): | ||
return "default" | ||
|
||
def allow_relation(self, obj1, obj2, **hints): | ||
# XXX do we need this? | ||
db_set = {"default", "replica"} | ||
if obj1._state.db in db_set and obj2._state.db in db_set: | ||
return True | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters