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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

Supported by

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