0

I've Images in my project folder created automatically and I want to upload these images to my MySQL DB. These images are named img0.png, img1.png.. etc

What I am currently doing :
views.py

for i in range(imageCount): img = open("img%s.png" %i) obj = userImages(userId = userid, images=img) obj.save() 

models.py

class userImages(models.Model): userId = models.IntegerField() images = models.ImageField() 

This doesn't work. How can I make it work??

3
  • 1
    I'd suggest you to store the file in a different place (like a server, google storage or aws s3) and create a column to keep the path in the db... databases are not so great to keep files... stackoverflow.com/questions/3748/… Commented May 20, 2020 at 14:18
  • Thanks for the suggestion, but is there any way I can make this work? I want these images to be stored directly in the database. Commented May 20, 2020 at 14:28
  • 1
    I found this pynative.com/… Commented May 20, 2020 at 14:46

1 Answer 1

1

Databases are not a good place to put files in, please consider using s3 or cloud.

If you really want to save it on database use BinaryField but as the same django docs is warning you that:

Abusing BinaryField

Although you might think about storing files in the database, consider that it is bad design in 99% of the cases. This field is not a replacement for proper static files handling.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.