[1.10.x] Edited multi-db topic guide for grammar and clarity.

Backport of ea65c7cb48 from master
This commit is contained in:
Liz Lemon 2016-08-02 19:55:57 -04:00 committed by Tim Graham
parent bcdf6c190a
commit 0d82389adf
1 changed files with 24 additions and 9 deletions

View File

@ -45,11 +45,11 @@ If the concept of a ``default`` database doesn't make sense in the context
of your project, you need to be careful to always specify the database of your project, you need to be careful to always specify the database
that you want to use. Django requires that a ``default`` database entry that you want to use. Django requires that a ``default`` database entry
be defined, but the parameters dictionary can be left blank if it will not be be defined, but the parameters dictionary can be left blank if it will not be
used. You must setup :setting:`DATABASE_ROUTERS` for all of your apps' models, used. To do this, you must set up :setting:`DATABASE_ROUTERS` for all of your
including those in any contrib and third-party apps you are using, so that no apps' models, including those in any contrib and third-party apps you're using,
queries are routed to the default database in order to do this. The following so that no queries are routed to the default database. The following is an
is an example ``settings.py`` snippet defining two non-default databases, with example ``settings.py`` snippet defining two non-default databases, with the
the ``default`` entry intentionally left empty:: ``default`` entry intentionally left empty::
DATABASES = { DATABASES = {
'default': {}, 'default': {},
@ -78,7 +78,7 @@ The :djadmin:`migrate` management command operates on one database at a
time. By default, it operates on the ``default`` database, but by time. By default, it operates on the ``default`` database, but by
providing the :option:`--database <migrate --database>` option, you can tell it providing the :option:`--database <migrate --database>` option, you can tell it
to synchronize a different database. So, to synchronize all models onto to synchronize a different database. So, to synchronize all models onto
all databases in our example, you would need to call:: all databases in the first example above, you would need to call::
$ ./manage.py migrate $ ./manage.py migrate
$ ./manage.py migrate --database=users $ ./manage.py migrate --database=users
@ -88,6 +88,13 @@ particular database, you can define a :ref:`database
router<topics-db-multi-db-routing>` that implements a policy router<topics-db-multi-db-routing>` that implements a policy
constraining the availability of particular models. constraining the availability of particular models.
If, as in the second example above, you've left the ``default`` database empty,
you must provide a database name each time you run :djadmin:`migrate`. Omitting
the database name would raise an error. For the second example::
$ ./manage.py migrate --database=users
$ ./manage.py migrate --database=customers
Using other management commands Using other management commands
------------------------------- -------------------------------
@ -359,8 +366,8 @@ routers are defined)::
DATABASE_ROUTERS = ['path.to.AuthRouter', 'path.to.PrimaryReplicaRouter'] DATABASE_ROUTERS = ['path.to.AuthRouter', 'path.to.PrimaryReplicaRouter']
The order in which routers are processed is significant. Routers will The order in which routers are processed is significant. Routers will
be queried in the order the are listed in the be queried in the order they are listed in the
:setting:`DATABASE_ROUTERS` setting . In this example, the :setting:`DATABASE_ROUTERS` setting. In this example, the
``AuthRouter`` is processed before the ``PrimaryReplicaRouter``, and as a ``AuthRouter`` is processed before the ``PrimaryReplicaRouter``, and as a
result, decisions concerning the models in ``auth`` are processed result, decisions concerning the models in ``auth`` are processed
before any other decision is made. If the :setting:`DATABASE_ROUTERS` before any other decision is made. If the :setting:`DATABASE_ROUTERS`
@ -394,6 +401,13 @@ With this setup installed, lets run some Django code::
>>> # ... but if we re-retrieve the object, it will come back on a replica >>> # ... but if we re-retrieve the object, it will come back on a replica
>>> mh = Book.objects.get(title='Mostly Harmless') >>> mh = Book.objects.get(title='Mostly Harmless')
This example defined a router to handle interaction with models from the
``auth`` app, and other routers to handle interaction with all other apps. If
you left your ``default`` database empty and don't want to define a catch-all
database router to handle all apps not otherwise specified, your routers must
handle the names of all apps in :setting:`INSTALLED_APPS` before you migrate.
See :ref:`contrib_app_multiple_databases` for information about contrib apps
that must be together in one database.
Manually selecting a database Manually selecting a database
============================= =============================
@ -586,7 +600,8 @@ where all objects of a given type are stored on a specific database
usage of multiple databases is more complex, your ``ModelAdmin`` will usage of multiple databases is more complex, your ``ModelAdmin`` will
need to reflect that strategy. need to reflect that strategy.
Inlines can be handled in a similar fashion. They require three customized methods:: :class:`~django.contrib.admin.InlineModelAdmin` objects can be handled in a
similar fashion. They require three customized methods::
class MultiDBTabularInline(admin.TabularInline): class MultiDBTabularInline(admin.TabularInline):
using = 'other' using = 'other'