create an equivalent of router, need to find a data type for it
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 3s

This commit is contained in:
pierre geeraert
2026-07-02 22:25:35 +01:00
parent c7eb2c5aba
commit d7ef0fcd3e
2 changed files with 72 additions and 3 deletions
+69 -1
View File
@@ -1 +1,69 @@
hello
# routers.py
class ProjectDatabaseRouter:
"""
A router to control all database operations on models in the
simpsons and cloudmanager applications.
"""
def db_for_read(self, model, **hints):
"""Attempts to read simpsons and cloudmanager models go to their specific dbs."""
if model._meta.app_label == 'simpsons':
return 'simpsons_db'
if model._meta.app_label == 'job':
return 'job_db'
if model._meta.app_label == 'redirect':
return 'redirect_db'
if model._meta.app_label == 'cloudmanager':
return 'cloudmanager_db'
if model._meta.app_label == 'telegram':
return 'telegram_db'
if model._meta.app_label == 'podcast':
return 'podcast_db'
return 'default'
def db_for_write(self, model, **hints):
"""Attempts to write simpsons and cloudmanager models go to their specific dbs."""
if model._meta.app_label == 'simpsons':
return 'simpsons_db'
if model._meta.app_label == 'job':
return 'job_db'
if model._meta.app_label == 'cloudmanager':
return 'cloudmanager_db'
if model._meta.app_label == 'redirect':
return 'redirect_db'
if model._meta.app_label == 'telegram':
return 'telegram_db'
if model._meta.app_label == 'podcast':
return 'podcast_db'
return 'default'
def allow_relation(self, obj1, obj2, **hints):
"""Allow relations if both models are in the same database."""
# Prevents accidental foreign key relationships across different physical DBs
if (
obj1._meta.app_label in ['simpsons', 'cloudmanager','redirect','job','telegram','podcast'] or
obj2._meta.app_label in ['simpsons', 'cloudmanager','redirect','job','telegram','podcast']
):
return obj1._meta.app_label == obj2._meta.app_label
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""Ensure the app's models only show up in the designated database."""
if app_label == 'simpsons':
return db == 'simpsons_db'
if app_label == 'job':
return db == 'job_db'
if app_label == 'cloudmanager':
return db == 'cloudmanager_db'
if app_label == 'redirect':
return db == 'redirect_db'
if app_label == 'telegram':
return db == 'telegram_db'
if app_label == 'podcast':
return db == 'podcast_db'
# Ensure default Django apps (auth, sessions, etc.) don't migrate into simpsons/cloudmanager
if db in ['simpsons_db', 'cloudmanager_db','redirect_db','job_db','telegram_db','podcast_db']:
return False
return None