Key(s) for using in encryption/decryption with Fernet. Usually generated from python manage.py generate_key. For rotation keys, use a list of keys instead (see MultiFernet).
SECURED_FIELDS_HASH_SALT
No
''
Salt to append after the field value before hashing. Usually generated from python manage.py generate_key.
File storage class used for storing encrypted file/image fields. See EncryptedStorageMixin
APIs
Field Arguments
Name
Type
Required
Default
Description
searchable
bool
No
False
Enable search function. exact/in lookups are only available when this is True; on a non-searchable field they raise LookupNotSupported.
Changing searchable on a field with existing records
Existing records stay readable after changing searchable, but they keep the storage format
of the previous flag until they are re-saved:
False → True: existing records do not have a hashed section yet, so exact/in lookups will not match them.
True → False: existing records still carry the old hashed section — a deterministic fingerprint of the value — until they are re-saved.
After changing the flag (and running makemigrations/migrate, since the database index changes),
re-save the affected records to convert them to the new format. Do this in a data migration that runs
after the generated AlterField operation — the historical model must already carry the new
searchable value, otherwise the records are silently re-written in the old format:
def resave_records(apps, schema_editor):
model = apps.get_model('myapp', 'MyModel')
model.objects.bulk_update(model.objects.all(), ['my_field'], batch_size=1000)
bulk_update re-encrypts through the same path as save() but batches the queries, and — unlike
re-saving each record — does not overwrite auto_now date/datetime fields with the migration run time.
Encryption
> from secured_fields.fernet import get_fernet
> data = b'test'
> encrypted_data = get_fernet().encrypt(data)
> encrypted_data
b'gAAAAABh2_Ry_thxLTuFFXeMc9hNttah82979JPuMSjnssRB0DmbgwdtEU5dapBgISOST_a_egDc66EG_ZtVu_EqF_69djJwuA=='
> get_fernet().decrypt(encrypted_data)
b'test'
If you have a field which is not supported by the package, you can use EncryptedMixin to enable encryption and search functionality for that custom field.
import secured_fields
from django.db import models
class EncryptedUUIDField(secured_fields.EncryptedMixin, models.UUIDField):
pass
task_id = EncryptedUUIDField(searchable=True)
EncryptedStorageMixin
If you use a custom file storage class (e.g. defined in settings.py's STORAGES), you can enable file encryption using EncryptedStorageMixin.
import secured_fields
from minio_storage.storage import MinioMediaStorage
class EncryptedMinioMediaStorage(
secured_fields.EncryptedStorageMixin,
MinioMediaStorage,
):
pass
Known Limitation
in lookup on JSONField is not available
Large files are not performance-friendly at the moment (see #2)
Search on BinaryField does not supported at the moment (see #6)
update() only accepts plain values or Value() for encrypted fields; other expressions such as F(), Case or database functions raise ExpressionNotSupported, since their result would be written unencrypted (see #52). bulk_update() is supported