django-cryptography

raw JSON →
2.0.3 verified Fri May 01 auth: no python

django-cryptography provides encryption for Django model fields using AES. Current version 2.0.3, with a focus on simplicity and Django ORM integration. Release cadence is low; maintained by saaspegasus.

pip install django-cryptography
error ModuleNotFoundError: No module named 'pycryptodome'
cause Missing required dependency pycryptodome.
fix
Install it: pip install pycryptodome
error ImportError: cannot import name 'EncryptedCharField' from 'encrypt.fields'
cause Using deprecated import path from an older version of the package.
fix
Use: from django_cryptography.fields import EncryptedCharField
error django.db.utils.OperationalError: no such column: ...
cause You forgot to run migrations after adding an encrypted field to your model.
fix
Run python manage.py makemigrations and python manage.py migrate.
gotcha Encrypted fields are not sortable or filterable by their encrypted values. Querying by encrypted field will not work as expected.
fix Do not use encrypted fields in `filter()`, `order_by()`, or indexing. Consider using a non-encrypted field or hashing for lookups.
gotcha If you change the `SECRET_KEY` in Django settings, all existing encrypted data will become undecryptable. There is no built-in key rotation.
fix Back up existing data before changing SECRET_KEY. Implement manual re-encryption if key rotation is needed.
deprecated The package was previously named `django-cryptography` and later `django-encrypt`? Actually, the correct import path changed from `encrypt` to `django_cryptography`. Ensure you are not using the old import.
fix Update imports to `from django_cryptography.fields import ...`

Define an encrypted char field in a Django model.

import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()

from django_cryptography.fields import EncryptedCharField
from django.db import models

class MyModel(models.Model):
    secret = EncryptedCharField(max_length=255)

# Now you can create and save instances normally:
instance = MyModel.objects.create(secret='my secret')
print(instance.secret)  # Output: 'my secret'