Skip to main content

Generate PostgreSQL schema-prefixed table names with the Django ORM

Project description

django-fqq

Tests

Generate schema-prefixed table names with the Django ORM.

django-fqq provides a custom database backend that wraps Django's built-in PostgreSQL backend. It overrides quote_name to prepend the active schema to table names — so "my_table" becomes "my_schema"."my_table" automatically. No changes to your models or queries required.

django-fqq does not modify queries if set_schema has not been called or has been set to a falsy value (e.g. None or "").

Installation

pip install django-fqq

Configuration

1. Set the database backend

DATABASES = {
    "default": {
        "ENGINE": "django_fqq.backend",
        "NAME": "your_db",
        # ... other options
    }
}

If you need a custom database backend you can either inherit from django_fqq.backend.base.DatabaseWrapper or set ops_class = DatabaseOperations from django_fqq.backend.operations.DatabaseOperations in your DatabaseWrapper.

2. Add the app

INSTALLED_APPS = [
    # ...
    "django_fqq",
]

3. Configure shared apps (optional)

Apps listed in FQQ_SHARED_APPS will always use the public schema, regardless of the active schema.

FQQ_SHARED_APPS = ["auth", "contenttypes"]

4. Add the middleware (optional)

The middleware provides very basic routing of requests to a specific database schema. For a more robust solution that handles migrations and other functionality, check out django-tenants.

MIDDLEWARE = [
    "django_fqq.middleware.SchemaMiddleware",
    # ...
]

Subclass SchemaMiddleware and override _get_schema(request) to resolve the schema from the request (e.g. from subdomain, header, or URL).

Usage

Setting the schema manually

from django_fqq.schema import set_schema, clear_schema

set_schema("my_schema")
# All queries now target the "my_schema" schema
# ...
clear_schema()

In a middleware or view

from django_fqq.middleware import SchemaMiddleware

class MySchemaMiddleware(SchemaMiddleware):
    def _get_schema(self, request):
        host = request.get_host().split(".")[0]
        return host

Using a context manager

query_schema sets the schema for the duration of a block and restores the previous state on exit.

from django_fqq.schema import query_schema

with query_schema("my_schema"):
    # All queries target "my_schema"
    ...
# Schema is restored to its previous value (or cleared if none was set)

Nesting is supported — each level restores the schema that was active before it:

from django_fqq.schema import set_schema, query_schema

set_schema("schema_a")

with query_schema("schema_b"):
    # queries target "schema_b"
    with query_schema("schema_c"):
        # queries target "schema_c"
        ...
    # back to "schema_b"

# back to "schema_a"

Context-safe

Schema state is stored in a ContextVar, so it's safe across concurrent async requests and threads.

Development

Requires Python 3.12+ and Django 4.2+. Tested against Django 4.2, 5.2, and 6.0.

uv sync
uv run pytest

License

MIT

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_fqq-0.1.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

django_fqq-0.1.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file django_fqq-0.1.0.tar.gz.

File metadata

  • Download URL: django_fqq-0.1.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_fqq-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3bfbcab41fb810e61f9838acc5929d2b697afe08317c1727c515685becfd5660
MD5 bd2077d5b1fcb3f365f644e56072ff43
BLAKE2b-256 b49afc35298d5f525c2172996865d68ae88c189f06c221cc009cee4ae069076d

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_fqq-0.1.0.tar.gz:

Publisher: publish.yml on apricotdotcool/django-fqq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_fqq-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_fqq-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_fqq-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e58afd4656fc0133b42c59248d17b56562a7e0ac16faa2d6a9206b760fe1f605
MD5 a99239c4c86c6a4df4439b41fb7127fe
BLAKE2b-256 141e02c5a37587e4ff5398d5d01bdbb8d86083b8fcda4108b95669d7ea46909d

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_fqq-0.1.0-py3-none-any.whl:

Publisher: publish.yml on apricotdotcool/django-fqq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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