djangodjango-modelsdjango-rest-framework

What fieldtype of Django model should I choose to store encrypted data?


I am working on a project where the requirement is to store some encrypted data in the database. What is the preferred fieldtype of Django model for storing encrypted data?

I am currently using CharField, however, I am not sure if this is the best approach. Also, should BinaryField be an option?


Solution

  • This depends on your encryption. If the encryption is a password for example, Django by default uses a CharField. But CharField always needs to have a max_length kwarg. So if your encryption produces a constant length hash, use CharField - if not you can use TextField.