Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django store image in database

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

like image 404
Rok Avatar asked Sep 22 '10 16:09

Rok


People also ask

Can we store images in Django database?

Most of the web applications deal with the file or images, Django provides the two model fields that allow the user to upload the images and files. These fields are FileField and ImageField; basically ImageField is a specialized version of FileField that uses Pillow to confirm that file is an image.

How do I store images in Django?

In Django, a default database is automatically created for you. All you have to do is add the tables called models. The upload_to tells Django to store the photo in a directory called pics under the media directory. The list_display list tells Django admin to display its contents in the admin dashboard.

Where to store files in Django?

By default, Django stores files locally, using the MEDIA_ROOT and MEDIA_URL settings. The examples below assume that you're using these defaults. However, Django provides ways to write custom file storage systems that allow you to completely customize where and how Django stores files.

Is it possible to store images in Django using DB?

Some people states that using DB for storing images is not a good idea but that's no true. My advice is to use Django with AppEngine Blobstore Service in this way: First, create a Django Custom Storage (or pick one from someone else like this one ):

What is the use of image field in Django?

The image column is an ImageField field that works with the Django’s file storage API, which provides a way to store and retrieve files, as well as read and write them. The upload_to parameters specify the location where images will be stored which for this model is MEDIA_ROOT/images/

How do I add images to a model in Django?

Let’s, start by creating models. The image column is an ImageField field that works with the Django’s file storage API, which provides a way to store and retrieve files, as well as read and write them. The upload_to parameters specify the location where images will be stored which for this model is MEDIA_ROOT/images/

How to create a database table for an image in Django?

So below we create a database table called Image. from django.db import models class Image (models.Model): name= models.CharField (max_length=500) videofile= models.FileField (upload_to='images/', null=True, verbose_name="") def __str__ (self): return self.name + ":" + str (self.imagefile)


3 Answers

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.

like image 136
Daniel Roseman Avatar answered Oct 28 '22 19:10

Daniel Roseman


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

like image 22
Andrzej Bobak Avatar answered Oct 28 '22 18:10

Andrzej Bobak


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.

like image 22
André Caron Avatar answered Oct 28 '22 19:10

André Caron