'Django: Storing Images Database

Earlier I was using default database sqlite3, but today i've changed it to postgresql.

I wants to save the image files in database not in project directory itself. How can I do that?



Solution 1:[1]

This is not a good idea to store an image in DB instead media folder. But you can use BinaryField for this:

model.py

class MyModel(model.Model):
    image = models.BinaryField(blank=True)

view.py

def image(request):
    image_file = request.FILES['image_file'].file.read()
    MyModel.objects.create(image=image_file)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 amarynets