Permalink
Fetching contributors…
Cannot retrieve contributors at this time
249 lines (235 sloc) 16.8 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>Registering Models &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="Adding the fields to the database" href="adding_the_fields.html" />
<link rel="prev" title="Using categories in templates" href="usage.html" />
</head>
<body>
<div id="docstitle">
<p>Django Categories v1.1 documentation</p>
</div>
<div id="header">
<div id="title"><h1>Registering Models</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="adding_the_fields.html" title="Adding the fields to the database" accesskey="N">next</a></div>
<div class="headerButton"><a href="usage.html" title="Using categories in templates" 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 current"><a class="current reference internal" href="">Registering Models</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#registering-models-in-settings-py">Registering models in settings.py</a></li>
<li class="toctree-l2"><a class="reference internal" href="#registering-a-many-to-one-relationship">Registering a many-to-one relationship</a></li>
<li class="toctree-l2"><a class="reference internal" href="#registering-a-many-to-many-relationship">Registering a many-to-many relationship</a></li>
</ul>
</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"><a class="reference internal" href="custom_categories.html">Creating Custom Categories</a></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/registering_models.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="registering-models">
<span id="id1"></span><h1>Registering Models<a class="headerlink" href="#registering-models" title="Permalink to this headline">¶</a></h1>
<div class="section" id="registering-models-in-settings-py">
<h2>Registering models in settings.py<a class="headerlink" href="#registering-models-in-settings-py" title="Permalink to this headline">¶</a></h2>
<p>It is nice to not have to modify the code of applications to add a relation to categories. You can therefore do all the registering in <tt class="docutils literal"><span class="pre">settings.py</span></tt>. For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">CATEGORIES_SETTINGS</span> <span class="o">=</span> <span class="p">{</span>
<span class="s">&#39;FK_REGISTRY&#39;</span><span class="p">:</span> <span class="p">{</span>
<span class="s">&#39;app.AModel&#39;</span><span class="p">:</span> <span class="s">&#39;category&#39;</span><span class="p">,</span>
<span class="s">&#39;app.MyModel&#39;</span><span class="p">:</span> <span class="p">(</span>
<span class="s">&#39;primary_category&#39;</span><span class="p">,</span>
<span class="p">{</span><span class="s">&#39;name&#39;</span><span class="p">:</span> <span class="s">&#39;secondary_category&#39;</span><span class="p">,</span> <span class="s">&#39;related_name&#39;</span><span class="p">:</span> <span class="s">&#39;mymodel_sec_cat&#39;</span><span class="p">},</span> <span class="p">)</span>
<span class="p">},</span>
<span class="s">&#39;M2M_REGISTRY&#39;</span><span class="p">:</span> <span class="p">{</span>
<span class="s">&#39;app.BModel&#39;</span><span class="p">:</span> <span class="s">&#39;categories&#39;</span><span class="p">,</span>
<span class="s">&#39;app.MyModel&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;other_categories&#39;</span><span class="p">,</span> <span class="s">&#39;more_categories&#39;</span><span class="p">,</span> <span class="p">),</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">FK_REGISTRY</span></tt> is a dictionary whose keys are the model to which to add the new field(s). The value is a string or tuple of strings or dictionaries specifying the necessary information for each field.</p>
<p>The <tt class="docutils literal"><span class="pre">M2M_REGISTRY</span></tt> is a dictionary whose keys are the model to which to add the new field(s). The value is a string or tuple of strings specifying the necessary information for each field.</p>
<div class="section" id="registering-one-category-field-to-model">
<h3>Registering one Category field to model<a class="headerlink" href="#registering-one-category-field-to-model" title="Permalink to this headline">¶</a></h3>
<p>The simplest way is to specify the name of the field, such as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">CATEGORIES_SETTINGS</span> <span class="o">=</span> <span class="p">{</span>
<span class="s">&#39;FK_REGISTRY&#39;</span><span class="p">:</span> <span class="p">{</span>
<span class="s">&#39;app.AModel&#39;</span><span class="p">:</span> <span class="s">&#39;category&#39;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>This is equivalent to adding the following the <tt class="docutils literal"><span class="pre">AModel</span></tt> of <tt class="docutils literal"><span class="pre">app</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">category</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Category</span><span class="p">)</span>
</pre></div>
</div>
<p>If you want more control over the new field, you can use a dictionary and pass other <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> options. The <tt class="docutils literal"><span class="pre">name</span></tt> key is required:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">CATEGORIES_SETTINGS</span> <span class="o">=</span> <span class="p">{</span>
<span class="s">&#39;FK_REGISTRY&#39;</span><span class="p">:</span> <span class="p">{</span>
<span class="s">&#39;app.AModel&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s">&#39;name&#39;</span><span class="p">:</span> <span class="s">&#39;category&#39;</span><span class="p">,</span> <span class="s">&#39;related_name&#39;</span><span class="p">:</span> <span class="s">&#39;amodel_cats&#39;</span><span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>This is equivalent to adding the following the <tt class="docutils literal"><span class="pre">AModel</span></tt> of <tt class="docutils literal"><span class="pre">app</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">category</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Category</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s">&#39;amodel_cats&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="registering-two-or-more-category-fields-to-a-model">
<h3>Registering two or more Category fields to a Model<a class="headerlink" href="#registering-two-or-more-category-fields-to-a-model" title="Permalink to this headline">¶</a></h3>
<p>When you want more than one relation to <tt class="docutils literal"><span class="pre">Category</span></tt>, all but one of the fields must specify a <tt class="docutils literal"><span class="pre">related_name</span></tt> to avoid conflicts, like so:</p>
<div class="highlight-python"><pre>CATEGORIES_SETTINGS = {
'FK_REGISTRY': {
'app.MyModel': (
'primary_category',
{'name': 'secondary_category', 'related_name': 'mymodel_sec_cat'}, )
},</pre>
</div>
</div>
<div class="section" id="registering-one-or-more-many-to-many-category-fields-to-a-model">
<h3>Registering one or more Many-to-Many Category fields to a Model<a class="headerlink" href="#registering-one-or-more-many-to-many-category-fields-to-a-model" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><div class="highlight"><pre><span class="n">CATEGORIES_SETTINGS</span> <span class="o">=</span> <span class="p">{</span>
<span class="s">&#39;M2M_REGISTRY&#39;</span><span class="p">:</span> <span class="p">{</span>
<span class="s">&#39;app.AModel&#39;</span><span class="p">:</span> <span class="s">&#39;categories&#39;</span><span class="p">,</span>
<span class="s">&#39;app.MyModel&#39;</span><span class="p">:</span> <span class="p">(</span>
<span class="p">{</span><span class="s">&#39;name&#39;</span><span class="p">:</span> <span class="s">&#39;other_categories&#39;</span><span class="p">,</span> <span class="s">&#39;related_name&#39;</span><span class="p">:</span> <span class="s">&#39;other_cats&#39;</span><span class="p">},</span>
<span class="p">{</span><span class="s">&#39;name&#39;</span><span class="p">:</span> <span class="s">&#39;more_categories&#39;</span><span class="p">,</span> <span class="s">&#39;related_name&#39;</span><span class="p">:</span> <span class="s">&#39;more_cats&#39;</span><span class="p">},</span>
<span class="p">),</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="registering-a-many-to-one-relationship">
<span id="registering-a-m2one-relationship"></span><h2>Registering a many-to-one relationship<a class="headerlink" href="#registering-a-many-to-one-relationship" title="Permalink to this headline">¶</a></h2>
<p>To create a many-to-one relationship (foreign key) between a model and Django Categories, you register your model with the <tt class="docutils literal"><span class="pre">register_fk</span></tt> function.</p>
<dl class="function">
<dt id="register_fk">
<tt class="descname">register_fk</tt><big>(</big><em>model, field_name='category', extra_params={}]</em><big>)</big><a class="headerlink" href="#register_fk" title="Permalink to this definition">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>model</strong> &#8211; The Django Model to link to Django Categories</li>
<li><strong>field_name</strong> &#8211; Optional name for the field <strong>default:</strong> category</li>
<li><strong>extra_params</strong> &#8211; Optional dictionary of extra parameters passed to the <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> class.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>Example, in your <tt class="docutils literal"><span class="pre">models.py</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">categories</span>
<span class="n">categories</span><span class="o">.</span><span class="n">register_fk</span><span class="p">(</span><span class="n">MyModel</span><span class="p">)</span>
</pre></div>
</div>
<p>If you want more than one field on a model you have to have some extra arguments:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">categories</span>
<span class="n">categories</span><span class="o">.</span><span class="n">register_fk</span><span class="p">(</span><span class="n">MyModel</span><span class="p">,</span> <span class="s">&#39;primary_category&#39;</span><span class="p">)</span>
<span class="n">categories</span><span class="o">.</span><span class="n">register_fk</span><span class="p">(</span><span class="n">MyModel</span><span class="p">,</span> <span class="s">&#39;secondary_category&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;related_name&#39;</span><span class="p">:</span><span class="s">&#39;mymodel_sec_cat&#39;</span><span class="p">})</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">extra_args</span></tt> allows you to specify the related_name of one of the fields so it doesn&#8217;t clash.</p>
</div>
<div class="section" id="registering-a-many-to-many-relationship">
<h2>Registering a many-to-many relationship<a class="headerlink" href="#registering-a-many-to-many-relationship" title="Permalink to this headline">¶</a></h2>
<p>To create a many-to-many relationship between a model and Django Categories, you register your model with the <tt class="docutils literal"><span class="pre">register_m2m</span></tt> function.</p>
<dl class="function">
<dt id="register_m2m">
<tt class="descname">register_m2m</tt><big>(</big><em>model, field_name='categories', extra_params={}]</em><big>)</big><a class="headerlink" href="#register_m2m" title="Permalink to this definition">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>model</strong> &#8211; The Django Model to link to Django Categories</li>
<li><strong>field_name</strong> &#8211; Optional name for the field <strong>default:</strong> categories</li>
<li><strong>extra_params</strong> &#8211; Optional dictionary of extra parameters passed to the <tt class="docutils literal"><span class="pre">ManyToManyField</span></tt> class.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>Example, in your <tt class="docutils literal"><span class="pre">models.py</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">categories</span>
<span class="n">categories</span><span class="o">.</span><span class="n">register_m2m</span><span class="p">(</span><span class="n">MyModel</span><span class="p">)</span>
</pre></div>
</div>
</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">
Registering Models
</ul>
</div>
<script type="text/javascript" charset="utf-8" src="_static/toc.js"></script>
</body>
</html>