Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 65530c6176058f9b54858c3b4f6385e6 > files > 607

python-django-doc-1.8.19-1.mga6.noarch.rpm

<!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" lang="">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Writing a custom storage system &#8212; Django 1.8.19 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.8.19',
        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="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="top" title="Django 1.8.19 documentation" href="../contents.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 role="document">

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 1.8.19 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">
    &laquo; <a href="custom-template-tags.html" title="Custom template tags and filters">previous</a>
     |
    <a href="index.html" title="&amp;#8220;How-to&amp;#8221; guides" accesskey="U">up</a>
   |
    <a href="deployment/index.html" title="Deploying Django">next</a> &raquo;</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 &#8211; a common example is storing files
on some remote system &#8211; you can do so by defining a custom storage class.
You&#8217;ll need to follow these steps:</p>
<ol class="arabic">
<li><p class="first">Your custom storage system must be a subclass of
<code class="docutils literal"><span class="pre">django.core.files.storage.Storage</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core.files.storage</span> <span class="k">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 <code class="docutils literal"><span class="pre">django.conf.settings</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.core.files.storage</span> <span class="k">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="kc">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 <a class="reference internal" href="#django.core.files.storage._open" title="django.core.files.storage._open"><code class="xref py py-meth docutils literal"><span class="pre">_open()</span></code></a> and <a class="reference internal" href="#django.core.files.storage._save" title="django.core.files.storage._save"><code class="xref py py-meth docutils literal"><span class="pre">_save()</span></code></a>
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 <code class="docutils literal"><span class="pre">path()</span></code> method.</p>
</li>
<li><p class="first">Your storage class must be <a class="reference internal" href="../topics/migrations.html#custom-deconstruct-method"><span class="std std-ref">deconstructible</span></a>
so it can be serialized when it&#8217;s used on a field in a migration. As long
as your field has arguments that are themselves
<a class="reference internal" href="../topics/migrations.html#migration-serializing"><span class="std std-ref">serializable</span></a>, you can use the
<code class="docutils literal"><span class="pre">django.utils.deconstruct.deconstructible</span></code> class decorator for this
(that&#8217;s what Django uses on FileSystemStorage).</p>
</li>
</ol>
<p>By default, the following methods raise <cite>NotImplementedError</cite> and will
typically have to be overridden:</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"><code class="xref py py-meth docutils literal"><span class="pre">Storage.delete()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">Storage.exists()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">Storage.listdir()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">Storage.size()</span></code></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"><code class="xref py py-meth docutils literal"><span class="pre">Storage.url()</span></code></a></li>
</ul>
<p>Note however that not all these methods are required and may be deliberately
omitted. As it happens, it is possible to leave each method unimplemented and
still have a working Storage.</p>
<p>By way of example, if listing the contents of certain storage backends turns
out to be expensive, you might decide not to implement <cite>Storage.listdir</cite>.</p>
<p>Another example would be a backend that only handles writing to files. In this
case, you would not need to implement any of the above methods.</p>
<p>Ultimately, which of these methods are implemented is up to you. Leaving some
methods unimplemented will result in a partial (possibly broken) interface.</p>
<p>You&#8217;ll also usually want to use hooks specifically designed for custom storage
objects. These are:</p>
<dl class="method">
<dt id="django.core.files.storage._open">
<code class="descname">_open</code>(<em>name</em>, <em>mode='rb'</em>)<a class="headerlink" href="#django.core.files.storage._open" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p><strong>Required</strong>.</p>
<p>Called by <code class="docutils literal"><span class="pre">Storage.open()</span></code>, this is the actual mechanism the storage class
uses to open the file. This must return a <code class="docutils literal"><span class="pre">File</span></code> object, though in most cases,
you&#8217;ll want to return some subclass here that implements logic specific to the
backend storage system.</p>
<dl class="method">
<dt id="django.core.files.storage._save">
<code class="descname">_save</code>(<em>name</em>, <em>content</em>)<a class="headerlink" href="#django.core.files.storage._save" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Called by <code class="docutils literal"><span class="pre">Storage.save()</span></code>. The <code class="docutils literal"><span class="pre">name</span></code> will already have gone through
<code class="docutils literal"><span class="pre">get_valid_name()</span></code> and <code class="docutils literal"><span class="pre">get_available_name()</span></code>, and the <code class="docutils literal"><span class="pre">content</span></code> will be a
<code class="docutils literal"><span class="pre">File</span></code> object itself.</p>
<p>Should return the actual name of name of the file saved (usually the <code class="docutils literal"><span class="pre">name</span></code>
passed in, but if the storage needs to change the file name return the new name
instead).</p>
<dl class="method">
<dt id="django.core.files.storage.get_valid_name">
<code class="descname">get_valid_name</code>(<em>name</em>)<a class="headerlink" href="#django.core.files.storage.get_valid_name" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Returns a filename suitable for use with the underlying storage system. The
<code class="docutils literal"><span class="pre">name</span></code> 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 <code class="docutils literal"><span class="pre">Storage</span></code> retains only alpha-numeric characters, periods
and underscores from the original filename, removing everything else.</p>
<dl class="method">
<dt id="django.core.files.storage.get_available_name">
<code class="descname">get_available_name</code>(<em>name</em>, <em>max_length=None</em>)<a class="headerlink" href="#django.core.files.storage.get_available_name" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>Returns a filename that is available in the storage mechanism, possibly taking
the provided filename into account. The <code class="docutils literal"><span class="pre">name</span></code> argument passed to this method
will have already cleaned to a filename valid for the storage system, according
to the <code class="docutils literal"><span class="pre">get_valid_name()</span></code> method described above.</p>
<p>The length of the filename will not exceed <code class="docutils literal"><span class="pre">max_length</span></code>, if provided. If a
free unique filename cannot be found, a <a class="reference internal" href="../ref/exceptions.html#django.core.exceptions.SuspiciousOperation" title="django.core.exceptions.SuspiciousOperation"><code class="xref py py-exc docutils literal"><span class="pre">SuspiciousFileOperation</span></code></a> exception is raised.</p>
<p>If a file with <code class="docutils literal"><span class="pre">name</span></code> already exists, an underscore plus a random 7 character
alphanumeric string is appended to the filename before the extension.</p>
<div class="versionchanged">
<span class="title">Changed in Django 1.7:</span> <p>Previously, an underscore followed by a number (e.g. <code class="docutils literal"><span class="pre">&quot;_1&quot;</span></code>, <code class="docutils literal"><span class="pre">&quot;_2&quot;</span></code>,
etc.) was appended to the filename until an available name in the destination
directory was found. A malicious user could exploit this deterministic
algorithm to create a denial-of-service attack. This change was also made
in Django 1.6.6, 1.5.9, and 1.4.14.</p>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 1.8:</span> <p>The <code class="docutils literal"><span class="pre">max_length</span></code> argument was added.</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <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.8.19 documentation</a>
        
          <ul><li><a href="index.html">&#8220;How-to&#8221; guides</a>
        
        <ul><li>Writing a custom storage system</li></ul>
        </li></ul>
      </li>
  </ul>

  <div role="note" aria-label="source link">
    <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>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 10, 2018</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="custom-template-tags.html" title="Custom template tags and filters">previous</a>
     |
    <a href="index.html" title="&amp;#8220;How-to&amp;#8221; guides" accesskey="U">up</a>
   |
    <a href="deployment/index.html" title="Deploying Django">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>