Skip to main content

django-sqlite-strict

python >= 3.10 Django >= 4.2 SQLite prek pytest nox coverage GitHub Actions Workflow Status PyPI Version

django-sqlite-strict is a drop-in replacement for Django's stock SQLite engine that enforces STRICT tables.

Prerequisites

django-sqlite-strict works on Django webapps that use:

  • Django >= 4.2
  • SQLite >= 3.37.0

Supported Django versions depend on the Python version in use; see pyproject.toml for the exact dependency constraints.

Install

  1. Add as a dependency:

    uv add django-sqlite-strict
    
  2. In settings.py, add to INSTALLED_APPS.

    INSTALLED_APPS = [
        ...,
        'django_sqlite_strict',
        ...,
    ]
    
  3. In settings.py, configure DATABASES to use it as the engine:

    DATABASES = {
        "default": {
            "ENGINE": "django_sqlite_strict",
            "NAME": BASE_DIR / "db.sqlite3",
        }
    }
    

Features

Compared to Django's stock SQLite backend

SQLite STRICT tables only permit the storage classes INTEGER, REAL, TEXT, BLOB and ANY (source).
To generate valid STRICT tables, django-sqlite-strict maps field types differently to Django's stock SQLite backend. For example:

  • CharField is stored as TEXT instead of VARCHAR
  • BooleanField as INTEGER instead of BOOL
  • DecimalField as REAL instead of DECIMAL

These field remappings affect only the SQL column types used in migrations. Django's Python field API remains unchanged.

[!NOTE] Plain Django DecimalField is stored as REAL so it remains compatible with SQLite STRICT column types. Values wider than SQLite's exact REAL precision may lose precision. For exact decimal storage on SQLite, use django_sqlite_strict.fields.StrictDecimalField, which stores scaled integer minor units.

StrictDecimalField

django_sqlite_strict.fields.StrictDecimalField is a DecimalField subclass that stores exact integer minor units in an INTEGER column instead of storing decimals as REAL. For example, Decimal("19.99") with decimal_places=2 is stored as 1999 and read back as a Decimal.

from django.db import models

from django_sqlite_strict.fields import StrictDecimalField


class Order(models.Model):
    total_price = StrictDecimalField(max_digits=18, decimal_places=2)
  • Round-trips are exact for max_digits <= 18. The dss.E003 check guards against wider fields because they could overflow SQLite's 8-byte INTEGER.
  • Values with more decimal places than the field declares raise ValueError on save.
  • Lookups and ordering work normally because comparisons are scaled to the stored integer value.
  • SQL-side arithmetic, including F() expressions, and raw SQL use the stored minor units, ie. 1999 rather than 19.99.

System checks

django-sqlite-strict will register the following Django system checks:

  • check_column_types
    Raises an error if any entry in the DATABASES setting has a column of a type that is not accepted by STRICT tables.
  • check_tables_are_strict
    Raises an error if a model's table already exists in the database but is not STRICT, ie. the ENGINE was switched without running convert_to_strict.
  • check_decimal_max_digits
    Raises a warning if a DecimalField has max_digits set to a value higher than the '15 significant digits' threshold up to which SQLite's REAL stores decimals.
  • dss.E003
    Raises an error if a StrictDecimalField has max_digits > 18, meaning its integer minor units could overflow SQLite's 8-byte INTEGER. Runs on every ./manage.py check.

Management commands

  • convert_to_strict
    Rebuilds existing non-STRICT tables as STRICT ones. Pass --dry-run to preview the effect of this command, and --no-input for use in CI pipelines.

Escape hatches

When you own the model, prefer mapping unsupported field types to STRICT-compatible storage, for example use StrictDecimalField for exact decimals.
For third-party app models, setting the strict_exempt_tables option allows specified tables to be created with Django's stock non-STRICT template. Exempt tables are skipped by the checks & the convert_to_strict management command.

DATABASES = {
    "default": {
        "ENGINE": "django_sqlite_strict",
        "NAME": BASE_DIR / "db.sqlite3",
        "OPTIONS": {
            "strict_exempt_tables": ["some_third_party_table"],
        },
    }
}

Develop

Development prerequisites

The following tools must be available locally to develop django-sqlite-strict:

Run tests

The project aims for 100% test coverage. nox is used to run the test suite against all supported Python/Django pairings (see noxfile.py).

# run latest supported Python/Django pairing only
# (eg. Python 3.14 / Django 6.1)
uvx nox

# run all test sessions
# (ie. all supported Python/Django pairings)
uvx nox -s test

Use the development version in projects

Build the package as a binary distribution (wheel) and install it in a Django project:

# in django-sqlite-strict's root directory
uv build

# in the target Django webapp project
uv add ~/Projects/django-sqlite-strict/dist/django_sqlite_strict-M.m.p-py3-none-any.whl

Acknowledgements

This package was inspired by Martin Dørum's advocacy for STRICT tables in the article SQLite should have (Rust-style) editions.


Shameless self-promotion

Starting a new Django project? Try djereo for a Django project template with modern defaults.

Try pycliché for a no-nonsense Python project template with opinionated tooling.


19th century apparatus to enforce STRICT columns (source, Eton College)

Download files

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

Source Distribution

django_sqlite_strict-1.1.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

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

django_sqlite_strict-1.1.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file django_sqlite_strict-1.1.0.tar.gz.

File metadata

  • Download URL: django_sqlite_strict-1.1.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_sqlite_strict-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f1c8c2fa8e1487e000256390799031409d86ae4d096903b2974ce0d00087644a
MD5 4503defc757a3d5b2d06147732aa07d9
BLAKE2b-256 38e84966fe7a78e106c24987c0229a0576ca4c5430504988927c65f1d54aafe7

See more details on using hashes here.

File details

Details for the file django_sqlite_strict-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_sqlite_strict-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_sqlite_strict-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a01b0f4a96dbb7b9e1c2f7b3cc7ca19430201e3fb199de9dee7ebf507e24dd63
MD5 4eb04dd99667aed68d77ec057b9ee303
BLAKE2b-256 5004967776c2020221bef58093eaf01d3648a4ed0aabf14309c34aaab35ba396

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 files

1.0.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page