<!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>Using MongoEngine with Django — MongoEngine 0.7.9 documentation</title> <link rel="stylesheet" href="_static/nature.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '', VERSION: '0.7.9', 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="MongoEngine 0.7.9 documentation" href="index.html" /> <link rel="next" title="Changelog" href="changelog.html" /> <link rel="prev" title="API Reference" href="apireference.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="changelog.html" title="Changelog" accesskey="N">next</a> |</li> <li class="right" > <a href="apireference.html" title="API Reference" accesskey="P">previous</a> |</li> <li><a href="index.html">MongoEngine 0.7.9 documentation</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="using-mongoengine-with-django"> <h1>Using MongoEngine with Django<a class="headerlink" href="#using-mongoengine-with-django" title="Permalink to this headline">¶</a></h1> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Updated to support Django 1.4</p> </div> <div class="section" id="connecting"> <h2>Connecting<a class="headerlink" href="#connecting" title="Permalink to this headline">¶</a></h2> <p>In your <strong>settings.py</strong> file, ignore the standard database settings (unless you also plan to use the ORM in your project), and instead call <a class="reference internal" href="apireference.html#mongoengine.connect" title="mongoengine.connect"><tt class="xref py py-func docutils literal"><span class="pre">connect()</span></tt></a> somewhere in the settings module.</p> </div> <div class="section" id="authentication"> <h2>Authentication<a class="headerlink" href="#authentication" title="Permalink to this headline">¶</a></h2> <p>MongoEngine includes a Django authentication backend, which uses MongoDB. The <tt class="xref py py-class docutils literal"><span class="pre">User</span></tt> model is a MongoEngine <a class="reference internal" href="apireference.html#mongoengine.Document" title="mongoengine.Document"><tt class="xref py py-class docutils literal"><span class="pre">Document</span></tt></a>, but implements most of the methods and attributes that the standard Django <tt class="xref py py-class docutils literal"><span class="pre">User</span></tt> model does - so the two are moderately compatible. Using this backend will allow you to store users in MongoDB but still use many of the Django authentication infrastucture (such as the <tt class="xref py py-func docutils literal"><span class="pre">login_required()</span></tt> decorator and the <tt class="xref py py-func docutils literal"><span class="pre">authenticate()</span></tt> function). To enable the MongoEngine auth backend, add the following to you <strong>settings.py</strong> file:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">AUTHENTICATION_BACKENDS</span> <span class="o">=</span> <span class="p">(</span> <span class="s">'mongoengine.django.auth.MongoEngineBackend'</span><span class="p">,</span> <span class="p">)</span> </pre></div> </div> <p>The <tt class="xref py py-mod docutils literal"><span class="pre">auth</span></tt> module also contains a <tt class="xref py py-func docutils literal"><span class="pre">get_user()</span></tt> helper function, that takes a user’s <tt class="xref py py-attr docutils literal"><span class="pre">id</span></tt> and returns a <tt class="xref py py-class docutils literal"><span class="pre">User</span></tt> object.</p> <p class="versionadded"> <span class="versionmodified">New in version 0.1.3.</span></p> </div> <div class="section" id="sessions"> <h2>Sessions<a class="headerlink" href="#sessions" title="Permalink to this headline">¶</a></h2> <p>Django allows the use of different backend stores for its sessions. MongoEngine provides a MongoDB-based session backend for Django, which allows you to use sessions in you Django application with just MongoDB. To enable the MongoEngine session backend, ensure that your settings module has <tt class="docutils literal"><span class="pre">'django.contrib.sessions.middleware.SessionMiddleware'</span></tt> in the <tt class="docutils literal"><span class="pre">MIDDLEWARE_CLASSES</span></tt> field and <tt class="docutils literal"><span class="pre">'django.contrib.sessions'</span></tt> in your <tt class="docutils literal"><span class="pre">INSTALLED_APPS</span></tt>. From there, all you need to do is add the following line into you settings module:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">SESSION_ENGINE</span> <span class="o">=</span> <span class="s">'mongoengine.django.sessions'</span> </pre></div> </div> <p class="versionadded"> <span class="versionmodified">New in version 0.2.1.</span></p> </div> <div class="section" id="storage"> <h2>Storage<a class="headerlink" href="#storage" title="Permalink to this headline">¶</a></h2> <p>With MongoEngine’s support for GridFS via the <a class="reference internal" href="apireference.html#mongoengine.FileField" title="mongoengine.FileField"><tt class="xref py py-class docutils literal"><span class="pre">FileField</span></tt></a>, it is useful to have a Django file storage backend that wraps this. The new storage module is called <tt class="xref py py-class docutils literal"><span class="pre">GridFSStorage</span></tt>. Using it is very similar to using the default FileSystemStorage.:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">mongoengine.django.storage</span> <span class="kn">import</span> <span class="n">GridFSStorage</span> <span class="n">fs</span> <span class="o">=</span> <span class="n">GridFSStorage</span><span class="p">()</span> <span class="n">filename</span> <span class="o">=</span> <span class="n">fs</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="s">'hello.txt'</span><span class="p">,</span> <span class="s">'Hello, World!'</span><span class="p">)</span> </pre></div> </div> <p>All of the <a class="reference external" href="http://docs.djangoproject.com/en/dev/ref/files/storage/">Django Storage API methods</a> have been implemented except <tt class="xref py py-func docutils literal"><span class="pre">path()</span></tt>. If the filename provided already exists, an underscore and a number (before # the file extension, if one exists) will be appended to the filename until the generated filename doesn’t exist. The <tt class="xref py py-func docutils literal"><span class="pre">save()</span></tt> method will return the new filename.:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">fs</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s">'hello.txt'</span><span class="p">)</span> <span class="go">True</span> <span class="gp">>>> </span><span class="n">fs</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">'hello.txt'</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="go">'Hello, World!'</span> <span class="gp">>>> </span><span class="n">fs</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="s">'hello.txt'</span><span class="p">)</span> <span class="go">13</span> <span class="gp">>>> </span><span class="n">fs</span><span class="o">.</span><span class="n">url</span><span class="p">(</span><span class="s">'hello.txt'</span><span class="p">)</span> <span class="go">'http://your_media_url/hello.txt'</span> <span class="gp">>>> </span><span class="n">fs</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">'hello.txt'</span><span class="p">)</span><span class="o">.</span><span class="n">name</span> <span class="go">'hello.txt'</span> <span class="gp">>>> </span><span class="n">fs</span><span class="o">.</span><span class="n">listdir</span><span class="p">()</span> <span class="go">([], [u'hello.txt'])</span> </pre></div> </div> <p>All files will be saved and retrieved in GridFS via the :class::<cite>FileDocument</cite> document, allowing easy access to the files without the GridFSStorage backend.:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">mongoengine.django.storage</span> <span class="kn">import</span> <span class="n">FileDocument</span> <span class="gp">>>> </span><span class="n">FileDocument</span><span class="o">.</span><span class="n">objects</span><span class="p">()</span> <span class="go">[<FileDocument: FileDocument object>]</span> </pre></div> </div> <p class="versionadded"> <span class="versionmodified">New in version 0.4.</span></p> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="index.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Using MongoEngine with Django</a><ul> <li><a class="reference internal" href="#connecting">Connecting</a></li> <li><a class="reference internal" href="#authentication">Authentication</a></li> <li><a class="reference internal" href="#sessions">Sessions</a></li> <li><a class="reference internal" href="#storage">Storage</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="apireference.html" title="previous chapter">API Reference</a></p> <h4>Next topic</h4> <p class="topless"><a href="changelog.html" title="next chapter">Changelog</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/django.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> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="changelog.html" title="Changelog" >next</a> |</li> <li class="right" > <a href="apireference.html" title="API Reference" >previous</a> |</li> <li><a href="index.html">MongoEngine 0.7.9 documentation</a> »</li> </ul> </div> <div class="footer"> © Copyright 2009-2012, MongoEngine Authors. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>