Can Django models use MySQL functions?

前端 未结 6 1646
情深已故
情深已故 2021-02-06 16:05

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

6条回答
  •  说谎
    说谎 (楼主)
    2021-02-06 16:38

    I would define a custom modelfield for the column you want encrypted/decrypted. Override the to_python method to run the decryption when the model is loaded, and get_db_prep_value to run the encryption on saving.

    Remember to set the field's metaclass to models.SubfieldBase otherwise these methods won't be called.

提交回复
热议问题