Skip to content

Commit b50a35a

Browse files
committed
Fixed #12941 -- Added documentation for the connections dictionary. Thanks to [email protected] for the report, and Alex Gaynor for the original text.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12709 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent abd0e96 commit b50a35a

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

docs/topics/db/multi-db.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,15 @@ This example sets up two admin sites. On the first site, the
535535
objects have an tabular inline showing books published by that
536536
publisher. The second site exposes just publishers, without the
537537
inlines.
538+
539+
Using raw cursors with multiple databases
540+
=========================================
541+
542+
If you are using more than one database you can use
543+
``django.db.connections`` to obtain the connection (and cursor) for a
544+
specific database. ``django.db.connections`` is a dictionary-like
545+
object that allows you to retrieve a specific connection using it's
546+
alias::
547+
548+
from django.db import connections
549+
cursor = connections['my_db_alias'].cursor()

docs/topics/db/sql.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ In these cases, you can always access the database directly, routing around
196196
the model layer entirely.
197197

198198
The object ``django.db.connection`` represents the
199-
current database connection, and ``django.db.transaction`` represents the
200-
current database transaction. To use the database connection, call
199+
default database connection, and ``django.db.transaction`` represents the
200+
default database transaction. To use the database connection, call
201201
``connection.cursor()`` to get a cursor object. Then, call
202202
``cursor.execute(sql, [params])`` to execute the SQL and ``cursor.fetchone()``
203203
or ``cursor.fetchall()`` to return the resulting rows. After performing a data
@@ -220,6 +220,15 @@ is required. For example::
220220

221221
return row
222222

223+
If you are using more than one database you can use
224+
``django.db.connections`` to obtain the connection (and cursor) for a
225+
specific database. ``django.db.connections`` is a dictionary-like
226+
object that allows you to retrieve a specific connection using it's
227+
alias::
228+
229+
from django.db import connections
230+
cursor = connections['my_db_alias'].cursor()
231+
223232
.. _transactions-and-raw-sql:
224233

225234
Transactions and raw SQL

0 commit comments

Comments
 (0)