[Django]-Encode ImageField in base64 and store it in DB instead of path of the image

0👍

ImageField, as far as the database is concerned, is a simple VARCHAR(100). That means you can only store up to 100 characters in it. Most of the "magic" of ImageField is that it provides an interface to store the file in the MEDIA_ROOT with particular paths and provides tools to retrieve it again. It also provides tools to store height and width of the image and provide form elements.

If you really want to store binary files in the database, you’ll have to create your own system using a BinaryField or something like that. Remember that you’ll likely also need to store some meta information such as filename or mimetype (since the raw binary won’t have that).

In most cases, storing binary files in a database is usually not the best solution as it’ll be slower than just fetching files from a file system. If you want the files to be accessible between machines, then you’d be better with a network mount or something like AWS S3.

Leave a comment