<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Writing a custom storage system — Django 1.4.18 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '1.4.18', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="top" title="Django 1.4.18 documentation" href="../index.html" /> <link rel="up" title="“How-to” guides" href="index.html" /> <link rel="next" title="Deploying Django" href="deployment/index.html" /> <link rel="prev" title="Custom template tags and filters" href="custom-template-tags.html" /> <script type="text/javascript" src="../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../ref/templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../index.html">Django 1.4.18 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../index.html">Home</a> | <a title="Table of contents" href="../contents.html">Table of contents</a> | <a title="Global index" href="../genindex.html">Index</a> | <a title="Module index" href="../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="custom-template-tags.html" title="Custom template tags and filters">previous</a> | <a href="index.html" title="&#8220;How-to&#8221; guides" accesskey="U">up</a> | <a href="deployment/index.html" title="Deploying Django">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="howto-custom-file-storage"> <div class="section" id="s-writing-a-custom-storage-system"> <span id="writing-a-custom-storage-system"></span><h1>Writing a custom storage system<a class="headerlink" href="#writing-a-custom-storage-system" title="Permalink to this headline">¶</a></h1> <p>If you need to provide custom file storage – a common example is storing files on some remote system – you can do so by defining a custom storage class. You’ll need to follow these steps:</p> <ol class="arabic"> <li><p class="first">Your custom storage system must be a subclass of <tt class="docutils literal"><span class="pre">django.core.files.storage.Storage</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.core.files.storage</span> <span class="kn">import</span> <span class="n">Storage</span> <span class="k">class</span> <span class="nc">MyStorage</span><span class="p">(</span><span class="n">Storage</span><span class="p">):</span> <span class="o">...</span> </pre></div> </div> </li> <li><p class="first">Django must be able to instantiate your storage system without any arguments. This means that any settings should be taken from <tt class="docutils literal"><span class="pre">django.conf.settings</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span> <span class="kn">from</span> <span class="nn">django.core.files.storage</span> <span class="kn">import</span> <span class="n">Storage</span> <span class="k">class</span> <span class="nc">MyStorage</span><span class="p">(</span><span class="n">Storage</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">option</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">option</span><span class="p">:</span> <span class="n">option</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">CUSTOM_STORAGE_OPTIONS</span> <span class="o">...</span> </pre></div> </div> </li> <li><p class="first">Your storage class must implement the <tt class="docutils literal"><span class="pre">_open()</span></tt> and <tt class="docutils literal"><span class="pre">_save()</span></tt> methods, along with any other methods appropriate to your storage class. See below for more on these methods.</p> <p>In addition, if your class provides local file storage, it must override the <tt class="docutils literal"><span class="pre">path()</span></tt> method.</p> </li> </ol> <p>Your custom storage system may override any of the storage methods explained in <a class="reference internal" href="../ref/files/storage.html"><em>File storage API</em></a>, but you <strong>must</strong> implement the following methods:</p> <ul class="simple"> <li><a class="reference internal" href="../ref/files/storage.html#django.core.files.storage.Storage.delete" title="django.core.files.storage.Storage.delete"><tt class="xref py py-meth docutils literal"><span class="pre">Storage.delete()</span></tt></a></li> <li><a class="reference internal" href="../ref/files/storage.html#django.core.files.storage.Storage.exists" title="django.core.files.storage.Storage.exists"><tt class="xref py py-meth docutils literal"><span class="pre">Storage.exists()</span></tt></a></li> <li><a class="reference internal" href="../ref/files/storage.html#django.core.files.storage.Storage.listdir" title="django.core.files.storage.Storage.listdir"><tt class="xref py py-meth docutils literal"><span class="pre">Storage.listdir()</span></tt></a></li> <li><a class="reference internal" href="../ref/files/storage.html#django.core.files.storage.Storage.size" title="django.core.files.storage.Storage.size"><tt class="xref py py-meth docutils literal"><span class="pre">Storage.size()</span></tt></a></li> <li><a class="reference internal" href="../ref/files/storage.html#django.core.files.storage.Storage.url" title="django.core.files.storage.Storage.url"><tt class="xref py py-meth docutils literal"><span class="pre">Storage.url()</span></tt></a></li> </ul> <p>You’ll also usually want to use hooks specifically designed for custom storage objects. These are:</p> <div class="section" id="s-open-name-mode-rb"> <span id="open-name-mode-rb"></span><h2><tt class="docutils literal"><span class="pre">_open(name,</span> <span class="pre">mode='rb')</span></tt><a class="headerlink" href="#open-name-mode-rb" title="Permalink to this headline">¶</a></h2> <p><strong>Required</strong>.</p> <p>Called by <tt class="docutils literal"><span class="pre">Storage.open()</span></tt>, this is the actual mechanism the storage class uses to open the file. This must return a <tt class="docutils literal"><span class="pre">File</span></tt> object, though in most cases, you’ll want to return some subclass here that implements logic specific to the backend storage system.</p> </div> <div class="section" id="s-save-name-content"> <span id="save-name-content"></span><h2><tt class="docutils literal"><span class="pre">_save(name,</span> <span class="pre">content)</span></tt><a class="headerlink" href="#save-name-content" title="Permalink to this headline">¶</a></h2> <p>Called by <tt class="docutils literal"><span class="pre">Storage.save()</span></tt>. The <tt class="docutils literal"><span class="pre">name</span></tt> will already have gone through <tt class="docutils literal"><span class="pre">get_valid_name()</span></tt> and <tt class="docutils literal"><span class="pre">get_available_name()</span></tt>, and the <tt class="docutils literal"><span class="pre">content</span></tt> will be a <tt class="docutils literal"><span class="pre">File</span></tt> object itself.</p> <p>Should return the actual name of name of the file saved (usually the <tt class="docutils literal"><span class="pre">name</span></tt> passed in, but if the storage needs to change the file name return the new name instead).</p> </div> <div class="section" id="s-get-valid-name-name"> <span id="get-valid-name-name"></span><h2><tt class="docutils literal"><span class="pre">get_valid_name(name)</span></tt><a class="headerlink" href="#get-valid-name-name" title="Permalink to this headline">¶</a></h2> <p>Returns a filename suitable for use with the underlying storage system. The <tt class="docutils literal"><span class="pre">name</span></tt> argument passed to this method is the original filename sent to the server, after having any path information removed. Override this to customize how non-standard characters are converted to safe filenames.</p> <p>The code provided on <tt class="docutils literal"><span class="pre">Storage</span></tt> retains only alpha-numeric characters, periods and underscores from the original filename, removing everything else.</p> </div> <div class="section" id="s-get-available-name-name"> <span id="get-available-name-name"></span><h2><tt class="docutils literal"><span class="pre">get_available_name(name)</span></tt><a class="headerlink" href="#get-available-name-name" title="Permalink to this headline">¶</a></h2> <p>Returns a filename that is available in the storage mechanism, possibly taking the provided filename into account. The <tt class="docutils literal"><span class="pre">name</span></tt> argument passed to this method will have already cleaned to a filename valid for the storage system, according to the <tt class="docutils literal"><span class="pre">get_valid_name()</span></tt> method described above.</p> <div class="versionchanged"> <span class="title">Changed in Django 1.4.14:</span> <a class="reference internal" href="../releases/1.4.14.html"><em>Please see the release notes</em></a></div> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Writing a custom storage system</a><ul> <li><a class="reference internal" href="#open-name-mode-rb"><tt class="docutils literal"><span class="pre">_open(name,</span> <span class="pre">mode='rb')</span></tt></a></li> <li><a class="reference internal" href="#save-name-content"><tt class="docutils literal"><span class="pre">_save(name,</span> <span class="pre">content)</span></tt></a></li> <li><a class="reference internal" href="#get-valid-name-name"><tt class="docutils literal"><span class="pre">get_valid_name(name)</span></tt></a></li> <li><a class="reference internal" href="#get-available-name-name"><tt class="docutils literal"><span class="pre">get_available_name(name)</span></tt></a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="custom-template-tags.html">Custom template tags and filters</a></li> <li>Next: <a href="deployment/index.html">Deploying Django</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../index.html">Django 1.4.18 documentation</a> <ul><li><a href="index.html">“How-to” guides</a> <ul><li>Writing a custom storage system</li></ul> </li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/howto/custom-file-storage.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Jan 15, 2015</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="custom-template-tags.html" title="Custom template tags and filters">previous</a> | <a href="index.html" title="&#8220;How-to&#8221; guides" accesskey="U">up</a> | <a href="deployment/index.html" title="Deploying Django">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>