[Django]-Storing Images generated in python to Django database(MySQL)

3👍

Storing images in the database is somewhat an edge-case. To relieve the database, I would not store them in the database, but in a folder on your server.

I understand your question so, that some users might create the same qr-codes. In this case, I would create a database table like that:

CREATE TABLE qrcodes (
   value TEXT PRIMARY KEY,
   fname TEXT
);

With this table you can find the qr-codes by the encoded value. If an entry exists for a given value, than you can just return the contents of the file whose name is stored.

Leaves one question: How to create the file names. There are many possibilities. One would be to create an UUID and make a filename from it. An other option would be to use a global counter and give every file a new number. The counter must be stored in a database table of course.

Of course you can still store the image in the database, if you want. Just don’t use the fname field, but a blob-field that stores the content. That solution should work on most databases, but is likely slower than the file approach when you have really big amounts of data.

Leave a comment