File: multiple_dbs.rst

package info (click to toggle)
python-django-simple-history 2.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 732 kB
  • sloc: python: 4,273; makefile: 187
file content (41 lines) | stat: -rw-r--r-- 1,492 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Multiple databases
==================

Interacting with Multiple Databases
-----------------------------------

`django-simple-history` follows the Django conventions for interacting with multiple databases.

.. code-block:: python

    >>> # This will create a new historical record on the 'other' database.
    >>> poll = Poll.objects.using('other').create(question='Question 1')

    >>> # This will also create a new historical record on the 'other' database.
    >>> poll.save(using='other')


When interacting with ``QuerySets``, use ``using()``:

.. code-block:: python

    >>> # This will return a QuerySet from the 'other' database.
    Poll.history.using('other').all()

When interacting with manager methods, use ``db_manager()``:

.. code-block:: python

    >>> # This will call a manager method on the 'other' database.
    >>> poll.history.db_manager('other').as_of(datetime(2010, 10, 25, 18, 4, 0))

See the Django documentation for more information on how to interact with multiple databases.

Tracking User in a Separate Database
------------------------------------

When using ``django-simple-history`` in app with multiple database, you may run into
an issue where you want to track the history on a table that lives in a separate
database to your user model. Since Django does not support cross-database relations,
you will have to manually track the ``history_user`` using an explicit ID. The full
documentation on this feature is in :ref:`Manually Track User Model`.