13

Does anyone know if there's an out-of-the box way of storing images directly in the database vs. using ImageField model type that simply uploads it to the MEDIA_ROOT. And if there is, how does one serve those images then?

Cheers

Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
Rok
  • 1,482
  • 3
  • 18
  • 37
  • I'd recommend to use the AppEngine Blob Store service. Not exactly out-of-the-box, but a clean a simple solution. Check this answer https://stackoverflow.com/questions/18747730/storing-images-in-db-using-django-models/18778381#18778381 – Cartucho Mar 14 '19 at 20:15

3 Answers3

7

No, there isn't. And for good reason. It's horribly inefficient to store and serve images from the database. Store them on the filesystem, and serve them directly from Apache.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 27
    perhaps you could give more details on the horrible inefficiencies of storing images in a database? – jeff7091 Apr 21 '11 at 18:57
  • 3
    It should be noted that serving images using Apache is not recommended. – Izz ad-Din Ruhulessin Aug 08 '11 at 00:21
  • 1
    @Izzad-DinRuhulessin was that meant as a joke? If not, citation needed – Foon Sep 11 '13 at 17:45
  • 2
    Unfounded and wrong. If there's a cache layer after the server, there mightn't be an issue. Storing in database adds portability - a db dump is complete data set. –  Oct 06 '16 at 07:27
  • 2
    Should not be the accepted answer. This depends on the use case and DB. For example using SQLite to store and retrieve images can even be 35% faster than retrieving from filesystem, see: https://www.sqlite.org/fasterthanfs.html – Wolkenarchitekt Apr 01 '19 at 19:02
  • 1
    Not really an answer. – Cartucho Feb 27 '21 at 22:46
4

There is a nice solution here: http://djangosnippets.org/snippets/1305/ it stores content in a database blob.

Andrzej Bobak
  • 2,106
  • 3
  • 28
  • 36
1

It seems there is no built-in BlobField in Django. However, there is one available here. I'm not sure if it supports all backends, but it might work for you. With that, you could write up a form & view that uploades the image as an attachment and stores it as a blob in the database.

André Caron
  • 44,541
  • 12
  • 67
  • 125