0
  • Python 3.8.10
  • Django 4.0.2
  • django-encrypted-model-fields: 0.6.1

I am using encrypted_model_fields to encrypt a model field. The field I am encrypting is a password used to access a user account for another app via an api.

According to the docs I need to import the module and then wrap the model field as follows:

Models.py

from encrypted_model_fields.fields import EncryptedCharField
account_password = EncryptedCharField(models.CharField(max_length=20, blank=True))

In addition to that I need to add a FIELD_ENCRYPTION_KEY to settings.py, which I have done, as per the docs.

Settings.py

FIELD_ENCRYPTION_KEY = os.environ.get('FIELD_ENCRYPTION_KEY')

I have also added 'encrypted_model_fields' to installed apps in settings.py and the encryption key to .env, which is in place of the placeholder below..env:

export FIELD_ENCRYPTION_KEY=my_encryption_key_place_holder

When I run makemigrations I receive the following error:

django.core.exceptions.ImproperlyConfigured: FIELD_ENCRYPTION_KEY must be defined in settings

I have defined it - so why is it not found?

JessicaRyan
  • 115
  • 3
  • 14
  • I still don't really understand the difference between using and not using the `export` word, even after reading [this](https://stackoverflow.com/questions/1158091/defining-a-variable-with-or-without-export), but try removing the term from the definition of `FIELD_ENCRYPTION_KEY` in your .env file. – raphael Feb 13 '22 at 13:10
  • You can also try to set a default value, like this: `FIELD_ENCRYPTION_KEY = os.environ.get('FIELD_ENCRYPTION_KEY', '')` – raphael Feb 13 '22 at 14:54
  • Thanks - I tried that initially. I think it's an issue with django version. I totally moved away from cryptography and used Signer and secrets instead. – JessicaRyan Feb 14 '22 at 05:07

1 Answers1

0

It seems that the issue is with the django version 4 - cryptography has not been updated for the latest version of django. After trying multiple solutions for encrypting a model field using a number of modules that use cryptography I finally decided to just use secrets and Signer instead.

import secrets
from django.core.signing import Signer

foo = secrets.token_hex(16)
signer = Signer()
value = signer.sign(foo)
queryset = Model.objects.get(pk=foo_pk)
queryset.model_field = foo
queryset.save()

documentation for Signer documentation for secrets

JessicaRyan
  • 115
  • 3
  • 14