Storing images generated in python into Django database (MySQL)

I am new to django and I researched the site to find good ways to store images generated with python (it seems that inconsistent views are stored in a local folder, file or database). My site is the only way to make qrcodes all the time. On my local machine, I would save it in my program folder, for example:

import pyqrcode
qr = pyqrcode.create("{'name': 'myqr'}")
qr.png("horn.png", scale=6)
print "All done!"

      

I am faced with a situation where I will be dealing with many users doing the same. I doubt storing it in a local folder would be a viable option. I am configured to save these images as blobs in mysql. Has anyone done such a thing? If so, what was the best way to implement it. Sample code would be helpful as well.

+3


source to share


1 answer


Storing images in a database is somewhat reddish. To free up the database, I would not store them in the database, but in a folder on your server.

I understand your question in such a way that some users can create the same qr codes. In this case, I would create a database table like this:

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

      



In this table, you can find QR codes by encoded value. If an entry exists for the given value, you can simply return the contents of the file whose name is preserved.

One question remains: how to create filenames. There are many possibilities. One could create a UUID and make a filename out of it. Another option is to use a global counter and give each file a new number. The counter must be stored in the database table, of course.

Of course, you can still save the image to the database if you like. Just don't use the fname field, but the blob field that holds the content. This solution should work on most databases, but is probably slower than the file-based approach when you have a really large amount of data.

+3


source







All Articles