A library that provides Django model fields to store value encrypted using Fernet.
With a correctly configured pipenv
toolchain:
pipenv install django-fernet
You may also use classic pip
to install the package:
pip install django-fernet
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
from django.db import models
from django_fernet.fields import *
class ExampleTextModel(models.Model):
example_field = FernetTextField(
verbose_name="Example field",
)
from django.db import models
from django_fernet.fields import *
class ExampleBinaryModel(models.Model):
example_field = FernetBinaryField(
verbose_name="Example field",
)
from django_fernet.fernet import *
field_data = FernetTextFieldData()
field_data.encrypt("foo", "--secret--")
instance = ExampleTextModel()
instance.example_field = field_data
instance.save()
from django_fernet.fernet import *
field_data = FernetTextFieldData()
field_data.encrypt(b"foo", "--secret--")
instance = ExampleBinaryModel()
instance.example_field = field_data
instance.save()
instance = ExampleTextModel.objects.get(pk=...)
decrypted_str = instance.example_field.decrypt("--secret--")
instance = ExampleBinaryModel.objects.get(pk=...)
decrypted_bytes = instance.example_field.decrypt("--secret--")
Django 4.2 | Django 5.0 | Django 5.1 | |
---|---|---|---|
Python 3.10 | ✓ | ✓ | ✓ |
Python 3.11 | ✓ | ✓ | ✓ |
Python 3.12 | ✓ | ✓ | ✓ |
Python 3.13 | ✓ | ✓ | ✓ |
PyPy 3.10 | ✓ | ✓ | ✓ |
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
- Andreas Stocker [email protected]