Comments on: Dynamic upload paths in Django http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/ Code and comments on web development, Django, Python and things (un)related. Wed, 03 Apr 2013 13:15:20 +0000 hourly 1 http://wordpress.org/?v=4.3 By: JayKay http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1092 Sat, 18 Feb 2012 21:35:19 +0000 http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1092 Thanks for this sample, worked perfectly.

]]>
By: Scott http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1039 Mon, 16 May 2011 19:40:30 +0000 http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1039 @Mirror

I think if you save with commit=False, you don’t get an id, in which case that won’t help.

It’s not as nice as just saving the form, but the way to do it would be pass just request.POST (not request.FILES) to the form, then save the file from request.FILES manually.

That’s the gist of it. Sorry, I don’t have any code to share – when I did it the situation was different.

]]>
By: Mirror http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1038 Mon, 16 May 2011 16:32:56 +0000 http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1038 Actually, I meant to say that I came up with those solutions and am not sure if they are correct solutions rather than just ugly hacks.

]]>
By: Mirror http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1037 Mon, 16 May 2011 16:29:18 +0000 http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1037 Thanks, but can you further clarify on some confusion:

How would I save the image after I save the Tree model? I have a model form like so:

class TreeForm(forms.ModelForm):
   class Meta:
      model = TreeModel
      fields = ('title', 'pic')

So in my views, I create a form by doing:

   if request == "POST":
      treeform = TreeForm(request.POST, request.FILES)
      ....

      if treeform.is_valid():
         new_tree = treeform.save()
         ....

Here, saving the pic (through upload_to) will be bundled with saving the entire TreeModel, so id will be ‘None’ at this point. How would you separately save the picture if the pic is a field bound to the TreeModel you’re already saving? Because when the user clicks ‘save’ on the form, it’s going to run through saving the pic as it saves the other fields in TreeModel.

One solution would be to use cleaned_data:

     pic = treeform.cleaned_data['pic']
     new_tree = treeform.save(commit=False)          <=== to ensure that I don't save the image yet because this will produce id=None
     new_tree.pic = pic
     new_tree.save()           <== id shouldn't be None here, I think

second solution would be to modify how save() works in TreeModel, but I'm not sure how that would work.

]]>
By: Scott http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1035 Mon, 16 May 2011 10:21:33 +0000 http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1035 @Mirror

In that situation I save the model instance first, then set the image. When the image is saved the model has an id so using it in the path is ok. This probably won’t work in admin though.

]]>
By: Mirror http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1033 Mon, 16 May 2011 04:03:13 +0000 http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1033 Sorry, my example didn’t show up correctly. I mean to display this as the scheme:

mysite.com/id/slug/pic.png

]]>
By: Mirror http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1032 Mon, 16 May 2011 04:01:55 +0000 http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1032 You’ve noted that ‘instance.id’ will not work anymore. Is there a way to make it work? slugs would be nice, but in my case I have a field that stores a nickname. Since nicknames for trees are not unique, I can’t store the images for a tree in a distinct location if I used slugs. Since ids are unique, I was planning on using this scheme:

mysite.com///pic.png

where ‘id’ is the id of the particular Tree model that holds the ImageField and ‘slug’ is the Model’s title:

class Tree(model.Models):
    def upload_image(self, filename):
         return 'uploads/%s/%s/%s' % (self.id, slugify(self.title), filename)

    title = models.CharField(......)
    pic = models.ImageField(upload_to='upload_image')

As you already mentioned, ‘id’ won’t work anymore since id has not been created, so in the end I receive ‘None’ as the ‘id’. Is there a way around this?

]]>
By: Matt Andrews http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1031 Sun, 15 May 2011 12:31:38 +0000 http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1031 Thanks for this, really helpful.

I needed to use a field from a different model – my image model had a foreign key on a category model and I wanted the URL slug for the category to be in the upload path. I tried this first:

def get_media_path(instance, filename):
    return os.path.join('media', str(instance.media_type__type_slug), filename)

This threw an error, but the syntax below (. instead of __) worked:

def get_media_path(instance, filename):
    return os.path.join('media', str(instance.media_type.type_slug), filename)

Just thought this might help someone.

]]>
By: Scott http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-996 Wed, 09 Feb 2011 09:41:40 +0000 http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-996 @Eric

I like it as a hack. Of course, if two people are adding at the same time, you could get the wrong id for one of them.

Instead of iterating through the queryset, perhaps you could use aggregation something like Photo.objects.aggregate(Max('id'))['id__max']

]]>
By: Eric http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-995 Tue, 08 Feb 2011 20:15:28 +0000 http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-995 Hello everyone,

What do you think about this little hack :

def get_image_path(instance, filename):
    id = instance.id
    if id == None:
        id = max(map(lambda a:a.id,Photo.objects.all())) + 1
    return os.path.join('photos', id, filename)

So if the instance is not created, set the id to the most probable id ie the maximum current id of all the Photo + 1

]]>