<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Dynamic upload paths in Django</title>
	<atom:link href="http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/feed/" rel="self" type="application/rss+xml" />
	<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/</link>
	<description>Code and comments on web development, Django, Python and things (un)related.</description>
	<lastBuildDate>Wed, 16 May 2012 20:44:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: JayKay</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1092</link>
		<dc:creator>JayKay</dc:creator>
		<pubDate>Sat, 18 Feb 2012 21:35:19 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1092</guid>
		<description>Thanks for this sample, worked perfectly.</description>
		<content:encoded><![CDATA[<p>Thanks for this sample, worked perfectly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1039</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Mon, 16 May 2011 19:40:30 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1039</guid>
		<description>@Mirror

I think if you save with commit=False, you don&#039;t get an id, in which case that won&#039;t help.

It&#039;s not as nice as just saving the form, but the way to do it would be pass just &lt;code&gt;request.POST&lt;/code&gt; (not &lt;code&gt;request.FILES&lt;/code&gt;) to the form, then save the file from &lt;code&gt;request.FILES&lt;/code&gt; manually.

That&#039;s the gist of it.  Sorry, I don&#039;t have any code to share - when I did it the situation was different.</description>
		<content:encoded><![CDATA[<p>@Mirror</p>
<p>I think if you save with commit=False, you don&#8217;t get an id, in which case that won&#8217;t help.</p>
<p>It&#8217;s not as nice as just saving the form, but the way to do it would be pass just <code>request.POST</code> (not <code>request.FILES</code>) to the form, then save the file from <code>request.FILES</code> manually.</p>
<p>That&#8217;s the gist of it.  Sorry, I don&#8217;t have any code to share &#8211; when I did it the situation was different.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mirror</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1038</link>
		<dc:creator>Mirror</dc:creator>
		<pubDate>Mon, 16 May 2011 16:32:56 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1038</guid>
		<description>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.</description>
		<content:encoded><![CDATA[<p>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.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mirror</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1037</link>
		<dc:creator>Mirror</dc:creator>
		<pubDate>Mon, 16 May 2011 16:29:18 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1037</guid>
		<description>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:
&lt;pre&gt;
class TreeForm(forms.ModelForm):
   class Meta:
      model = TreeModel
      fields = (&#039;title&#039;, &#039;pic&#039;)
&lt;/pre&gt;
So in my views, I create a form by doing:
&lt;pre&gt;
   if request == &quot;POST&quot;:
      treeform = TreeForm(request.POST, request.FILES)
      ....

      if treeform.is_valid():
         new_tree = treeform.save()
         ....
&lt;/pre&gt;
Here, saving the pic (through upload_to) will be bundled with saving the entire TreeModel, so id will be &#039;None&#039; at this point. How would you separately save the picture if the pic is a field bound to the TreeModel you&#039;re already saving? Because when the user clicks &#039;save&#039; on the form, it&#039;s going to run through saving the pic as it saves the other fields in TreeModel. 

One solution would be to use cleaned_data:
&lt;pre&gt;
     pic = treeform.cleaned_data[&#039;pic&#039;]
     new_tree = treeform.save(commit=False)          &lt;=== to ensure that I don&#039;t save the image yet because this will produce id=None
     new_tree.pic = pic
     new_tree.save()           &lt;== id shouldn&#039;t be None here, I think
&lt;/pre&gt;
second solution would be to modify how save() works in TreeModel, but I&#039;m not sure how that would work.</description>
		<content:encoded><![CDATA[<p>Thanks, but can you further clarify on some confusion:</p>
<p>How would I save the image after I save the Tree model? I have a model form like so:</p>
<pre>
class TreeForm(forms.ModelForm):
   class Meta:
      model = TreeModel
      fields = ('title', 'pic')
</pre>
<p>So in my views, I create a form by doing:</p>
<pre>
   if request == "POST":
      treeform = TreeForm(request.POST, request.FILES)
      ....

      if treeform.is_valid():
         new_tree = treeform.save()
         ....
</pre>
<p>Here, saving the pic (through upload_to) will be bundled with saving the entire TreeModel, so id will be &#8216;None&#8217; at this point. How would you separately save the picture if the pic is a field bound to the TreeModel you&#8217;re already saving? Because when the user clicks &#8217;save&#8217; on the form, it&#8217;s going to run through saving the pic as it saves the other fields in TreeModel. </p>
<p>One solution would be to use cleaned_data:</p>
<pre>
     pic = treeform.cleaned_data['pic']
     new_tree = treeform.save(commit=False)          &lt;=== to ensure that I don&#039;t save the image yet because this will produce id=None
     new_tree.pic = pic
     new_tree.save()           &lt;== id shouldn&#039;t be None here, I think
</pre>
<p>second solution would be to modify how save() works in TreeModel, but I&#039;m not sure how that would work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1035</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Mon, 16 May 2011 10:21:33 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1035</guid>
		<description>@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&#039;t work in admin though.</description>
		<content:encoded><![CDATA[<p>@Mirror</p>
<p>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&#8217;t work in admin though.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mirror</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1033</link>
		<dc:creator>Mirror</dc:creator>
		<pubDate>Mon, 16 May 2011 04:03:13 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1033</guid>
		<description>Sorry, my example didn&#039;t show up correctly. I mean to display this as the scheme:

mysite.com/id/slug/pic.png</description>
		<content:encoded><![CDATA[<p>Sorry, my example didn&#8217;t show up correctly. I mean to display this as the scheme:</p>
<p>mysite.com/id/slug/pic.png</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mirror</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1032</link>
		<dc:creator>Mirror</dc:creator>
		<pubDate>Mon, 16 May 2011 04:01:55 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1032</guid>
		<description>You&#039;ve noted that &#039;instance.id&#039; 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&#039;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 &#039;id&#039; is the id of the particular Tree model that holds the ImageField and &#039;slug&#039; is the Model&#039;s title:
&lt;pre&gt;
class Tree(model.Models):
    def upload_image(self, filename):
         return &#039;uploads/%s/%s/%s&#039; % (self.id, slugify(self.title), filename)

    title = models.CharField(......)
    pic = models.ImageField(upload_to=&#039;upload_image&#039;)
&lt;/pre&gt;
As you already mentioned, &#039;id&#039; won&#039;t work anymore since id has not been created, so in the end I receive &#039;None&#039; as the &#039;id&#039;. Is there a way around this?</description>
		<content:encoded><![CDATA[<p>You&#8217;ve noted that &#8216;instance.id&#8217; 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&#8217;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:</p>
<p>mysite.com///pic.png</p>
<p>where &#8216;id&#8217; is the id of the particular Tree model that holds the ImageField and &#8217;slug&#8217; is the Model&#8217;s title:</p>
<pre>
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')
</pre>
<p>As you already mentioned, &#8216;id&#8217; won&#8217;t work anymore since id has not been created, so in the end I receive &#8216;None&#8217; as the &#8216;id&#8217;. Is there a way around this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Andrews</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-1031</link>
		<dc:creator>Matt Andrews</dc:creator>
		<pubDate>Sun, 15 May 2011 12:31:38 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-1031</guid>
		<description>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:

&lt;pre&gt;
def get_media_path(instance, filename):
    return os.path.join(&#039;media&#039;, 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(&#039;media&#039;, str(instance.media_type.type_slug), filename)
&lt;/pre&gt;
Just thought this might help someone.</description>
		<content:encoded><![CDATA[<p>Thanks for this, really helpful.</p>
<p>I needed to use a field from a different model &#8211; 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:</p>
<pre>
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)
</pre>
<p>Just thought this might help someone.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-996</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Wed, 09 Feb 2011 09:41:40 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-996</guid>
		<description>@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 &lt;a href=&quot;http://docs.djangoproject.com/en/dev/topics/db/aggregation/&quot; rel=&quot;nofollow&quot;&gt;aggregation&lt;/a&gt; something like &lt;code&gt;Photo.objects.aggregate(Max(&#039;id&#039;))[&#039;id__max&#039;]&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>@Eric</p>
<p>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.</p>
<p>Instead of iterating through the queryset, perhaps you could use <a href="http://docs.djangoproject.com/en/dev/topics/db/aggregation/" rel="nofollow">aggregation</a> something like <code>Photo.objects.aggregate(Max('id'))['id__max']</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric</title>
		<link>http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/comment-page-1/#comment-995</link>
		<dc:creator>Eric</dc:creator>
		<pubDate>Tue, 08 Feb 2011 20:15:28 +0000</pubDate>
		<guid isPermaLink="false">http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/#comment-995</guid>
		<description>Hello everyone,

What do you think about this little hack :

&lt;pre&gt;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(&#039;photos&#039;, id, filename)&lt;/pre&gt;

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</description>
		<content:encoded><![CDATA[<p>Hello everyone,</p>
<p>What do you think about this little hack :</p>
<pre>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)</pre>
<p>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</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.228 seconds -->

