{"id":8946,"library":"django-cryptography-django5","title":"django-cryptography-django5","description":"A fork of the original `django-cryptography` library, `django-cryptography-django5` provides primitives for easily encrypting data in Django models, wrapping the Python `cryptography` library. It offers field-level encryption for sensitive data, ensuring it's stored securely and decrypted automatically when accessed through Django. The library is actively maintained for Django 5 compatibility and is currently at version 2.2, with releases driven by Django version updates and bug fixes.","status":"active","version":"2.2","language":"en","source_language":"en","source_url":"https://github.com/chrisclark/django-cryptography","tags":["django","encryption","security","cryptography","fields"],"install":[{"cmd":"pip install django-cryptography-django5","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core framework integration; supports Django 3.2, 4.1, 4.2, 5.0.","package":"Django","optional":false},{"reason":"Backend cryptographic operations; requires version 2.0 or higher.","package":"cryptography","optional":false},{"reason":"Runtime environment; requires Python 3.8 or higher.","package":"Python","optional":false}],"imports":[{"note":"Used as a decorator/wrapper for Django model fields to enable encryption.","symbol":"encrypt","correct":"from django_cryptography.fields import encrypt"}],"quickstart":{"code":"import os\nfrom django.db import models\nfrom django_cryptography.fields import encrypt\n\n# Ensure your Django settings.py has a strong SECRET_KEY and optionally ENCRYPTION_KEY\n# For example, in settings.py:\n# SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'your-very-long-and-random-secret-key-here')\n# ENCRYPTION_KEY = os.environ.get('DJANGO_ENCRYPTION_KEY', SECRET_KEY) # Falls back to SECRET_KEY if not set\n\nclass SecureData(models.Model):\n    name = models.CharField(max_length=100)\n    # Encrypt the sensitive_info field\n    sensitive_info = encrypt(models.TextField())\n\n    def __str__(self):\n        return self.name\n\n# Example usage (after running migrations):\n# from myapp.models import SecureData\n# data_entry = SecureData.objects.create(name='User A', sensitive_info='This is very secret data.')\n# print(data_entry.sensitive_info) # Automatically decrypted when accessed via Django ORM","lang":"python","description":"Define a model with `encrypt` wrapped around the desired field. Ensure `SECRET_KEY` (and optionally `ENCRYPTION_KEY`) is configured in your Django settings."},"warnings":[{"fix":"Use `django-cryptography-django5` (version 1.2 or higher) for Django 5.x compatibility.","message":"The original `django-cryptography` library used `django.utils.baseconv`, which was removed in Django 5.x. Attempting to use the un-forked version with Django 5.x will result in import errors or deprecation warnings.","severity":"breaking","affected_versions":"Original `django-cryptography` with Django 5.x"},{"fix":"Avoid using `unique=True` directly on encrypted fields. If uniqueness is a strict requirement, implement it at the application layer (e.g., in `clean()` methods of models or forms) or consider if the field genuinely needs to be both encrypted and unique at the database level.","message":"Using `unique=True` on an encrypted model field (`encrypt(models.SomeField(unique=True))`) can lead to `FieldError: Unsupported lookup 'exact'` or cause the uniqueness constraint to be ignored by the database. Encryption changes the stored value, making true database-level uniqueness difficult to enforce on the ciphertext.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Generate a long, random `SECRET_KEY` for production and manage it securely using environment variables, a secrets manager, or similar methods. Do not commit it to source control.","message":"The security of encrypted data critically depends on Django's `SECRET_KEY`. This key must be strong, unique to each deployment, and kept absolutely secret. Exposing it in version control or using a default/weak key compromises all encrypted data.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Monitor project updates and releases for any changes in development status or significant API shifts. Factor this status into project risk assessments.","message":"The project's PyPI metadata indicates a 'Development Status :: 2 - Pre-Alpha'. While actively maintained, users should be aware of this classification, suggesting it may still be undergoing significant development or refinement.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Remove `unique=True` from the encrypted field definition. Implement uniqueness checks at the application level if required. Review queries against encrypted fields to ensure compatibility.","cause":"Attempting to apply `unique=True` or certain database lookups directly to an `encrypt()`-wrapped field, which stores varying ciphertext values for the same plaintext.","error":"django.core.exceptions.FieldError: Unsupported lookup 'exact' for EncryptedBigIntegerField or join on the field not permitted, perhaps you meant exact or iexact?"},{"fix":"Install the package using pip: `pip install django-cryptography-django5`.","cause":"The `django-cryptography-django5` package or its base (`django-cryptography`) is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'django_cryptography'"},{"fix":"Ensure the correct import path `from django_cryptography.fields import encrypt` is used, and verify `django-cryptography-django5` is installed and up-to-date.","cause":"Incorrect import path or a version mismatch where `encrypt` might have been located elsewhere in older or different forks of the library.","error":"ImportError: cannot import name 'encrypt' from 'django_cryptography.fields'"},{"fix":"Upgrade to the `django-cryptography-django5` package, which provides explicit Django 5.x compatibility. Install with `pip install django-cryptography-django5`.","cause":"Using the older, un-forked `django-cryptography` library with Django 5.x, which removed `django.utils.baseconv`.","error":"DeprecationWarning: django.utils.baseconv is deprecated (when using Django 5.x)"}]}