Index: models/sql/query.py
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- models/sql/query.py	(revision 9685)
+++ models/sql/query.py	(working copy)
@@ -1783,6 +1783,13 @@
 else:=0A=
     Query =3D BaseQuery=0A=
 =0A=
+def query_class_wrap(connection,QueryClass=3DBaseQuery):=0A=
+    if connection.features.uses_custom_query_class:=0A=
+    	Query =3D connection.ops.query_class(BaseQuery)=0A=
+    else:=0A=
+    	Query =3D BaseQuery=0A=
+    return Query=0A=
+    =0A=
 def get_order_dir(field, default=3D'ASC'):=0A=
     """=0A=
     Returns the field name and direction for an order specification. For=0A=
Index: models/options.py
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- models/options.py	(revision 9685)
+++ models/options.py	(working copy)
@@ -21,7 +21,7 @@
 DEFAULT_NAMES =3D ('verbose_name', 'db_table', 'ordering',=0A=
                  'unique_together', 'permissions', 'get_latest_by',=0A=
                  'order_with_respect_to', 'app_label', 'db_tablespace',=0A=
-                 'abstract')=0A=
+                 'abstract', 'using',)=0A=
 =0A=
 class Options(object):=0A=
     def __init__(self, meta, app_label=3DNone):=0A=
@@ -58,6 +58,7 @@
         self.object_name =3D cls.__name__=0A=
         self.module_name =3D self.object_name.lower()=0A=
         self.verbose_name =3D get_verbose_name(self.object_name)=0A=
+        self.using =3D None=0A=
 =0A=
         # Next, apply any overridden values from 'class Meta'.=0A=
         if self.meta:=0A=
Index: models/query.py
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- models/query.py	(revision 9685)
+++ models/query.py	(working copy)
@@ -123,7 +123,11 @@
     """=0A=
     def __init__(self, model=3DNone, query=3DNone):=0A=
         self.model =3D model=0A=
-        self.query =3D query or sql.Query(self.model, connection)=0A=
+        if model._meta.using:=0A=
+            from django.db import get_connection, get_current_connection=0A=
+            self.query =3D query or sql.Query(self.model, =
get_connection(model._meta.using))=0A=
+        else:=0A=
+            self.query =3D query or sql.Query(self.model, connection)=0A=
         self._result_cache =3D None=0A=
         self._iter =3D None=0A=
         self._sticky_filter =3D False=0A=
Index: __init__.py
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- __init__.py	(revision 9685)
+++ __init__.py	(working copy)
@@ -1,11 +1,15 @@
-import os=0A=
-from django.conf import settings=0A=
+import os, copy=0A=
+from django.conf import settings, LazySettings=0A=
 from django.core import signals=0A=
 from django.core.exceptions import ImproperlyConfigured=0A=
 from django.utils.functional import curry=0A=
+try:=0A=
+    import thread=0A=
+except ImportError:=0A=
+    import dummy_thread as thread=0A=
+    =0A=
+__all__ =3D ('backend', 'connection', 'DatabaseError', =
'IntegrityError', 'get_backend', 'get_connection', =
'get_current_connection','ConnectionManagementError', 'using')=0A=
 =0A=
-__all__ =3D ('backend', 'connection', 'DatabaseError', 'IntegrityError')=0A=
-=0A=
 if not settings.DATABASE_ENGINE:=0A=
     settings.DATABASE_ENGINE =3D 'dummy'=0A=
 =0A=
@@ -31,15 +35,88 @@
         available_backends.sort()=0A=
         if settings.DATABASE_ENGINE not in available_backends:=0A=
             raise ImproperlyConfigured, "%r isn't an available database =
backend. Available options are: %s\nError was: %s" % \=0A=
-                (settings.DATABASE_ENGINE, ", ".join(map(repr, =
available_backends)), e_user)=0A=
+                  (settings.DATABASE_ENGINE, ", ".join(map(repr, =
available_backends)), e_user)=0A=
         else:=0A=
             raise # If there's some other error, this must be an error =
