Created
June 12, 2009 17:42
-
-
Save tswicegood/128787 to your computer and use it in GitHub Desktop.
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
class CustomModelManager(models.Manager): | |
_connection = None | |
def get_query_set(self): | |
return QuerySet(self.model, self.create_query()) | |
def create_query(self): | |
return Query(self.model, self.get_connection()) | |
def get_connection(self): | |
if self._connection is None: | |
if hasattr('settings', 'CUSTOM_DATABASE'): | |
self._connection = self.create_custom_db() | |
else: | |
self._connection = connection | |
return self._connection | |
def create_custom_db(self): | |
database_conf = settings.CUSTOM_DATABASE | |
to_import = 'django.db.backends.' + database_conf['DATABASE_ENGINE'] + '.base' | |
backend = __import__(to_import, {}, {}, ['base']) | |
return backend.DatabaseWrapper(database_conf) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment