CRYPTO_KEY = b'99lectrHf-urwE8CEXAqCf2UofCb-K-rEiT_VdRWhXY='
class CryptoCharField(models.CharField):
description = _("Crypto char field")
def value_from_object(self, obj):
val = getattr(obj, self.attname)
if val not in (None, "", b""):
val = settings.CYPHER.decrypt(val).decode('utf-8')
return val
def get_internal_type(self):
return "BinaryField"
def get_db_prep_value(self, value, connection, prepared=False):
value = super().get_db_prep_value(value, connection, prepared)
if value is not None:
return
connection.Database.Binary(Fernet(CRYPTO_KEY).encrypt(value.encode('utf-8')))
return value
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34246>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => wontfix
* component: Forms => Database layer (models, ORM)
Comment:
I think we'd rather see specialized fields like this implemented in third-
party packages, e.g. https://pypi.org/project/django-crypto-fields/.
For future reference, it's better to raise a proposal like this on the
DevelopersMailingList to see if there's consensus about adding it.
--
Ticket URL: <https://code.djangoproject.com/ticket/34246#comment:1>