Looking for a Django developer? Red Robot Studios is now accepting clients for Django development projects. Find out more...

Dynamic upload paths in Django

25 August 2008

For a while I’ve been using the CustomImageField as a way to specify an upload path for images. It’s a hack that lets you use ids or slugs from your models in the upload path, e.g.:

/path/to/media/photos/1234/flowers.jpg
or
/path/to/media/photos/scotland-trip/castle.jpg

CustomImageField no more

Since the FileStorageRefactor was merged in to trunk r8244, it’s no longer necessary to use the custom field. Other recent changes to trunk mean that it doesn’t work any more in its current state, so this is a good time to retire it.

Pass a callable in upload_to

It is now possible for the upload_to parameter of the FileField or ImageField to be a callable, instead of a string. The callable is passed the current model instance and uploaded file name and must return a path. That sounds ideal.

Here’s an example:

import os
from django.db import models

def get_image_path(instance, filename):
    return os.path.join('photos', instance.id, filename)

class Photo(models.Model):
    image = models.ImageField(upload_to=get_image_path)

get_image_path is the callable (in this case a function). It simply gets the id from the instance of Photo and uses that in the upload path. Images will be uploaded to paths like:

photos/1/kitty.jpg

You can use whatever fields are in the instance (slugs, etc), or fields in related models. For example, if Photo models are associated with an Album model, the upload path for a Photo could include the Album slug.

Note that if you are using the id, you need to make sure the model instance was saved before you upload the file. Otherwise, the id hasn’t been set at that point and can’t be used.

For reference, here’s what the main part of the view might look like:

...
    if request.method == 'POST':
        form = PhotoForm(request.POST, request.FILES)
        if form.is_valid():
            photo = Photo.objects.create()
            image_file = request.FILES['image']
            photo.image.save(image_file.name, image_file)
...

This is much simpler than the hacks used in CustomImageField and provides a nice flexible way to specify file or image upload paths per-model instance.

Note: If you are using ModelForm, when you call form.save() it will save the file – no need to do it yourself as in the example above.

Filed under: Django — Scott @ 10:38 pm

16 Comments »

  1. Thanks for example Scott, there are really lots of new features since refactoring!

    Comment by ilya — 27 August 2008 @ 9:26 am

  2. For some reason, whenever one posts a new image (I am doing this from the admin interface) using the above method, instance.id returns None, which then gets inserted in my upload_to path where I thought instance.id should have gone… Do you have any recommendations as to what I should do? I’ve been playing with this a lot, and I’ve had no luck. Thank you very much!

    Comment by Kevin Holzer — 31 August 2008 @ 9:49 am

  3. @Kevin Holzer

    I don’t see a good way around that, Kevin. Admin is getting all the field values (including the upload_to path) before saving. Before the first save, there is no id for you to use. In your own views, you get to create the model instance first and then save the image, but not with admin.

    If there’s another field you can use, such as a slug, that’s probably the best alternative. Otherwise, you could upload to a temporary path and respond to the post_save signal by moving the image to its proper location.

    Comment by Scott — 1 September 2008 @ 9:18 am

  4. Thanks alot! I’ve a bunch of these CustomImageField hacks, and I’m just starting to update them, but first needed to get up-to-date on the refactoring stuff. You’ve really made it easier for me.

    Comment by Bjorn — 2 September 2008 @ 1:32 pm

  5. NOTE (OBS!): If you are using this method with a ModelForm, and using data from the fields for your filename, your image field MUST come after (in the model) the other fields you want to access in creating the filename. The fields in the instance which is passed into your callable are filled out in order of declaration, so if your image is defined first, the instance will have EMPTY STRINGS for all other fields and you will get an EMPTY FILENAME.

    Comment by Kellen — 15 September 2008 @ 5:57 pm

  6. Thanks a lot for both the code and the decent guide-through. Nice, simple code and works perfectly.

    Comment by Bulak — 27 December 2008 @ 4:15 pm

  7. For multiple models that modify the upload_to path you can use a single function to create the callable to pass to the upload_to parameter.

    def upload_to(path, attribute):
    
        def upload_callback(instance, filename):
            return '%s%s/%s' % (path, unicode(slugify(getattr(instance, attribute))), filename)
    
        return upload_callback
    
    ...
    #Model definitions
    class Photo(models.Model):
        name = models.CharField()
        image = models.ImageField(upload_to = upload_to('uploads/', 'name'))
    
    class Screenshot(models.Model):
        title = models.CharField()
        image = models.ImageField(upload_to = upload_to('uploads/', 'title'))

    Comment by bcline — 14 January 2009 @ 7:51 pm

  8. For completeness, the above code uses…

    from django.template.defaultfilters import slugify

    Comment by bcline — 15 January 2009 @ 9:25 am

  9. Needed this functionality, found your post, implemented it in 10 minutes. Super helpful, thanks!

    Comment by Tom — 20 May 2009 @ 5:30 am

  10. Yes, very helpful. Thanks a bunch!

    Comment by Moorthy — 25 May 2009 @ 11:18 am

  11. Really helpful, great!

    I have a question, is it possible to give the method a third argument?
    something like below?

    def get_image_path(instance, filename, number):
    return ‘photos/%s/%s%s’ % (instance.id, filename, number)

    and how would you use it in the model?

    thx

    Comment by Incinnerator — 3 August 2009 @ 10:02 pm

  12. @Incinnerator.

    The method gets called by Django, so you can’t add parameters to it. If you can work out the “number” from the instance, that’s best. Otherwise, I think you’ll need to get that number from the submitted form or from the view and set it separately on your model (Photo in my example).

    Comment by Scott — 4 August 2009 @ 12:22 pm

  13. Nice example, simple and to the point. However remember to use os.path.join instead of manually writing dir paths, or it wouldn’t work in (at least) Windows.

    import os
    
    def get_image_path(instance, filename):
        return os.path.join('photos', instance.id, filename)

    Comment by Fidel Ramos — 3 September 2009 @ 10:20 am

  14. Thanks, Fidel. I’ve updated the post to use os.path.join in the example.

    Comment by Scott — 3 September 2009 @ 10:56 am

  15. Thanks for the great example.

    Comment by Richard — 21 November 2009 @ 11:13 pm

  16. Thanks very much for this tutorial … it was a lifesaver for me. A small note to add to the comments:

    - The upload_to callable technique also works just fine if you’re uploading an image as part of larger form. You can save a form as normal in your view code, and the request.FILES data will be passed along to your model w/o problems, like this:

        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            form.save() # all FILES data get passed along, get_image_path callable works fine
            return HttpResponseRedirect('/some/url/')

    Initially I got stuck on this point, because I was trying to save the form with form.save() AND save the image separately with the view code in your tutorial.

    Again thanks. There are a lot of outdated file/image upload tutorials on the web that can be quite confusing for django newcomers. This is great.

    Comment by Jesse — 2 January 2010 @ 6:24 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment