/usr/share/doc/python-django-mptt/html/tutorial.html is in python-django-mptt 0.5.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | <!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>Tutorial — django-mptt v0.5.2 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: '0.5.2',
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-mptt v0.5.2 documentation" href="index.html" />
<link rel="next" title="Models and Managers" href="models.html" />
<link rel="prev" title="Installation" href="install.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="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="models.html" title="Models and Managers"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="install.html" title="Installation"
accesskey="P">previous</a> |</li>
<li><a href="index.html">django-mptt v0.5.2 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="tutorial">
<h1>Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h1>
<div class="section" id="the-problem">
<h2>The Problem<a class="headerlink" href="#the-problem" title="Permalink to this headline">¶</a></h2>
<p>You’ve created a Django project, and you need to manage some hierarchical data. For instance you’ve got a bunch of hierarchical pages in a CMS, and sometimes pages are <em>chidren</em> of other pages</p>
<p>Now suppose you want to show a breadcrumb on your site, like this:</p>
<div class="highlight-python"><pre>Home > Products > Food > Meat > Spam > Spammy McDelicious</pre>
</div>
<p>To get all those page titles you might do something like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">titles</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">while</span> <span class="n">page</span><span class="p">:</span>
<span class="n">titles</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">page</span><span class="o">.</span><span class="n">title</span><span class="p">)</span>
<span class="n">page</span> <span class="o">=</span> <span class="n">page</span><span class="o">.</span><span class="n">parent</span>
</pre></div>
</div>
<p>That’s one database query for each page in the breadcrumb, and database queries are slow. Let’s do this a better way.</p>
</div>
<div class="section" id="the-solution">
<h2>The Solution<a class="headerlink" href="#the-solution" title="Permalink to this headline">¶</a></h2>
<p>Modified Preorder Tree Traversal can be a bit daunting at first, but it’s one of the best ways to solve this problem.</p>
<p>If you want to go into the details, there’s a good explanation here: <a class="reference external" href="http://www.sitepoint.com/hierarchical-data-database/">Storing Hierarchical Data in a Database</a></p>
<dl class="docutils">
<dt>tl;dr: MPTT makes most tree operations much cheaper in terms of queries. In fact all these operations take at most one query, and sometimes zero:</dt>
<dd><ul class="first last simple">
<li>get descendants of a node</li>
<li>get ancestors of a node</li>
<li>get all nodes at a given level</li>
<li>get leaf nodes</li>
</ul>
</dd>
<dt>And this one takes zero queries:</dt>
<dd><ul class="first last simple">
<li>count the descendants of a given node</li>
</ul>
</dd>
</dl>
<p>Enough intro. Let’s get started.</p>
</div>
<div class="section" id="getting-started">
<h2>Getting started<a class="headerlink" href="#getting-started" title="Permalink to this headline">¶</a></h2>
<div class="section" id="add-mptt-to-installed-apps">
<h3>Add <tt class="docutils literal"><span class="pre">mptt</span></tt> To <tt class="docutils literal"><span class="pre">INSTALLED_APPS</span></tt><a class="headerlink" href="#add-mptt-to-installed-apps" title="Permalink to this headline">¶</a></h3>
<p>As with most Django applications, you should add <tt class="docutils literal"><span class="pre">mptt</span></tt> to the <tt class="docutils literal"><span class="pre">INSTALLED_APPS</span></tt> in your <tt class="docutils literal"><span class="pre">settings.py</span></tt> file:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">INSTALLED_APPS</span> <span class="o">=</span> <span class="p">(</span>
<span class="s">'django.contrib.auth'</span><span class="p">,</span>
<span class="c"># ...</span>
<span class="s">'mptt'</span><span class="p">,</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="set-up-your-model">
<h3>Set up your model<a class="headerlink" href="#set-up-your-model" title="Permalink to this headline">¶</a></h3>
<p>Start with a basic subclass of MPTTModel, something like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">mptt.models</span> <span class="kn">import</span> <span class="n">MPTTModel</span><span class="p">,</span> <span class="n">TreeForeignKey</span>
<span class="k">class</span> <span class="nc">Genre</span><span class="p">(</span><span class="n">MPTTModel</span><span class="p">):</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">parent</span> <span class="o">=</span> <span class="n">TreeForeignKey</span><span class="p">(</span><span class="s">'self'</span><span class="p">,</span> <span class="n">null</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s">'children'</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">MPTTMeta</span><span class="p">:</span>
<span class="n">order_insertion_by</span> <span class="o">=</span> <span class="p">[</span><span class="s">'name'</span><span class="p">]</span>
</pre></div>
</div>
<p>You must define a parent field which is a <tt class="docutils literal"><span class="pre">TreeForeignKey</span></tt> to <tt class="docutils literal"><span class="pre">'self'</span></tt>. A <tt class="docutils literal"><span class="pre">TreeForeignKey</span></tt> is just a regular <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> that renders form fields differently in the admin and a few other places.</p>
<p>Because you’re inheriting from MPTTModel, your model will also have a number of
other fields: <tt class="docutils literal"><span class="pre">level</span></tt>, <tt class="docutils literal"><span class="pre">lft</span></tt>, <tt class="docutils literal"><span class="pre">rght</span></tt>, and <tt class="docutils literal"><span class="pre">tree_id</span></tt>. These fields are managed by the MPTT algorithm. Most of the time you won’t need to use these fields directly.</p>
<p>That <tt class="docutils literal"><span class="pre">MPTTMeta</span></tt> class adds some tweaks to <tt class="docutils literal"><span class="pre">django-mptt</span></tt> - in this case, just <tt class="docutils literal"><span class="pre">order_insertion_by</span></tt>. This indicates the natural ordering of the data in the tree.</p>
<p>Now create your table in the database:</p>
<div class="highlight-python"><pre>python manage.py syncdb</pre>
</div>
</div>
<div class="section" id="create-some-data">
<h3>Create some data<a class="headerlink" href="#create-some-data" title="Permalink to this headline">¶</a></h3>
<p>Fire up a django shell:</p>
<div class="highlight-python"><pre>python manage.py shell</pre>
</div>
<p>Now create some data to test:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">myapp.models</span> <span class="kn">import</span> <span class="n">Genre</span>
<span class="n">rock</span> <span class="o">=</span> <span class="n">Genre</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"Rock"</span><span class="p">)</span>
<span class="n">blues</span> <span class="o">=</span> <span class="n">Genre</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"Blues"</span><span class="p">)</span>
<span class="n">Genre</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"Hard Rock"</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="n">rock</span><span class="p">)</span>
<span class="n">Genre</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">"Pop Rock"</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="n">rock</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="make-a-view">
<h3>Make a view<a class="headerlink" href="#make-a-view" title="Permalink to this headline">¶</a></h3>
<p>This one’s pretty simple for now. Add this lightweight view to your <tt class="docutils literal"><span class="pre">views.py</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">show_genres</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="k">return</span> <span class="n">render_to_response</span><span class="p">(</span><span class="s">"genres.html"</span><span class="p">,</span>
<span class="p">{</span><span class="s">'nodes'</span><span class="p">:</span><span class="n">Genre</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()},</span>
<span class="n">context_instance</span><span class="o">=</span><span class="n">RequestContext</span><span class="p">(</span><span class="n">request</span><span class="p">))</span>
</pre></div>
</div>
<p>And add a URL for it in <tt class="docutils literal"><span class="pre">urls.py</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">(</span><span class="s">r'^genres/$'</span><span class="p">,</span> <span class="s">'myapp.views.show_genres'</span><span class="p">),</span>
</pre></div>
</div>
</div>
<div class="section" id="template">
<h3>Template<a class="headerlink" href="#template" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">django-mptt</span></tt> includes some template tags for making this bit easy too.
Create a template called <tt class="docutils literal"><span class="pre">genres.html</span></tt> in your template directory and put this in it:</p>
<div class="highlight-html+django"><div class="highlight"><pre><span class="cp">{%</span> <span class="k">load</span> <span class="nv">mptt_tags</span> <span class="cp">%}</span>
<span class="nt"><ul></span>
<span class="cp">{%</span> <span class="k">recursetree</span> <span class="nv">nodes</span> <span class="cp">%}</span>
<span class="nt"><li></span>
<span class="cp">{{</span> <span class="nv">node.name</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="nv">node.is_leaf_node</span> <span class="cp">%}</span>
<span class="nt"><ul</span> <span class="na">class=</span><span class="s">"children"</span><span class="nt">></span>
<span class="cp">{{</span> <span class="nv">children</span> <span class="cp">}}</span>
<span class="nt"></ul></span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="nt"></li></span>
<span class="cp">{%</span> <span class="k">endrecursetree</span> <span class="cp">%}</span>
<span class="nt"></ul></span>
</pre></div>
</div>
<p>That recursetree tag will recursively render that template fragment for all the nodes. Try it out by going to <tt class="docutils literal"><span class="pre">/genres/</span></tt>.</p>
<p>There’s more; <a class="reference external" href="http://django-mptt.github.com/django-mptt/">check out the docs</a> for custom admin-site stuff, more template tags, tree rebuild functions etc.</p>
<p>Now you can stop thinking about how to do trees, and start making a great django app!</p>
</div>
</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="#">Tutorial</a><ul>
<li><a class="reference internal" href="#the-problem">The Problem</a></li>
<li><a class="reference internal" href="#the-solution">The Solution</a></li>
<li><a class="reference internal" href="#getting-started">Getting started</a><ul>
<li><a class="reference internal" href="#add-mptt-to-installed-apps">Add <tt class="docutils literal"><span class="pre">mptt</span></tt> To <tt class="docutils literal"><span class="pre">INSTALLED_APPS</span></tt></a></li>
<li><a class="reference internal" href="#set-up-your-model">Set up your model</a></li>
<li><a class="reference internal" href="#create-some-data">Create some data</a></li>
<li><a class="reference internal" href="#make-a-view">Make a view</a></li>
<li><a class="reference internal" href="#template">Template</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="install.html"
title="previous chapter">Installation</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="models.html"
title="next chapter">Models and Managers</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="sources/tutorial.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="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="models.html" title="Models and Managers"
>next</a> |</li>
<li class="right" >
<a href="install.html" title="Installation"
>previous</a> |</li>
<li><a href="index.html">django-mptt v0.5.2 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2007 - 2011, Jonathan Buchanan and others.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.8.
</div>
</body>
</html>
|