Document direct API usage of FileField and ImageField

114 views
Skip to first unread message

Yo-Yo Ma

unread,
Sep 15, 2010, 2:28:58 PM9/15/10
to Django developers
I think it might be a good idea to document the direct usage of the
FileField, and ImageField model fields.

The docs make the assumption that everyone is using ModelForm to
upload files. If I have a file on my hard drive and want to use it to
populate the field, I would try something like:



from PIL import Image, ImageFile
image = open('/some/path/to/image.jpg', 'rb')

instance = MyModel(
image=image,
title="Hello, World."
)

instance.save()



The docs show examples of how to manually use models, but don't give
any examples of how to do this when an image, or file is included in
the mix.

Yo-Yo Ma

unread,
Sep 15, 2010, 2:31:41 PM9/15/10
to Django developers
BTW, ignore the PIL imports

Russell Keith-Magee

unread,
Sep 15, 2010, 7:52:52 PM9/15/10
to django-d...@googlegroups.com
On Thu, Sep 16, 2010 at 2:28 AM, Yo-Yo Ma <baxters...@gmail.com> wrote:
> I think it might be a good idea to document the direct usage of the
> FileField, and ImageField model fields.

Sure -- sounds like a reasonable proposal to me. Open a ticket on Trac
so the idea isn't forgotten.

We also accept patches :-)

Yours,
Russ Magee %-)

Yo-Yo Ma

unread,
Sep 15, 2010, 8:34:03 PM9/15/10
to Django developers
I actually don't know how to do it myself. I'm still trying to put
some context to the Storage API, and file uploads in general. There
seems not to be a consensus on the best way to handle files in Django.
I figured I'd put the request up here so others don't run into the
same problem, but I've not solved my own problems in the meantime.
When I do, I'll make a patch for the docs.

I'm trying to allow a user to upload a single photo, and have it saved
as 2 different sized thumbnails, as well the original. Should I look
into using the Storage API directly?



On Sep 15, 5:52 pm, Russell Keith-Magee <russ...@keith-magee.com>
wrote:

Owen Nelson

unread,
Sep 17, 2010, 2:11:40 PM9/17/10
to django-d...@googlegroups.com
I'd love to pitch in on updating the docs for file handling :)

Even though this kind of talk is better suited over on django-users,  here's one way to skin a cat.
I had a view that displayed a model form (one of the fields was a FileField).  I needed to alter the resolution of the uploaded image to make sure it wasn't HUGE.


In my case, I was resizing the original image *after* it had been stored according to the file field's upload_to settings.
The 'image' I got from the form is basically a file object (http://docs.djangoproject.com/en/dev/ref/files/file/) which is what I tripped up on at first when putting this together.  You have to make sure whatever you pass to ImageField as "contents" is an object that implements that "File" interface, so to speak.  In the case of the upload from the form, this was done for us, but if you have some random file on the filesystem, you'd do something like this:

from django.core.files import File
tmpfile = File(open('/tmp/some-pic.jpg','rb'))
object.image.save('pic.jpg', tmpfile, save=True) # copy the image to it's proper location and save the model instance when done.

Owen Nelson

unread,
Sep 17, 2010, 8:12:30 PM9/17/10
to django-d...@googlegroups.com
Actually, I'm not sure what way to go with this.  All the info is there, it's just spread around in a number of topics:

I'd almost say the best way to explore file handling would be to add an "extra credit" tutorial -- an extension of the poll app that serves as django's HelloWorld.  Tricky thing is that there are a lot of moving parts... the storage system, upload_to, serving static files (if using ./manage.py runserver), upload handlers, the concepts around the File wrapper...  It's all a bit much for a newcomer to the project.
Reply all
Reply to author
Forward
0 new messages