<h2>Registering models in settings.py<aclass="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 <ttclass="docutils literal"><spanclass="pre">settings.py</span></tt>. For example:</p>
<p>The <ttclass="docutils literal"><spanclass="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 <ttclass="docutils literal"><spanclass="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>
<h3>Registering one Category field to model<aclass="headerlink"href="#registering-one-category-field-to-model"title="Permalink to this headline">¶</a></h3>
<p>This is equivalent to adding the following the <ttclass="docutils literal"><spanclass="pre">AModel</span></tt> of <ttclass="docutils literal"><spanclass="pre">app</span></tt>:</p>
<p>If you want more control over the new field, you can use a dictionary and pass other <ttclass="docutils literal"><spanclass="pre">ForeignKey</span></tt> options. The <ttclass="docutils literal"><spanclass="pre">name</span></tt> key is required:</p>
<p>This is equivalent to adding the following the <ttclass="docutils literal"><spanclass="pre">AModel</span></tt> of <ttclass="docutils literal"><spanclass="pre">app</span></tt>:</p>
<h3>Registering two or more Category fields to a Model<aclass="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 <ttclass="docutils literal"><spanclass="pre">Category</span></tt>, all but one of the fields must specify a <ttclass="docutils literal"><spanclass="pre">related_name</span></tt> to avoid conflicts, like so:</p>
<h3>Registering one or more Many-to-Many Category fields to a Model<aclass="headerlink"href="#registering-one-or-more-many-to-many-category-fields-to-a-model"title="Permalink to this headline">¶</a></h3>
<spanid="registering-a-m2one-relationship"></span><h2>Registering a many-to-one relationship<aclass="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 <ttclass="docutils literal"><spanclass="pre">register_fk</span></tt> function.</p>
<dlclass="function">
<dtid="register_fk">
<ttclass="descname">register_fk</tt><big>(</big><em>model, field_name='category', extra_params={}]</em><big>)</big><aclass="headerlink"href="#register_fk"title="Permalink to this definition">¶</a></dt>
<trclass="field"><thclass="field-name">Parameters:</th><tdclass="field-body"><ulclass="first last simple">
<li><strong>model</strong>– The Django Model to link to Django Categories</li>
<li><strong>field_name</strong>– Optional name for the field <strong>default:</strong> category</li>
<li><strong>extra_params</strong>– Optional dictionary of extra parameters passed to the <ttclass="docutils literal"><spanclass="pre">ForeignKey</span></tt> class.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>Example, in your <ttclass="docutils literal"><spanclass="pre">models.py</span></tt>:</p>
<p>The <ttclass="docutils literal"><spanclass="pre">extra_args</span></tt> allows you to specify the related_name of one of the fields so it doesn’t clash.</p>
<h2>Registering a many-to-many relationship<aclass="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 <ttclass="docutils literal"><spanclass="pre">register_m2m</span></tt> function.</p>
<dlclass="function">
<dtid="register_m2m">
<ttclass="descname">register_m2m</tt><big>(</big><em>model, field_name='categories', extra_params={}]</em><big>)</big><aclass="headerlink"href="#register_m2m"title="Permalink to this definition">¶</a></dt>
<trclass="field"><thclass="field-name">Parameters:</th><tdclass="field-body"><ulclass="first last simple">
<li><strong>model</strong>– The Django Model to link to Django Categories</li>
<li><strong>field_name</strong>– Optional name for the field <strong>default:</strong> categories</li>
<li><strong>extra_params</strong>– Optional dictionary of extra parameters passed to the <ttclass="docutils literal"><spanclass="pre">ManyToManyField</span></tt> class.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>Example, in your <ttclass="docutils literal"><spanclass="pre">models.py</span></tt>:</p>