<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.3" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Uploading images to a dynamic path with Django</title>
	<link>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/</link>
	<description>Code and comments on web development, Django, Python and things (un)related.</description>
	<pubDate>Mon, 06 Oct 2008 13:04:06 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.3</generator>
		<item>
		<title>By: Scott</title>
		<link>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-301</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Mon, 25 Aug 2008 21:48:30 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-301</guid>
		<description>@Fidel Ramos

I have a new short post with an example of how to get dynamic upload paths since the FileStorageRefactor merge.  It uses a callable in the upload_to parameter and is much simpler than the method in this post.

See:
&lt;a href="http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/" rel="nofollow"&gt;Dynamic upload paths in Django&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>@Fidel Ramos</p>
<p>I have a new short post with an example of how to get dynamic upload paths since the FileStorageRefactor merge.  It uses a callable in the upload_to parameter and is much simpler than the method in this post.</p>
<p>See:<br />
<a href="http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/" rel="nofollow">Dynamic upload paths in Django</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fidel Ramos</title>
		<link>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-292</link>
		<dc:creator>Fidel Ramos</dc:creator>
		<pubDate>Thu, 21 Aug 2008 11:23:02 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-292</guid>
		<description>This solution was working fine until the File Storage refactoring (http://code.djangoproject.com/wiki/FileStorageRefactor), and I'm afraid the changes to make it work again may be extensive. Anyone knows a File Storage-compatible substitute?</description>
		<content:encoded><![CDATA[<p>This solution was working fine until the File Storage refactoring (http://code.djangoproject.com/wiki/FileStorageRefactor), and I&#8217;m afraid the changes to make it work again may be extensive. Anyone knows a File Storage-compatible substitute?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mat</title>
		<link>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-288</link>
		<dc:creator>mat</dc:creator>
		<pubDate>Fri, 08 Aug 2008 10:04:19 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-288</guid>
		<description>Because of http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges#Signalrefactoring I guess this is gonna look like this:

	def contribute_to_class(self, cls, name):
		"""
		Hook up events so we can access the instance.
		"""
		super(CustomImageField, self).contribute_to_class(cls, name)
		if self.prime_upload:
			signals.post_init.connect(self._get_upload_to, sender=cls)
		signals.pre_save.connect(self._get_upload_to, sender=cls)

	def _get_upload_to(self, **kwargs):
		"""
		Get dynamic upload_to value from the model instance.
		"""
		instance = kwargs['instance']
		if hasattr(instance, 'get_upload_to'):
			self.upload_to = instance.get_upload_to(self.attname)</description>
		<content:encoded><![CDATA[<p>Because of <a href="http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges#Signalrefactoring" rel="nofollow">http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges#Signalrefactoring</a> I guess this is gonna look like this:</p>
<p>	def contribute_to_class(self, cls, name):<br />
		&#8220;&#8221;"<br />
		Hook up events so we can access the instance.<br />
		&#8220;&#8221;"<br />
		super(CustomImageField, self).contribute_to_class(cls, name)<br />
		if self.prime_upload:<br />
			signals.post_init.connect(self._get_upload_to, sender=cls)<br />
		signals.pre_save.connect(self._get_upload_to, sender=cls)</p>
<p>	def _get_upload_to(self, **kwargs):<br />
		&#8220;&#8221;"<br />
		Get dynamic upload_to value from the model instance.<br />
		&#8220;&#8221;"<br />
		instance = kwargs[&#8217;instance&#8217;]<br />
		if hasattr(instance, &#8216;get_upload_to&#8217;):<br />
			self.upload_to = instance.get_upload_to(self.attname)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-287</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Wed, 06 Aug 2008 04:02:12 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-287</guid>
		<description>In an effort to get this going with newforms-admin I tried working with the &lt;code&gt;post_save&lt;/code&gt; signal.  I've posted it on my blog:
http://pandemoniumillusion.wordpress.com/2008/08/06/django-imagefield-and-filefield-dynamic-upload-path-in-newforms-admin/</description>
		<content:encoded><![CDATA[<p>In an effort to get this going with newforms-admin I tried working with the <code>post_save</code> signal.  I&#8217;ve posted it on my blog:<br />
<a href="http://pandemoniumillusion.wordpress.com/2008/08/06/django-imagefield-and-filefield-dynamic-upload-path-in-newforms-admin/" rel="nofollow">http://pandemoniumillusion.wordpress.com/2008/08/06/django-imagefield-and-filefield-dynamic-upload-path-in-newforms-admin/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-284</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Tue, 05 Aug 2008 02:38:45 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-284</guid>
		<description>Great Post! I've used this for a while now.
However, this seems to have broken with the inclusion of the Newforms Admin now. I'm getting a DoesNotExist error on the instance. Have you tried the newest revision?</description>
		<content:encoded><![CDATA[<p>Great Post! I&#8217;ve used this for a while now.<br />
However, this seems to have broken with the inclusion of the Newforms Admin now. I&#8217;m getting a DoesNotExist error on the instance. Have you tried the newest revision?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-205</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Tue, 20 May 2008 12:02:51 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-205</guid>
		<description>@Michael

I've just posted an updated version (see above) which should work in this situation.</description>
		<content:encoded><![CDATA[<p>@Michael</p>
<p>I&#8217;ve just posted an updated version (see above) which should work in this situation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-204</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Tue, 20 May 2008 04:55:40 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-204</guid>
		<description>The CustomImageField class doesn't properly save the image to the specified dynamic path via the following code:

        if form.is_valid():
            l = ListingImage()

            uploadedImage = form.cleaned_data['image']   # an UploadedFile object
            l.save_image_file(uploadedImage.filename, uploadedImage.content)

            l.caption = form.cleaned_data['caption']
            l.sort = form.cleaned_data['sort']
            l.save()
            

ListingImage.image is a CustomImageField()

Anyone know why calling save_image_file() doesn't invoke the get_upload_to() to save the image to the dynamic path?</description>
		<content:encoded><![CDATA[<p>The CustomImageField class doesn&#8217;t properly save the image to the specified dynamic path via the following code:</p>
<p>        if form.is_valid():<br />
            l = ListingImage()</p>
<p>            uploadedImage = form.cleaned_data[&#8217;image&#8217;]   # an UploadedFile object<br />
            l.save_image_file(uploadedImage.filename, uploadedImage.content)</p>
<p>            l.caption = form.cleaned_data[&#8217;caption&#8217;]<br />
            l.sort = form.cleaned_data[&#8217;sort&#8217;]<br />
            l.save()</p>
<p>ListingImage.image is a CustomImageField()</p>
<p>Anyone know why calling save_image_file() doesn&#8217;t invoke the get_upload_to() to save the image to the dynamic path?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-199</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Fri, 02 May 2008 07:40:06 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-199</guid>
		<description>Hi Chris.

Yes, you can.  The model that contains the &lt;code&gt;CustomImageField&lt;/code&gt; gets to build up the path.  Assuming you have a &lt;code&gt;Photo&lt;/code&gt; model which has a &lt;code&gt;CustomImageField&lt;/code&gt; and is related to a user, you could do something like:

&lt;pre&gt;class Photo(models.Model):
    user = models.ForeignKey(User, related_name='photos')
    image = CustomImageField(upload_to='photos')
    creation_date = models.DateTimeField()
    ...
    
    def get_upload_to(self, field_attname):
        return self.user.username
&lt;/pre&gt;

The upload path will be made up of the media path, username and filename.  Assuming the username is "ted", you will get something like: &lt;code&gt;/path/to/media/ted/head.jpg&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Hi Chris.</p>
<p>Yes, you can.  The model that contains the <code>CustomImageField</code> gets to build up the path.  Assuming you have a <code>Photo</code> model which has a <code>CustomImageField</code> and is related to a user, you could do something like:</p>
<pre>class Photo(models.Model):
    user = models.ForeignKey(User, related_name='photos')
    image = CustomImageField(upload_to='photos')
    creation_date = models.DateTimeField()
    ...

    def get_upload_to(self, field_attname):
        return self.user.username
</pre>
<p>The upload path will be made up of the media path, username and filename.  Assuming the username is &#8220;ted&#8221;, you will get something like: <code>/path/to/media/ted/head.jpg</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chris rockwell</title>
		<link>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-198</link>
		<dc:creator>chris rockwell</dc:creator>
		<pubDate>Thu, 01 May 2008 22:16:36 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-198</guid>
		<description>This is great..  but I have a question.  I want to save all uploads for a user in a folder just for them:  Example - /path/to/media/Ted/head.jpg  or with user id - /path/to/media/1/head.jpg

Is there anyway to do this?</description>
		<content:encoded><![CDATA[<p>This is great..  but I have a question.  I want to save all uploads for a user in a folder just for them:  Example - /path/to/media/Ted/head.jpg  or with user id - /path/to/media/1/head.jpg</p>
<p>Is there anyway to do this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jose</title>
		<link>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-175</link>
		<dc:creator>Jose</dc:creator>
		<pubDate>Tue, 01 Apr 2008 20:05:34 +0000</pubDate>
		<guid>http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/#comment-175</guid>
		<description>Scott !...you are the man ;-). Thank you very much !
This is my first experience with Python and i'm really enjoying this stuff (it's really interesting, i'm java/php developer, and sometimes i do some other stuff, but never with Python).

To find people that helps - like you - it's really a good sign when i have to decide to continue with a project on python or not. :-).

Thanks again</description>
		<content:encoded><![CDATA[<p>Scott !&#8230;you are the man ;-). Thank you very much !<br />
This is my first experience with Python and i&#8217;m really enjoying this stuff (it&#8217;s really interesting, i&#8217;m java/php developer, and sometimes i do some other stuff, but never with Python).</p>
<p>To find people that helps - like you - it&#8217;s really a good sign when i have to decide to continue with a project on python or not. :-).</p>
<p>Thanks again</p>
]]></content:encoded>
	</item>
</channel>
</rss>

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