in Django itself.=0A=
 =0A=
 # Convenient aliases for backend bits.=0A=
-connection =3D backend.DatabaseWrapper(**settings.DATABASE_OPTIONS)=0A=
+_connection =3D backend.DatabaseWrapper(**settings.DATABASE_OPTIONS)=0A=
 DatabaseError =3D backend.DatabaseError=0A=
 IntegrityError =3D backend.IntegrityError=0A=
 =0A=
+_backends =3D {}=0A=
+_settings =3D {}=0A=
+_connections =3D {}=0A=
+state =3D {}=0A=
+=0A=
+for name in settings.DATABASES:=0A=
+    database =3D settings.DATABASES[name]=0A=
+    if not database['DATABASE_ENGINE'] in _backends:=0A=
+        try:=0A=
+            backend =3D __import__('django.db.backends.' + =
database['DATABASE_ENGINE']=0A=
+                                 + ".base", {}, {}, ['base'])=0A=
+        except ImportError, e:=0A=
+            try:=0A=
+                backend =3D __import__('%s.base' % =
database['DATABASE_ENGINE'], {}, {}, [''])=0A=
+            except ImportError, e_user:=0A=
+                raise	=0A=
+        _backends[database['DATABASE_ENGINE']] =3D backend=0A=
+    options =3D LazySettings()=0A=
+    for key, value in database.iteritems():=0A=
+        setattr(options, key, value)=0A=
+    _settings[name] =3D options=0A=
+    wrapper =3D backend.DatabaseWrapper(**options.DATABASE_OPTIONS)=0A=
+    wrapper._cursor(options)=0A=
+    _connections[name] =3D wrapper=0A=
+=0A=
+def get_backend(name):=0A=
+    return _backends[settings.DATABASES[name]['DATABASE_ENGINE']]=0A=
+=0A=
+def get_connection(name):=0A=
+    settings =3D _settings[name]=0A=
+    wrapper =3D _connections[name]=0A=
+    return wrapper=0A=
+=0A=
+class ConnectionManagementError(Exception):=0A=
+    pass=0A=
+=0A=
+def enter_connection_management(connection):=0A=
+    thread_ident =3D thread.get_ident()=0A=
+    if thread_ident in state and state[thread_ident]:=0A=
+        state[thread_ident].append(connection)=0A=
+    else:=0A=
+        state[thread_ident] =3D []=0A=
+        state[thread_ident].append(connection)=0A=
+=0A=
+def leave_connection_management():=0A=
+    thread_ident =3D thread.get_ident()=0A=
+    if thread_ident in state and state[thread_ident]:=0A=
+        del state[thread_ident][-1]=0A=
+    else:=0A=
+        raise ConnectionManagementError("This code isn't under =
connection management")=0A=
+=0A=
+class using(object):=0A=
+    def __init__(self, database):=0A=
+        self.database =3D database=0A=
+    def __enter__(self):=0A=
+        enter_connection_management(get_connection(self.database))=0A=
+    def __exit__(self, etyp, einst, etb):=0A=
+        leave_connection_management()=0A=
+=0A=
+class ConnectionDesc(object):=0A=
+    def __getattribute__(self, key):=0A=
+        db =3D  get_current_connection()=0A=
+        return db.__getattribute__(key)=0A=
+=0A=
+def get_current_connection():=0A=
+    thread_ident =3D thread.get_ident()=0A=
+    if thread_ident in state and state[thread_ident]:=0A=
+        return state[thread_ident][-1]=0A=
+    else:=0A=
+        return _connection=0A=
+=0A=
+connection =3D ConnectionDesc()=0A=
+=0A=
 # Register an event that closes the database connection=0A=
 # when a Django request is finished.=0A=
 def close_connection(**kwargs):=0A=
