djfernet-redux
Fernet symmetric encryption for Django model fields, using the cryptography library. A maintained continuation of djfernet, itself a fork of django-fernet-fields.
Drop-in replacement for djfernet and django-fernet-fields: the same import
name (fernet_fields), the same settings and the same encrypted data format. Swap the
package and nothing else changes: no code, migration or data changes.
pip uninstall djfernet django-fernet-fields
pip install djfernet-redux
Uninstall the old package first: both install the fernet_fields module.
Supported versions
Every combination below is tested.
| Python | 3.8 – 3.14 |
| Django | 2.2 – 6.1, each on the Python versions it supports |
| Databases | PostgreSQL 9.6 – 18 (psycopg2, and psycopg 3 from Django 4.2), SQLite |
| cryptography | 3.1 and later |
Other Django database backends with BinaryField support (MySQL, MariaDB, Oracle)
should work but are not tested.
Usage
Import and use the field classes in your models:
from django.db import models
from fernet_fields import EncryptedTextField
class Customer(models.Model):
tax_id = EncryptedTextField()
Read and write tax_id as usual: values are encrypted before they are sent to the
database and decrypted when read. Encryption happens in your app; the key never
reaches the database server, which only ever sees the encrypted value.
Field types
EncryptedTextField, EncryptedCharField, EncryptedEmailField,
EncryptedIntegerField, EncryptedDateField, EncryptedDateTimeField,
EncryptedBinaryField and EncryptedJSONField. Each accepts the same arguments as
its unencrypted Django counterpart.
EncryptedJSONField stores any JSON-serializable value (dicts, lists, strings,
numbers, booleans). Like Django's JSONField it accepts encoder and decoder, for
example encoder=DjangoJSONEncoder to store Decimal and datetime values (they read
back as strings). Its form field, used by the admin and ModelForm, needs Django 3.1
or later.
To encrypt another field class, inherit from EncryptedField and that class:
from django.db import models
from fernet_fields import EncryptedField
class EncryptedBigIntegerField(EncryptedField, models.BigIntegerField):
pass
Nullable fields
A nullable encrypted field stores Python None as SQL NULL, which shows anyone with
database access whether a value is present. If that matters, use a non-nullable field
and store an "empty" sentinel value, which is encrypted like any other value.
Keys
By default the fields use your SECRET_KEY setting as the encryption key.
To use dedicated keys, list them in FERNET_KEYS. The first key encrypts all new data;
decryption tries every key in order. To rotate keys, put the new key first and keep the
old ones until all data has been re-saved:
FERNET_KEYS = [
env("FERNET_KEY"), # encrypts new and changed data
env("FERNET_KEY_2025"), # still decrypts older data
]
Disabling HKDF
Fernet needs a 32-byte, url-safe base64-encoded key. By default the fields derive one from each configured key with HKDF, so any string works as a key.
To provide Fernet keys directly (for example from
Fernet.generate_key()),
set FERNET_USE_HKDF = False. Every key in FERNET_KEYS must then be a valid Fernet
key; an invalid key usually fails with an "incorrect padding" error. Without
FERNET_KEYS, SECRET_KEY itself must be a Fernet key.
Data from djfernet 0.7
djfernet 0.7.x derived keys with a different HKDF salt. If your data was written by djfernet before 0.8, add this setting (note the bytestring):
DJFERNET_PREFIX = b'djfernet'
Data written by django-fernet-fields or djfernet 0.8 and later needs no setting.
Lookups, indexes and ordering
Fernet encryption is not deterministic: the same value encrypts differently every time. Filtering, indexing or enforcing uniqueness on encrypted data can't work, so:
primary_key=True,unique=Trueanddb_index=TrueraiseImproperlyConfigured.- Every lookup except
isnullraisesFieldError. - Ordering by an encrypted field doesn't raise, but it orders by the encrypted bytes, which is meaningless.
The admin and forms show decrypted values, so restrict admin access and list_display
accordingly.
Migrations
The database can't encrypt existing values, so AlterField from a plain field to an
encrypted one doesn't work. Instead:
- Add the encrypted field under a new name, with
null=True. - Copy the values in a data migration (
RunPythonwith the ORM, not raw SQL); saving through the ORM encrypts them. - Remove the old field and, if needed, rename the new one.
Troubleshooting
DecryptionError: Can't decrypt <field>
A stored value doesn't decrypt with any configured key. Check that FERNET_KEYS (or
SECRET_KEY) still includes the key the data was written with, FERNET_USE_HKDF, and
DJFERNET_PREFIX for data from djfernet 0.7. It also happens when the column holds
unencrypted or empty values, for example after an AlterField from a plain field.
DecryptionError subclasses cryptography.fernet.InvalidToken, so existing
except InvalidToken handlers keep working.
EncryptedDateTimeField returns naive datetimes with USE_TZ = True
On SQLite (and other backends without native time zone support) aware datetimes are
stored in UTC and read back as naive UTC values; on PostgreSQL they read back aware.
Use timezone.make_aware(value, timezone.utc) where you need an aware value. This is
unchanged from django-fernet-fields, so existing code keeps working.
EncryptedBinaryField values written on PostgreSQL by djfernet 0.8
djfernet 0.8 encrypted the database driver's text representation of the bytes instead
of the bytes themselves, so those values read back as text such as
'\\000\\377'::bytea (psycopg2) or Binary(b'...') (psycopg 3, cut short for longer
values). djfernet-redux encrypts the bytes themselves; re-save affected values from
their original source.
Credits
djfernet-redux continues the work of the people who built and maintained this library, and all credit for its design and code belongs to them:
- django-fernet-fields, the original library, © 2015 ORCAS, Inc., created by Carl Meyer with Jess Johnson and maintained by Brad Pitcher, with contributions from Alex Riina, Vlad Temian, Yacht Shaver, Owen Miller, Douglas Anderson, Carel Burger and Brian Helba.
- djfernet, the maintained fork by James Pic (yourlabs), with contributions from Stefan, irtazaakram and Flávio Juvenal da Silva Junior.
Fixes and ideas in djfernet-redux also come from Sid Mitra (Airbase's psycopg 3 fix), intgr, lucas-bremond and Sult; see the changelog.
The original BSD license and copyright notice are kept in LICENSE.txt, and the original authors in AUTHORS.md. The full commit history of both projects is preserved in this repository.
Contributing and help
- Source, issues and pull requests: https://github.com/amanjain/djfernet-redux
- How to run the tests: CONTRIBUTING.md
- What changed in each release: CHANGES.md
- Install from PyPI:
pip install djfernet-redux
Metadata
Release files for djfernet-redux 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| djfernet_redux-1.0.0.tar.gz | 10.6 kB | With provenance | Details for djfernet_redux-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| djfernet_redux-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for djfernet_redux-1.0.0-py3-none-any.whl |
Total release size: 20.4 kB
Release files / djfernet_redux-1.0.0.tar.gz
| Download URL | djfernet_redux-1.0.0.tar.gz |
|---|---|
| Size | 10.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
67bfc7412394ca7cb7eedccc3dddbe6105f230147312e390e1f1c91f55bd5676
|
|
BLAKE2b-256 checksum How to use checksums |
5a66f6f12ec6cecd0992d0a2d58d7da00e891ce45edee0e7aa239c9706fbd551
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.
Transparency logRelease files / djfernet_redux-1.0.0-py3-none-any.whl
| Download URL | djfernet_redux-1.0.0-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
86295ff6ef59d20b9b3faeee34fabedb96074f2e348f8abe948846742178a9d6
|
|
BLAKE2b-256 checksum How to use checksums |
2da1feaae94190cf292832ab6a40b31c4dd9a9688d842fd138750ea21b9c1cd1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.
Transparency logSecurity for djfernet-redux 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for djfernet_redux-1.0.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.