I had trouble wrapping my head around Django ORM’s handling of Images.
The first hurdle was realizing that
there is no easy way to store images in the database. I couldn’t be too upset with that, it was a flawed concept at best. I wrote a dump script to fix this, and converted my app to read from disk. One of those “good” problems.
When it came to uploading new content… I seemed to hit every roadblock. So I
thought this guide might be of use for people (other than future-me). What I
want to do is upload an image, resize it per the requirements of my app, and
save the resized image.
The Template
The template was fairly easy, yet I forgot to specify the enctype attribute.
Here’s what you need:
Resizing an image
Resizing an image was straightforward. The Python Imaging Library is very easy
to work with, and very similar to the PHP GD Library. There was one caveat…
I’m using OS X and didn’t have libjpeg installed. The recommended way was to
install it via fink.
Generating a filename
I like using MD5 to generate filenames for images because MD5 almost guarantees
uniqueness (“unique enough”).
Saving the file via the model
The model is supposed to put the files in the right spot and populate the meta data.
You can supposedly do this via:
That will also take care of my_object.save(). The trick is that content
needs to be a django.core.files.File object or things will never work.
Further more, you can’t use a StringIO for your File object. A true file
object is best. Hence rewriting the JPEG:
Note: I also had to reopen the imagefile.
All together now
I’m sure there’s room to optimize this flow. But this gets the job done. I do
wish I could have used a StringIO instead of an actual file to save the
image. Writing to disk twice seems silly.