Stroring images in the database

66 views
Skip to first unread message

ckg...@gmail.com

unread,
Nov 12, 2013, 3:12:14 AM11/12/13
to django...@googlegroups.com
I would like to store images directly into the database i have been trying this but does not seem to help or i may be doing it in the wrong way''

models.py
from database_storage import DatabaseStorage


DB_FILES = {
        'table': 'files',
        'base_url': '/blog/attach/',
    }


class Img(models.model):

         uploaded_map_16 = models.ImageField(upload_to="map_images", storage=DatabaseStorage(DB_FILES), null=True, blank=True)
   
         uploaded_map_17 =  models.ImageField(upload_to="map_images", storage=DatabaseStorage(DB_FILES), null=True, blank=True)
   
         uploaded_map_18 =  models.ImageField(upload_to="map_images", storage=DatabaseStorage(DB_FILES), null=True, blank=True)


this is how i am trying to save it:

storage = DatabaseStorage(DB_FILES)
image_file1 = open(file_name_18, 'rb')
file_content1 = image_file1.read()
image_file2 = open(file_name_17, 'rb')
file_content2 = image_file2.read()
image_file3 = open(file_name_16, 'rb')
file_content3 = image_file3.read()

Img.uploaded_map_18 = storage.save(name + '_18.jpg', file_content1)
Img.uploaded_map_17 = storage.save(name + '_17.jpg', file_content2)
Img.uploaded_map_16 = storage.save(name + '.jpg', file_content3)

Img.save()


Leslie Jeffries

unread,
Nov 12, 2013, 8:47:14 AM11/12/13
to django...@googlegroups.com
Have you looked at this django add-on?

m1chael

unread,
Nov 12, 2013, 8:57:33 AM11/12/13
to django...@googlegroups.com
People have always recommended to me that storing images directly in a
database is a bad idea.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/b01140fc-1931-4b80-8cb5-5e9625ca265f%40googlegroups.com.
>
> For more options, visit https://groups.google.com/groups/opt_out.

Rafael E. Ferrero

unread,
Nov 12, 2013, 9:00:19 AM11/12/13
to django...@googlegroups.com
Why you want to save your images directly on your database?

+1 to m1chael say


2013/11/12 m1chael <myk...@gmail.com>




--
Rafael E. Ferrero

Javier Guerra Giraldez

unread,
Nov 12, 2013, 9:30:35 AM11/12/13
to django...@googlegroups.com
On Tue, Nov 12, 2013 at 8:57 AM, m1chael <myk...@gmail.com> wrote:
> People have always recommended to me that storing images directly in a
> database is a bad idea.


it IS a bad idea, but that doesn't mean it shouldn't be possible.

there are several Storage subclasses that do that, and it's not hard
to do another one. The original question seems to use one of these,
but it's not clear exactly which one.

--
Javier

Avraham Serour

unread,
Nov 12, 2013, 11:24:47 AM11/12/13
to django...@googlegroups.com
I believe 1.6 has a binary field, no need for an external solution, but again, do you really need to store the image in the DB? for most cases ImageField is enough, it stores the path so apache/nginx can serve the image directly


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages