Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
341 lines (319 sloc) 23.7 KB
<!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>Creating Custom Categories &mdash; Django Categories v1.1 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.1',
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 Categories v1.1 documentation" href="index.html" />
<link rel="next" title="Reference" href="reference/index.html" />
<link rel="prev" title="Adding the fields to the database" href="adding_the_fields.html" />
</head>
<body>
<div id="docstitle">
<p>Django Categories v1.1 documentation</p>
</div>
<div id="header">
<div id="title"><h1>Creating Custom Categories</h1></div>
<ul id="headerButtons">
<li id="toc_button"><div class="headerButton"><a href="#">Table of Contents</a></div></li>
<li id="page_buttons">
<div class="headerButton"><a href="genindex.html" title="General Index" accesskey="I">index</a></div>
<div class="headerButton"><a href="reference/index.html" title="Reference" accesskey="N">next</a></div>
<div class="headerButton"><a href="adding_the_fields.html" title="Adding the fields to the database" accesskey="P">previous</a></div>
</li>
</ul>
</div>
<div id="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<ul><li class="toctree-l1"><a href="index.html">Main Page</a></li></ul>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="getting_started.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage.html">Using categories in templates</a></li>
<li class="toctree-l1"><a class="reference internal" href="registering_models.html">Registering Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="adding_the_fields.html">Adding the fields to the database</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Creating Custom Categories</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#name-only">Name only</a></li>
<li class="toctree-l2"><a class="reference internal" href="#name-and-other-data">Name and other data</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="reference/index.html">Reference</a></li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/custom_categories.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<form class="search" action="search.html" method="get">
<div class="search-wrapper">
<span class="search-left"></span>
<input class="prettysearch" type="text" name="q" size="18" />
<span class="search-right">&nbsp;</span>
</div>
<input type="submit" value="Search" class="searchbutton" />
<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="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="creating-custom-categories">
<span id="id1"></span><h1>Creating Custom Categories<a class="headerlink" href="#creating-custom-categories" title="Permalink to this headline">¶</a></h1>
<p>Django Categories isn&#8217;t just for using a single category model. It allows you to create your own custom category-like models with as little or much customization as you need.</p>
<div class="section" id="name-only">
<h2>Name only<a class="headerlink" href="#name-only" title="Permalink to this headline">¶</a></h2>
<p>For many cases, you want a simple user-managed lookup table. You can do this with just a little bit of code. The resulting model will include name, slug and active fields and a hierarchical admin.</p>
<ol class="arabic">
<li><p class="first">Create a model that subclasses <a class="reference internal" href="reference/models.html#CategoryBase" title="CategoryBase"><tt class="xref py py-class docutils literal"><span class="pre">CategoryBase</span></tt></a></p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7
8
9</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">categories.models</span> <span class="kn">import</span> <span class="n">CategoryBase</span>
<span class="k">class</span> <span class="nc">SimpleCategory</span><span class="p">(</span><span class="n">CategoryBase</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> A simple of catgorizing example</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
<span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="s">&#39;simple categories&#39;</span>
</pre></div>
</td></tr></table></div>
</li>
<li><p class="first">Create a subclass of CategoryBaseAdmin.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10</pre></div></td><td class="code"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">categories.admin</span> <span class="kn">import</span> <span class="n">CategoryBaseAdmin</span>
<span class="kn">from</span> <span class="nn">.models</span> <span class="kn">import</span> <span class="n">SimpleCategory</span>
<span class="k">class</span> <span class="nc">SimpleCategoryAdmin</span><span class="p">(</span><span class="n">CategoryBaseAdmin</span><span class="p">):</span>
<span class="k">pass</span>
<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">SimpleCategory</span><span class="p">,</span> <span class="n">SimpleCategoryAdmin</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
</li>
<li><p class="first">Register your model and custom model admin class.</p>
</li>
</ol>
</div>
<div class="section" id="name-and-other-data">
<h2>Name and other data<a class="headerlink" href="#name-and-other-data" title="Permalink to this headline">¶</a></h2>
<p>Sometimes you need more functionality, such as extra metadata and custom functions. The <a class="reference internal" href="reference/models.html#Category" title="Category"><tt class="xref py py-class docutils literal"><span class="pre">Category</span></tt></a> model in this package does this.</p>
<ol class="arabic">
<li><p class="first">Create a model that subclasses <a class="reference internal" href="reference/models.html#CategoryBase" title="CategoryBase"><tt class="xref py py-class docutils literal"><span class="pre">CategoryBase</span></tt></a> as above.</p>
</li>
<li><p class="first">Add new fields to the model. The <a class="reference internal" href="reference/models.html#Category" title="Category"><tt class="xref py py-class docutils literal"><span class="pre">Category</span></tt></a> model adds these extra fields.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 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</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Category</span><span class="p">(</span><span class="n">CategoryBase</span><span class="p">):</span>
<span class="n">thumbnail</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">FileField</span><span class="p">(</span>
<span class="n">upload_to</span><span class="o">=</span><span class="n">THUMBNAIL_UPLOAD_PATH</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">storage</span><span class="o">=</span><span class="n">STORAGE</span><span class="p">(),)</span>
<span class="n">thumbnail_width</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</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">null</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">thumbnail_height</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</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">null</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">order</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="n">alternate_title</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">blank</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="n">default</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">,</span>
<span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">help_text</span><span class="o">=</span><span class="s">&quot;An alternative title to use on pages with this category.&quot;</span><span class="p">)</span>
<span class="n">alternate_url</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">blank</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="n">max_length</span><span class="o">=</span><span class="mi">200</span><span class="p">,</span>
<span class="n">help_text</span><span class="o">=</span><span class="s">&quot;An alternative URL to use instead of the one derived from &quot;</span>
<span class="s">&quot;the category hierarchy.&quot;</span><span class="p">)</span>
<span class="n">description</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</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">null</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">meta_keywords</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">blank</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="n">default</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">,</span>
<span class="n">max_length</span><span class="o">=</span><span class="mi">255</span><span class="p">,</span>
<span class="n">help_text</span><span class="o">=</span><span class="s">&quot;Comma-separated keywords for search engines.&quot;</span><span class="p">)</span>
<span class="n">meta_extra</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</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">default</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">,</span>
<span class="n">help_text</span><span class="o">=</span><span class="s">&quot;(Advanced) Any additional HTML to be placed verbatim &quot;</span>
<span class="s">&quot;in the &amp;lt;head&amp;gt;&quot;</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
</li>
<li><p class="first">Add new methods to the model. For example, the <a class="reference internal" href="reference/models.html#Category" title="Category"><tt class="xref py py-class docutils literal"><span class="pre">Category</span></tt></a> model adds several new methods, including overriding the <tt class="xref py py-meth docutils literal"><span class="pre">save()</span></tt> method.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">thumbnail</span><span class="p">:</span>
<span class="kn">from</span> <span class="nn">django.core.files.images</span> <span class="kn">import</span> <span class="n">get_image_dimensions</span>
<span class="kn">import</span> <span class="nn">django</span>
<span class="k">if</span> <span class="n">django</span><span class="o">.</span><span class="n">VERSION</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>
<span class="n">width</span><span class="p">,</span> <span class="n">height</span> <span class="o">=</span> <span class="n">get_image_dimensions</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">thumbnail</span><span class="o">.</span><span class="n">file</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">width</span><span class="p">,</span> <span class="n">height</span> <span class="o">=</span> <span class="n">get_image_dimensions</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">thumbnail</span><span class="o">.</span><span class="n">file</span><span class="p">,</span> <span class="n">close</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">width</span><span class="p">,</span> <span class="n">height</span> <span class="o">=</span> <span class="bp">None</span><span class="p">,</span> <span class="bp">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">thumbnail_width</span> <span class="o">=</span> <span class="n">width</span>
<span class="bp">self</span><span class="o">.</span><span class="n">thumbnail_height</span> <span class="o">=</span> <span class="n">height</span>
<span class="nb">super</span><span class="p">(</span><span class="n">Category</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
</li>
<li><p class="first">Alter <tt class="xref py py-class docutils literal"><span class="pre">Meta</span></tt> or <tt class="xref py py-class docutils literal"><span class="pre">MPTTMeta</span></tt> class. Either of these inner classes can be overridden, however your <tt class="xref py py-class docutils literal"><span class="pre">Meta</span></tt> class should inherit <tt class="xref py py-class docutils literal"><span class="pre">CategoryBase.Meta</span></tt>. Options for <tt class="xref py py-class docutils literal"><span class="pre">Meta</span></tt> are in the <a class="reference external" href="http://readthedocs.org/docs/django-mptt/en/latest/models.html#model-options">Django-MPTT docs</a>.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Meta</span><span class="p">(</span><span class="n">CategoryBase</span><span class="o">.</span><span class="n">Meta</span><span class="p">):</span>
<span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="s">&#39;categories&#39;</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">&#39;order&#39;</span><span class="p">,</span> <span class="s">&#39;name&#39;</span><span class="p">)</span>
</pre></div>
</td></tr></table></div>
</li>
<li><p class="first">For the admin, you must create a form that subclasses <tt class="xref py py-class docutils literal"><span class="pre">CategoryBaseAdminForm</span></tt> and at least sets the <tt class="docutils literal"><span class="pre">Meta.model</span></tt> attribute. You can also alter the form fields and cleaning methods, as <a class="reference internal" href="reference/models.html#Category" title="Category"><tt class="xref py py-class docutils literal"><span class="pre">Category</span></tt></a> does.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7
8
9</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">CategoryAdminForm</span><span class="p">(</span><span class="n">CategoryBaseAdminForm</span><span class="p">):</span>
<span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">Category</span>
<span class="k">def</span> <span class="nf">clean_alternate_title</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">instance</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;alternate_title&#39;</span><span class="p">]:</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;name&#39;</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;alternate_title&#39;</span><span class="p">]</span>
</pre></div>
</td></tr></table></div>
</li>
<li><p class="first">Next you must subclass <tt class="xref py py-class docutils literal"><span class="pre">CategoryBaseAdmin</span></tt> and assign the <tt class="docutils literal"><span class="pre">form</span></tt> attribute the form class created above. You can alter any other attributes as necessary.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">class</span> <span class="nc">CategoryAdmin</span><span class="p">(</span><span class="n">CategoryBaseAdmin</span><span class="p">):</span>
<span class="n">form</span> <span class="o">=</span> <span class="n">CategoryAdminForm</span>
<span class="n">list_display</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;name&#39;</span><span class="p">,</span> <span class="s">&#39;alternate_title&#39;</span><span class="p">,</span> <span class="s">&#39;active&#39;</span><span class="p">)</span>
<span class="n">fieldsets</span> <span class="o">=</span> <span class="p">(</span>
<span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="p">{</span>
<span class="s">&#39;fields&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;parent&#39;</span><span class="p">,</span> <span class="s">&#39;name&#39;</span><span class="p">,</span> <span class="s">&#39;thumbnail&#39;</span><span class="p">,</span> <span class="s">&#39;active&#39;</span><span class="p">)</span>
<span class="p">}),</span>
<span class="p">(</span><span class="s">&#39;Meta Data&#39;</span><span class="p">,</span> <span class="p">{</span>
<span class="s">&#39;fields&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;alternate_title&#39;</span><span class="p">,</span> <span class="s">&#39;alternate_url&#39;</span><span class="p">,</span> <span class="s">&#39;description&#39;</span><span class="p">,</span>
<span class="s">&#39;meta_keywords&#39;</span><span class="p">,</span> <span class="s">&#39;meta_extra&#39;</span><span class="p">),</span>
<span class="s">&#39;classes&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;collapse&#39;</span><span class="p">,),</span>
<span class="p">}),</span>
<span class="p">(</span><span class="s">&#39;Advanced&#39;</span><span class="p">,</span> <span class="p">{</span>
<span class="s">&#39;fields&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;order&#39;</span><span class="p">,</span> <span class="s">&#39;slug&#39;</span><span class="p">),</span>
<span class="s">&#39;classes&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;collapse&#39;</span><span class="p">,),</span>
<span class="p">}),</span>
<span class="p">)</span>
</pre></div>
</td></tr></table></div>
</li>
</ol>
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
&copy; Copyright 2010-2012, Corey Oordt.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
</p>
</div>
<div class="clearer"></div>
</div>
<div id="breadcrumbs">
Creating Custom Categories
</ul>
</div>
<script type="text/javascript" charset="utf-8" src="_static/toc.js"></script>
</body>
</html>
Something went wrong with that request. Please try again.