About Using Multi-database Project

Django supports projects with multiple databases. In that case, all databases will have the same schema which is based on your models.

Settings:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "main.sqlite3",
    },
    "archive": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "archive.sqlite3",
    },
}

To run migrations in the non-default database, pass the --database parameter:

python manage.py migrate --database=archive

Use the using() queryset method to operate on the specific database:

Post.objects.using("archive").create(title="Hello, World!", content="...")

Use the using argument in the save method, to save an entry in a specific database:

post = Post(title="Hello, World!", content="...")
post.save(using="archive")

To check to which database an entry belongs, see its state:

post._state.db  # "archive" or "default"

To check which database is queried by a queryset, see the db attribute:

qs.db  # "archive" or "default"

Tips and Tricks Programming Development Databases Django 5.2 Django 4.2 Django 3.2 PostgreSQL MySQL SQLite