I'm working on adding multiple-database support to an application and am building a custom manager that lets me specify which DATABASE_NAME to use for a particular model. It looks something like this:
class OtherDatabaseManager(models.Manager): """ This Manager lets you set the DATABASE_NAME on a per-model basis. """ def __init__(self, database_name, *args, **kwargs): models.Manager.__init__(self, *args, **kwargs) self.database_name = database_name def get_query_set(self): qs = models.Manager.get_query_set(self) qs.query.connection = self.get_db_wrapper() return qs def get_db_wrapper(self): from django.db.backends.postgresql_psycopg2.base import DatabaseWrapper # Monkeypatch the settings file. This is not thread-safe! old_db_name = settings.DATABASE_NAME settings.DATABASE_NAME = self.database_name wrapper = DatabaseWrapper() wrapper._cursor(settings) settings.DATABASE_NAME = old_db_name return wrapper class MyModel(models.Model): # ... objects = OtherDatabaseManager('my_custom_database) This is not unlike the code here: http://www.eflorenzano.com/blog/post/easy-multi-database-support-django/ -- One ugliness about this is that it has to monkeypatch the settings file in order to change the DATABASE_NAME, before passing it to DatabaseWrapper._cursor(). So my proposal is to change DatabaseWrapper._cursor() to accept a settings *dictionary* instead of a settings *object*. The only place in the Django codebase that calls DatabaseWrapper._cursor() is BaseDatabaseWrapper.cursor() in db/backends/__init__.py. That, plus the fact that this method name is preceded by an underscore, leads me to believe there are no backwards-incompatibility issues with making this change. But I'm bringing it up here in case anybody wants to point out problems before I commit it. Adrian --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---