Skip to main content

No project description provided

Project description

django-multidb

Configure different apps or models to different database instances.

Django settings

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": data_dir / "default.sqlite3",
    },
    "db1": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": data_dir / "db1.sqlite3",
    },
    "db2": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": data_dir / "db2.sqlite3",
    },
    "db3": {
        "ENGINE": "djongo",
        "NAME": "db3",
    }
}

Configure different models to different database instances.

DATABASE_ROUTERS = ["django_multidb.routers.DatabaseModelsRouter"] 
DATABASE_MODELS_MAPPING = {"permission": "db1", "role": "db2"}

The Permission model uses db1, and the Role model uses db2. Run the migrate command to create tables.

python manage.py migrate --database=db1 # create permission
python manage.py migrate --database=db2 # create role

Configure different apps to different database instances.

DATABASE_ROUTERS = ["django_multidb.routers.DatabaseAppsRouter"]
DATABASE_APPS_MAPPING = {"authz": "db1", "order": "db2"}

The app authz uses db1 instance, and the order uses db2 instance. Run the migrate command to create tables. c

python manage.py migrate --database=db1 # create all tables for the authz app. 
python manage.py migrate --database=db2 # Create all tables for the order app.

Tested association queries and deletions between different SQLite instances and MongoDB. More use cases can be referred to in the tests.

About the Queryset.filter method

Due to the involvement of cross-database instance querying, even direct associations across different database architectures, the filter method has been overridden.

First, query the IDs of data in the associated table and then perform a query on the main table using an IN condition with these IDs, rather than using JOIN syntax for the query.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_multidb-0.1.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_multidb-0.1.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file django_multidb-0.1.1.tar.gz.

File metadata

  • Download URL: django_multidb-0.1.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.9 Darwin/23.2.0

File hashes

Hashes for django_multidb-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c8f690afc9177cab694bbd0bf8514a9e455daed9565941bbc60bed581d257260
MD5 c760576e9d09b4e50410a3ea73870a96
BLAKE2b-256 df4358b4f650b709e1c8b3927d29364a59d8dbe5bdcadb1663c1a475acb38f22

See more details on using hashes here.

File details

Details for the file django_multidb-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: django_multidb-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.9 Darwin/23.2.0

File hashes

Hashes for django_multidb-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 11feda3690b5aa2e91aed09d11360ccdac938e06057d990d75553a57ddae2b4e
MD5 4d3bb23fb3bc8dea686fcf07b7cea2e6
BLAKE2b-256 9ea6686d73d220f8249b11e240bda724ee8299c449c070d8479af6cbf5b8685e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page