Skip to content

Instantly share code, notes, and snippets.

@tswicegood
Created June 12, 2009 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tswicegood/128787 to your computer and use it in GitHub Desktop.
Save tswicegood/128787 to your computer and use it in GitHub Desktop.
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