Django Fernet
A library that provides Django model fields to store value encrypted using Fernet.
Installation
With a correctly configured pipenv toolchain:
pipenv install django-fernet
You may also use classic pip to install the package:
pip install django-fernet
Auto-formatter setup
We use ruff (https://github.com/astral-sh/ruff) for local auto-formatting and for linting in the CI pipeline. The pre-commit framework (https://pre-commit.com) provides Git hooks for these tools, so they are automatically applied before every commit.
Steps to activate:
- Install the pre-commit framework:
pip install pre-commit(for alternative installation options see https://pre-commit.com/#install) - Activate the framework (from the root directory of the repository):
pre-commit install
Hint: You can also run the formatters manually at any time with the following command: pre-commit run --all-files
Getting started
Example model that defines fernet text fields
from django.db import models
from django_fernet.fields import *
class ExampleTextModel(models.Model):
example_field = FernetTextField(
verbose_name="Example field",
)
Example model that defines fernet binary fields
from django.db import models
from django_fernet.fields import *
class ExampleBinaryModel(models.Model):
example_field = FernetBinaryField(
verbose_name="Example field",
)
How to use
Save encrypted text to the database
from django_fernet.fernet import *
field_data = FernetTextFieldData()
field_data.encrypt("foo", "--secret--")
instance = ExampleTextModel()
instance.example_field = field_data
instance.save()
Save encrypted binary data to the database
from django_fernet.fernet import *
field_data = FernetTextFieldData()
field_data.encrypt(b"foo", "--secret--")
instance = ExampleBinaryModel()
instance.example_field = field_data
instance.save()
Load encrypted text from the database
instance = ExampleTextModel.objects.get(pk=...)
decrypted_str = instance.example_field.decrypt("--secret--")
Load encrypted binary data from the database
instance = ExampleBinaryModel.objects.get(pk=...)
decrypted_bytes = instance.example_field.decrypt("--secret--")
Supported versions
| Django 4.2 | Django 5.0 | Django 5.1 | |
|---|---|---|---|
| Python 3.10 | ✓ | ✓ | ✓ |
| Python 3.11 | ✓ | ✓ | ✓ |
| Python 3.12 | ✓ | ✓ | ✓ |
| Python 3.13 | ✓ | ✓ | ✓ |
| PyPy 3.10 | ✓ | ✓ | ✓ |
Tests
An example Django app that makes use of django-fernet can be found in the tests/ folder. This example
Django app also contains the unit tests.
Follow below instructions to run the tests. You may exchange the installed Django version according to your requirements.
# install dependencies
python -m pip install --upgrade pip
pip install -r requirements.txt
# run tests
cd tests && pytest
List of developers
- Andreas Stocker AStocker@anexia-it.com
Metadata
Release files for django-fernet 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-fernet-1.0.1.tar.gz | 5.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_fernet-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.8 kB
Release files / django-fernet-1.0.1.tar.gz
| Download URL | django-fernet-1.0.1.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5efd93818c10eb2c22ddb70feb01827af5e158eb0d9ced495bf99f309ba5e3ef
|
|
BLAKE2b-256 checksum How to use checksums |
5ec92b27096ca1cd129de03c82c4a625f291800a42c0862cda6402c690129577
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.15
|
Release files / django_fernet-1.0.1-py3-none-any.whl
| Download URL | django_fernet-1.0.1-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
26db0c46a53d0eac6332de8862d740ef9e7638d4410ff3615631ce1cbbc751e9
|
|
BLAKE2b-256 checksum How to use checksums |
fed0265ce523f743c7d40e9e67f955f954c2410d39c26478af54f38f8186f9ca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.15
|