aditya
aditya

Reputation: 133

DJANGO - Upload an image file from directory to DB

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??

Upvotes: 0

Views: 340

Answers (1)

Jian Huang
Jian Huang

Reputation: 454

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.

Upvotes: 1

Related Questions