django-sqlite-strict
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
-
Add as a dependency:
uv add django-sqlite-strict
-
In
settings.py, add toINSTALLED_APPS.INSTALLED_APPS = [ ..., 'django_sqlite_strict', ..., ]
-
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:
CharFieldis stored asTEXTinstead ofVARCHARBooleanFieldasINTEGERinstead ofBOOLDecimalFieldasREALinstead ofDECIMAL
These field remappings affect only the SQL column types used in migrations. Django's Python field API remains unchanged.
[!NOTE] Plain Django
DecimalFieldis stored asREALso it remains compatible with SQLiteSTRICTcolumn types. Values wider than SQLite's exact REAL precision may lose precision. For exact decimal storage on SQLite, usedjango_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. Thedss.E003check guards against wider fields because they could overflow SQLite's 8-byte INTEGER. - Values with more decimal places than the field declares raise
ValueErroron 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.1999rather than19.99.
System checks
django-sqlite-strict will register the following Django system checks:
check_column_types
Raises an error if any entry in theDATABASESsetting 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. theENGINEwas switched without runningconvert_to_strict.check_decimal_max_digits
Raises a warning if aDecimalFieldhasmax_digitsset to a value higher than the '15 significant digits' threshold up to which SQLite's REAL stores decimals.dss.E003
Raises an error if aStrictDecimalFieldhasmax_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-runto preview the effect of this command, and--no-inputfor 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1c8c2fa8e1487e000256390799031409d86ae4d096903b2974ce0d00087644a
|
|
| MD5 |
4503defc757a3d5b2d06147732aa07d9
|
|
| BLAKE2b-256 |
38e84966fe7a78e106c24987c0229a0576ca4c5430504988927c65f1d54aafe7
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a01b0f4a96dbb7b9e1c2f7b3cc7ca19430201e3fb199de9dee7ebf507e24dd63
|
|
| MD5 |
4eb04dd99667aed68d77ec057b9ee303
|
|
| BLAKE2b-256 |
5004967776c2020221bef58093eaf01d3648a4ed0aabf14309c34aaab35ba396
|