<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Red Robot Studios &#187; Django</title>
	<atom:link href="http://www.redrobotstudios.com/blog/tag/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.redrobotstudios.com/blog</link>
	<description>Web development company specialising in Django and mobile development for clients around the world</description>
	<lastBuildDate>Mon, 09 Aug 2010 11:29:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Don&#8217;t delete an image file when deleting a Django model instance</title>
		<link>http://www.redrobotstudios.com/blog/2010/03/02/dont-delete-image-file-when-deleting-django-model-instance/</link>
		<comments>http://www.redrobotstudios.com/blog/2010/03/02/dont-delete-image-file-when-deleting-django-model-instance/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 09:54:22 +0000</pubDate>
		<dc:creator>Scott Barnham</dc:creator>
				<category><![CDATA[Django]]></category>

		<guid isPermaLink="false">http://www.redrobotstudios.com/blog/?p=55</guid>
		<description><![CDATA[If you have a Django model with a FileField or ImageField, when you delete the model instance, the associated file or image is also deleted.  In most cases this is desirable and keeps things tidy, but I had a situation recently where the image file should not be deleted when the model was deleted. [...]]]></description>
			<content:encoded><![CDATA[<p>If you have a Django model with a <code>FileField</code> or <code>ImageField</code>, when you delete the model instance, the associated file or image is also deleted.  In most cases this is desirable and keeps things tidy, but I had a situation recently where the image file should not be deleted when the model was deleted.  Here&#8217;s a simple way to override the default behaviour.</p>
<h3>Custom file storage</h3>
<p>Django uses storage classes to determine how files are read and written.  Normally, the data is just written as files to disk, but there are other possibilities such as storing on remote servers.</p>
<p>It&#8217;s easy to write a <a href="http://docs.djangoproject.com/en/dev/howto/custom-file-storage/">custom file storage</a> class to override the behaviour of the default <code>FileStorageSystem</code>.  In this case, we only need to change the <code>delete</code> method so it does not delete the file.</p>
<p>In custom.py</p>
<pre>from django.core.files import storage

class NoDeleteFileStorage(storage.FileSystemStorage):
    def delete(self, name):
        pass</pre>
<p>We can then <a href="http://docs.djangoproject.com/en/1.1/topics/files/#file-storage">use the custom file storage</a> by making an instance and passing it to the <code>ImageField</code>.</p>
<p>In models.py</p>
<pre>from custom import NoDeleteFileStorage

ndfs = NoDeleteFileStorage()

class ImageInstance(models.Model):
    image = models.ImageField(storage=ndfs, ...)</pre>
<p>It&#8217;s as simple as that!  Custom file storage has some interesting possibilities.  With it you can handle how files are named or integrate with some caching or <a href="http://en.wikipedia.org/wiki/Content_Delivery_Network">CDN</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.redrobotstudios.com/blog/2010/03/02/dont-delete-image-file-when-deleting-django-model-instance/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
