Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like the following:
On model load: SELECT AES_DECRYPT(fieldname, password) FROM tablename
On model save: INSERT INTO tablename VALUES (AES_ENCRYPT(userinput, password))
I would define a custom modelfield for the column you want encrypted/decrypted. Override the
to_pythonmethod to run the decryption when the model is loaded, andget_db_prep_valueto run the encryption on saving.Remember to set the field’s metaclass to
models.SubfieldBaseotherwise these methods won’t be called.