<!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" lang=""> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>The contenttypes framework — Django 1.8.19 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.8.19', 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="index" title="Index" href="../../genindex.html" /> <link rel="search" title="Search" href="../../search.html" /> <link rel="top" title="Django 1.8.19 documentation" href="../../contents.html" /> <link rel="up" title="contrib packages" href="index.html" /> <link rel="next" title="The flatpages app" href="flatpages.html" /> <link rel="prev" title="django.contrib.auth" href="auth.html" /> <script type="text/javascript" src="../../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body role="document"> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../index.html">Django 1.8.19 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../../index.html">Home</a> | <a title="Table of contents" href="../../contents.html">Table of contents</a> | <a title="Global index" href="../../genindex.html">Index</a> | <a title="Module index" href="../../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="auth.html" title="<code class="docutils literal"><span class="pre">django.contrib.auth</span></code>">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="flatpages.html" title="The flatpages app">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="ref-contrib-contenttypes"> <div class="section" id="s-module-django.contrib.contenttypes"> <span id="s-the-contenttypes-framework"></span><span id="module-django.contrib.contenttypes"></span><span id="the-contenttypes-framework"></span><h1>The contenttypes framework<a class="headerlink" href="#module-django.contrib.contenttypes" title="Permalink to this headline">¶</a></h1> <p>Django includes a <a class="reference internal" href="#module-django.contrib.contenttypes" title="django.contrib.contenttypes: Provides generic interface to installed models."><code class="xref py py-mod docutils literal"><span class="pre">contenttypes</span></code></a> application that can track all of the models installed in your Django-powered project, providing a high-level, generic interface for working with your models.</p> <div class="section" id="s-overview"> <span id="overview"></span><h2>Overview<a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2> <p>At the heart of the contenttypes application is the <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> model, which lives at <code class="docutils literal"><span class="pre">django.contrib.contenttypes.models.ContentType</span></code>. Instances of <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> represent and store information about the models installed in your project, and new instances of <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> are automatically created whenever new models are installed.</p> <p>Instances of <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> have methods for returning the model classes they represent and for querying objects from those models. <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> also has a <a class="reference internal" href="../../topics/db/managers.html#custom-managers"><span class="std std-ref">custom manager</span></a> that adds methods for working with <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> and for obtaining instances of <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> for a particular model.</p> <p>Relations between your models and <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> can also be used to enable “generic” relationships between an instance of one of your models and instances of any model you have installed.</p> </div> <div class="section" id="s-installing-the-contenttypes-framework"> <span id="installing-the-contenttypes-framework"></span><h2>Installing the contenttypes framework<a class="headerlink" href="#installing-the-contenttypes-framework" title="Permalink to this headline">¶</a></h2> <p>The contenttypes framework is included in the default <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></code></a> list created by <code class="docutils literal"><span class="pre">django-admin</span> <span class="pre">startproject</span></code>, but if you’ve removed it or if you manually set up your <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></code></a> list, you can enable it by adding <code class="docutils literal"><span class="pre">'django.contrib.contenttypes'</span></code> to your <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></code></a> setting.</p> <p>It’s generally a good idea to have the contenttypes framework installed; several of Django’s other bundled applications require it:</p> <ul class="simple"> <li>The admin application uses it to log the history of each object added or changed through the admin interface.</li> <li>Django’s <a class="reference internal" href="../../topics/auth/index.html#module-django.contrib.auth" title="django.contrib.auth: Django's authentication framework."><code class="xref py py-mod docutils literal"><span class="pre">authentication</span> <span class="pre">framework</span></code></a> uses it to tie user permissions to specific models.</li> </ul> </div> <div class="section" id="s-the-contenttype-model"> <span id="the-contenttype-model"></span><h2>The <code class="docutils literal"><span class="pre">ContentType</span></code> model<a class="headerlink" href="#the-contenttype-model" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.contrib.contenttypes.models.ContentType"> <em class="property">class </em><code class="descname">ContentType</code><a class="headerlink" href="#django.contrib.contenttypes.models.ContentType" title="Permalink to this definition">¶</a></dt> <dd><p>Each instance of <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> has two fields which, taken together, uniquely describe an installed model:</p> <dl class="attribute"> <dt id="django.contrib.contenttypes.models.ContentType.app_label"> <code class="descname">app_label</code><a class="headerlink" href="#django.contrib.contenttypes.models.ContentType.app_label" title="Permalink to this definition">¶</a></dt> <dd><p>The name of the application the model is part of. This is taken from the <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType.app_label" title="django.contrib.contenttypes.models.ContentType.app_label"><code class="xref py py-attr docutils literal"><span class="pre">app_label</span></code></a> attribute of the model, and includes only the <em>last</em> part of the application’s Python import path; “django.contrib.contenttypes”, for example, becomes an <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType.app_label" title="django.contrib.contenttypes.models.ContentType.app_label"><code class="xref py py-attr docutils literal"><span class="pre">app_label</span></code></a> of “contenttypes”.</p> </dd></dl> <dl class="attribute"> <dt id="django.contrib.contenttypes.models.ContentType.model"> <code class="descname">model</code><a class="headerlink" href="#django.contrib.contenttypes.models.ContentType.model" title="Permalink to this definition">¶</a></dt> <dd><p>The name of the model class.</p> </dd></dl> <p>Additionally, the following property is available:</p> <dl class="attribute"> <dt id="django.contrib.contenttypes.models.ContentType.name"> <code class="descname">name</code><a class="headerlink" href="#django.contrib.contenttypes.models.ContentType.name" title="Permalink to this definition">¶</a></dt> <dd><p>The human-readable name of the content type. This is taken from the <a class="reference internal" href="../models/fields.html#django.db.models.Field.verbose_name" title="django.db.models.Field.verbose_name"><code class="xref py py-attr docutils literal"><span class="pre">verbose_name</span></code></a> attribute of the model.</p> </dd></dl> </dd></dl> <div class="versionchanged"> <span class="title">Changed in Django 1.8:</span> <p>Before Django 1.8, the <code class="docutils literal"><span class="pre">name</span></code> property was a real field on the <code class="docutils literal"><span class="pre">ContentType</span></code> model.</p> </div> <p>Let’s look at an example to see how this works. If you already have the <a class="reference internal" href="#module-django.contrib.contenttypes" title="django.contrib.contenttypes: Provides generic interface to installed models."><code class="xref py py-mod docutils literal"><span class="pre">contenttypes</span></code></a> application installed, and then add <a class="reference internal" href="sites.html#module-django.contrib.sites" title="django.contrib.sites: Lets you operate multiple Web sites from the same database and Django project"><code class="xref py py-mod docutils literal"><span class="pre">the</span> <span class="pre">sites</span> <span class="pre">application</span></code></a> to your <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal"><span class="pre">INSTALLED_APPS</span></code></a> setting and run <code class="docutils literal"><span class="pre">manage.py</span> <span class="pre">migrate</span></code> to install it, the model <a class="reference internal" href="sites.html#django.contrib.sites.models.Site" title="django.contrib.sites.models.Site"><code class="xref py py-class docutils literal"><span class="pre">django.contrib.sites.models.Site</span></code></a> will be installed into your database. Along with it a new instance of <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> will be created with the following values:</p> <ul class="simple"> <li><a class="reference internal" href="#django.contrib.contenttypes.models.ContentType.app_label" title="django.contrib.contenttypes.models.ContentType.app_label"><code class="xref py py-attr docutils literal"><span class="pre">app_label</span></code></a> will be set to <code class="docutils literal"><span class="pre">'sites'</span></code> (the last part of the Python path “django.contrib.sites”).</li> <li><a class="reference internal" href="#django.contrib.contenttypes.models.ContentType.model" title="django.contrib.contenttypes.models.ContentType.model"><code class="xref py py-attr docutils literal"><span class="pre">model</span></code></a> will be set to <code class="docutils literal"><span class="pre">'site'</span></code>.</li> </ul> </div> <div class="section" id="s-methods-on-contenttype-instances"> <span id="methods-on-contenttype-instances"></span><h2>Methods on <code class="docutils literal"><span class="pre">ContentType</span></code> instances<a class="headerlink" href="#methods-on-contenttype-instances" title="Permalink to this headline">¶</a></h2> <p>Each <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> instance has methods that allow you to get from a <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> instance to the model it represents, or to retrieve objects from that model:</p> <dl class="method"> <dt id="django.contrib.contenttypes.models.ContentType.get_object_for_this_type"> <code class="descclassname">ContentType.</code><code class="descname">get_object_for_this_type</code>(<em>**kwargs</em>)<a class="headerlink" href="#django.contrib.contenttypes.models.ContentType.get_object_for_this_type" title="Permalink to this definition">¶</a></dt> <dd><p>Takes a set of valid <a class="reference internal" href="../../topics/db/queries.html#field-lookups-intro"><span class="std std-ref">lookup arguments</span></a> for the model the <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> represents, and does <a class="reference internal" href="../models/querysets.html#django.db.models.query.QuerySet.get" title="django.db.models.query.QuerySet.get"><code class="xref py py-meth docutils literal"><span class="pre">a</span> <span class="pre">get()</span> <span class="pre">lookup</span></code></a> on that model, returning the corresponding object.</p> </dd></dl> <dl class="method"> <dt id="django.contrib.contenttypes.models.ContentType.model_class"> <code class="descclassname">ContentType.</code><code class="descname">model_class</code>()<a class="headerlink" href="#django.contrib.contenttypes.models.ContentType.model_class" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the model class represented by this <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> instance.</p> </dd></dl> <p>For example, we could look up the <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> for the <a class="reference internal" href="auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><code class="xref py py-class docutils literal"><span class="pre">User</span></code></a> model:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">django.contrib.contenttypes.models</span> <span class="k">import</span> <span class="n">ContentType</span> <span class="gp">>>> </span><span class="n">user_type</span> <span class="o">=</span> <span class="n">ContentType</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">app_label</span><span class="o">=</span><span class="s2">"auth"</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="s2">"user"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">user_type</span> <span class="go"><ContentType: user></span> </pre></div> </div> <p>And then use it to query for a particular <a class="reference internal" href="auth.html#django.contrib.auth.models.User" title="django.contrib.auth.models.User"><code class="xref py py-class docutils literal"><span class="pre">User</span></code></a>, or to get access to the <code class="docutils literal"><span class="pre">User</span></code> model class:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">user_type</span><span class="o">.</span><span class="n">model_class</span><span class="p">()</span> <span class="go"><class 'django.contrib.auth.models.User'></span> <span class="gp">>>> </span><span class="n">user_type</span><span class="o">.</span><span class="n">get_object_for_this_type</span><span class="p">(</span><span class="n">username</span><span class="o">=</span><span class="s1">'Guido'</span><span class="p">)</span> <span class="go"><User: Guido></span> </pre></div> </div> <p>Together, <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType.get_object_for_this_type" title="django.contrib.contenttypes.models.ContentType.get_object_for_this_type"><code class="xref py py-meth docutils literal"><span class="pre">get_object_for_this_type()</span></code></a> and <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType.model_class" title="django.contrib.contenttypes.models.ContentType.model_class"><code class="xref py py-meth docutils literal"><span class="pre">model_class()</span></code></a> enable two extremely important use cases:</p> <ol class="arabic simple"> <li>Using these methods, you can write high-level generic code that performs queries on any installed model – instead of importing and using a single specific model class, you can pass an <code class="docutils literal"><span class="pre">app_label</span></code> and <code class="docutils literal"><span class="pre">model</span></code> into a <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> lookup at runtime, and then work with the model class or retrieve objects from it.</li> <li>You can relate another model to <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> as a way of tying instances of it to particular model classes, and use these methods to get access to those model classes.</li> </ol> <p>Several of Django’s bundled applications make use of the latter technique. For example, <a class="reference internal" href="auth.html#django.contrib.auth.models.Permission" title="django.contrib.auth.models.Permission"><code class="xref py py-class docutils literal"><span class="pre">the</span> <span class="pre">permissions</span> <span class="pre">system</span></code></a> in Django’s authentication framework uses a <a class="reference internal" href="auth.html#django.contrib.auth.models.Permission" title="django.contrib.auth.models.Permission"><code class="xref py py-class docutils literal"><span class="pre">Permission</span></code></a> model with a foreign key to <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a>; this lets <a class="reference internal" href="auth.html#django.contrib.auth.models.Permission" title="django.contrib.auth.models.Permission"><code class="xref py py-class docutils literal"><span class="pre">Permission</span></code></a> represent concepts like “can add blog entry” or “can delete news story”.</p> <div class="section" id="s-the-contenttypemanager"> <span id="the-contenttypemanager"></span><h3>The <code class="docutils literal"><span class="pre">ContentTypeManager</span></code><a class="headerlink" href="#the-contenttypemanager" title="Permalink to this headline">¶</a></h3> <dl class="class"> <dt id="django.contrib.contenttypes.models.ContentTypeManager"> <em class="property">class </em><code class="descname">ContentTypeManager</code><a class="headerlink" href="#django.contrib.contenttypes.models.ContentTypeManager" title="Permalink to this definition">¶</a></dt> <dd><p><a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> also has a custom manager, <a class="reference internal" href="#django.contrib.contenttypes.models.ContentTypeManager" title="django.contrib.contenttypes.models.ContentTypeManager"><code class="xref py py-class docutils literal"><span class="pre">ContentTypeManager</span></code></a>, which adds the following methods:</p> <dl class="method"> <dt id="django.contrib.contenttypes.models.ContentTypeManager.clear_cache"> <code class="descname">clear_cache</code>()<a class="headerlink" href="#django.contrib.contenttypes.models.ContentTypeManager.clear_cache" title="Permalink to this definition">¶</a></dt> <dd><p>Clears an internal cache used by <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> to keep track of models for which it has created <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> instances. You probably won’t ever need to call this method yourself; Django will call it automatically when it’s needed.</p> </dd></dl> <dl class="method"> <dt id="django.contrib.contenttypes.models.ContentTypeManager.get_for_id"> <code class="descname">get_for_id</code>(<em>id</em>)<a class="headerlink" href="#django.contrib.contenttypes.models.ContentTypeManager.get_for_id" title="Permalink to this definition">¶</a></dt> <dd><p>Lookup a <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> by ID. Since this method uses the same shared cache as <a class="reference internal" href="#django.contrib.contenttypes.models.ContentTypeManager.get_for_model" title="django.contrib.contenttypes.models.ContentTypeManager.get_for_model"><code class="xref py py-meth docutils literal"><span class="pre">get_for_model()</span></code></a>, it’s preferred to use this method over the usual <code class="docutils literal"><span class="pre">ContentType.objects.get(pk=id)</span></code></p> </dd></dl> <dl class="method"> <dt id="django.contrib.contenttypes.models.ContentTypeManager.get_for_model"> <code class="descname">get_for_model</code>(<em>model</em>, <em>for_concrete_model=True</em>)<a class="headerlink" href="#django.contrib.contenttypes.models.ContentTypeManager.get_for_model" title="Permalink to this definition">¶</a></dt> <dd><p>Takes either a model class or an instance of a model, and returns the <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> instance representing that model. <code class="docutils literal"><span class="pre">for_concrete_model=False</span></code> allows fetching the <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> of a proxy model.</p> </dd></dl> <dl class="method"> <dt id="django.contrib.contenttypes.models.ContentTypeManager.get_for_models"> <code class="descname">get_for_models</code>(<em>*models</em>, <em>for_concrete_models=True</em>)<a class="headerlink" href="#django.contrib.contenttypes.models.ContentTypeManager.get_for_models" title="Permalink to this definition">¶</a></dt> <dd><p>Takes a variadic number of model classes, and returns a dictionary mapping the model classes to the <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> instances representing them. <code class="docutils literal"><span class="pre">for_concrete_models=False</span></code> allows fetching the <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> of proxy models.</p> </dd></dl> <dl class="method"> <dt id="django.contrib.contenttypes.models.ContentTypeManager.get_by_natural_key"> <code class="descname">get_by_natural_key</code>(<em>app_label</em>, <em>model</em>)<a class="headerlink" href="#django.contrib.contenttypes.models.ContentTypeManager.get_by_natural_key" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> instance uniquely identified by the given application label and model name. The primary purpose of this method is to allow <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> objects to be referenced via a <a class="reference internal" href="../../topics/serialization.html#topics-serialization-natural-keys"><span class="std std-ref">natural key</span></a> during deserialization.</p> </dd></dl> </dd></dl> <p>The <a class="reference internal" href="#django.contrib.contenttypes.models.ContentTypeManager.get_for_model" title="django.contrib.contenttypes.models.ContentTypeManager.get_for_model"><code class="xref py py-meth docutils literal"><span class="pre">get_for_model()</span></code></a> method is especially useful when you know you need to work with a <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> but don’t want to go to the trouble of obtaining the model’s metadata to perform a manual lookup:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="k">import</span> <span class="n">User</span> <span class="gp">>>> </span><span class="n">user_type</span> <span class="o">=</span> <span class="n">ContentType</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_for_model</span><span class="p">(</span><span class="n">User</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">user_type</span> <span class="go"><ContentType: user></span> </pre></div> </div> <span class="target" id="module-django.contrib.contenttypes.fields"></span></div> </div> <div class="section" id="s-generic-relations"> <span id="s-id1"></span><span id="generic-relations"></span><span id="id1"></span><h2>Generic relations<a class="headerlink" href="#generic-relations" title="Permalink to this headline">¶</a></h2> <p>Adding a foreign key from one of your own models to <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> allows your model to effectively tie itself to another model class, as in the example of the <a class="reference internal" href="auth.html#django.contrib.auth.models.Permission" title="django.contrib.auth.models.Permission"><code class="xref py py-class docutils literal"><span class="pre">Permission</span></code></a> model above. But it’s possible to go one step further and use <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> to enable truly generic (sometimes called “polymorphic”) relationships between models.</p> <p>A simple example is a tagging system, which might look like this:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span> <span class="kn">from</span> <span class="nn">django.contrib.contenttypes.fields</span> <span class="k">import</span> <span class="n">GenericForeignKey</span> <span class="kn">from</span> <span class="nn">django.contrib.contenttypes.models</span> <span class="k">import</span> <span class="n">ContentType</span> <span class="k">class</span> <span class="nc">TaggedItem</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span> <span class="n">tag</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">SlugField</span><span class="p">()</span> <span class="n">content_type</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">ContentType</span><span class="p">)</span> <span class="n">object_id</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">PositiveIntegerField</span><span class="p">()</span> <span class="n">content_object</span> <span class="o">=</span> <span class="n">GenericForeignKey</span><span class="p">(</span><span class="s1">'content_type'</span><span class="p">,</span> <span class="s1">'object_id'</span><span class="p">)</span> <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c1"># __unicode__ on Python 2</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">tag</span> </pre></div> </div> <p>A normal <a class="reference internal" href="../models/fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></code></a> can only “point to” one other model, which means that if the <code class="docutils literal"><span class="pre">TaggedItem</span></code> model used a <a class="reference internal" href="../models/fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></code></a> it would have to choose one and only one model to store tags for. The contenttypes application provides a special field type (<code class="docutils literal"><span class="pre">GenericForeignKey</span></code>) which works around this and allows the relationship to be with any model:</p> <dl class="class"> <dt id="django.contrib.contenttypes.fields.GenericForeignKey"> <em class="property">class </em><code class="descname">GenericForeignKey</code><a class="headerlink" href="#django.contrib.contenttypes.fields.GenericForeignKey" title="Permalink to this definition">¶</a></dt> <dd><p>There are three parts to setting up a <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericForeignKey" title="django.contrib.contenttypes.fields.GenericForeignKey"><code class="xref py py-class docutils literal"><span class="pre">GenericForeignKey</span></code></a>:</p> <ol class="arabic simple"> <li>Give your model a <a class="reference internal" href="../models/fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></code></a> to <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a>. The usual name for this field is “content_type”.</li> <li>Give your model a field that can store primary key values from the models you’ll be relating to. For most models, this means a <a class="reference internal" href="../models/fields.html#django.db.models.PositiveIntegerField" title="django.db.models.PositiveIntegerField"><code class="xref py py-class docutils literal"><span class="pre">PositiveIntegerField</span></code></a>. The usual name for this field is “object_id”.</li> <li>Give your model a <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericForeignKey" title="django.contrib.contenttypes.fields.GenericForeignKey"><code class="xref py py-class docutils literal"><span class="pre">GenericForeignKey</span></code></a>, and pass it the names of the two fields described above. If these fields are named “content_type” and “object_id”, you can omit this – those are the default field names <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericForeignKey" title="django.contrib.contenttypes.fields.GenericForeignKey"><code class="xref py py-class docutils literal"><span class="pre">GenericForeignKey</span></code></a> will look for.</li> </ol> <dl class="attribute"> <dt id="django.contrib.contenttypes.fields.GenericForeignKey.for_concrete_model"> <code class="descname">for_concrete_model</code><a class="headerlink" href="#django.contrib.contenttypes.fields.GenericForeignKey.for_concrete_model" title="Permalink to this definition">¶</a></dt> <dd><p>If <code class="docutils literal"><span class="pre">False</span></code>, the field will be able to reference proxy models. Default is <code class="docutils literal"><span class="pre">True</span></code>. This mirrors the <code class="docutils literal"><span class="pre">for_concrete_model</span></code> argument to <a class="reference internal" href="#django.contrib.contenttypes.models.ContentTypeManager.get_for_model" title="django.contrib.contenttypes.models.ContentTypeManager.get_for_model"><code class="xref py py-meth docutils literal"><span class="pre">get_for_model()</span></code></a>.</p> </dd></dl> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 1.7: </span>This class was moved from <code class="docutils literal"><span class="pre">django.contrib.contenttypes.generic</span></code> to <code class="docutils literal"><span class="pre">django.contrib.contenttypes.fields</span></code>. Support for importing from the old location will be removed in Django 1.9.</p> </div> </dd></dl> <div class="admonition-primary-key-type-compatibility admonition"> <p class="first admonition-title">Primary key type compatibility</p> <p>The “object_id” field doesn’t have to be the same type as the primary key fields on the related models, but their primary key values must be coercible to the same type as the “object_id” field by its <a class="reference internal" href="../models/fields.html#django.db.models.Field.get_db_prep_value" title="django.db.models.Field.get_db_prep_value"><code class="xref py py-meth docutils literal"><span class="pre">get_db_prep_value()</span></code></a> method.</p> <p>For example, if you want to allow generic relations to models with either <a class="reference internal" href="../models/fields.html#django.db.models.IntegerField" title="django.db.models.IntegerField"><code class="xref py py-class docutils literal"><span class="pre">IntegerField</span></code></a> or <a class="reference internal" href="../models/fields.html#django.db.models.CharField" title="django.db.models.CharField"><code class="xref py py-class docutils literal"><span class="pre">CharField</span></code></a> primary key fields, you can use <a class="reference internal" href="../models/fields.html#django.db.models.CharField" title="django.db.models.CharField"><code class="xref py py-class docutils literal"><span class="pre">CharField</span></code></a> for the “object_id” field on your model since integers can be coerced to strings by <a class="reference internal" href="../models/fields.html#django.db.models.Field.get_db_prep_value" title="django.db.models.Field.get_db_prep_value"><code class="xref py py-meth docutils literal"><span class="pre">get_db_prep_value()</span></code></a>.</p> <p>For maximum flexibility you can use a <a class="reference internal" href="../models/fields.html#django.db.models.TextField" title="django.db.models.TextField"><code class="xref py py-class docutils literal"><span class="pre">TextField</span></code></a> which doesn’t have a maximum length defined, however this may incur significant performance penalties depending on your database backend.</p> <p class="last">There is no one-size-fits-all solution for which field type is best. You should evaluate the models you expect to be pointing to and determine which solution will be most effective for your use case.</p> </div> <div class="admonition-serializing-references-to-contenttype-objects admonition"> <p class="first admonition-title">Serializing references to <code class="docutils literal"><span class="pre">ContentType</span></code> objects</p> <p class="last">If you’re serializing data (for example, when generating <a class="reference internal" href="../../topics/testing/tools.html#django.test.TransactionTestCase.fixtures" title="django.test.TransactionTestCase.fixtures"><code class="xref py py-class docutils literal"><span class="pre">fixtures</span></code></a>) from a model that implements generic relations, you should probably be using a natural key to uniquely identify related <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> objects. See <a class="reference internal" href="../../topics/serialization.html#topics-serialization-natural-keys"><span class="std std-ref">natural keys</span></a> and <a class="reference internal" href="../django-admin.html#django-admin-option---natural-foreign"><code class="xref std std-djadminopt docutils literal"><span class="pre">dumpdata</span> <span class="pre">--natural-foreign</span></code></a> for more information.</p> </div> <p>This will enable an API similar to the one used for a normal <a class="reference internal" href="../models/fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></code></a>; each <code class="docutils literal"><span class="pre">TaggedItem</span></code> will have a <code class="docutils literal"><span class="pre">content_object</span></code> field that returns the object it’s related to, and you can also assign to that field or use it when creating a <code class="docutils literal"><span class="pre">TaggedItem</span></code>:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="k">import</span> <span class="n">User</span> <span class="gp">>>> </span><span class="n">guido</span> <span class="o">=</span> <span class="n">User</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">username</span><span class="o">=</span><span class="s1">'Guido'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">t</span> <span class="o">=</span> <span class="n">TaggedItem</span><span class="p">(</span><span class="n">content_object</span><span class="o">=</span><span class="n">guido</span><span class="p">,</span> <span class="n">tag</span><span class="o">=</span><span class="s1">'bdfl'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">content_object</span> <span class="go"><User: Guido></span> </pre></div> </div> <p>Due to the way <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericForeignKey" title="django.contrib.contenttypes.fields.GenericForeignKey"><code class="xref py py-class docutils literal"><span class="pre">GenericForeignKey</span></code></a> is implemented, you cannot use such fields directly with filters (<code class="docutils literal"><span class="pre">filter()</span></code> and <code class="docutils literal"><span class="pre">exclude()</span></code>, for example) via the database API. Because a <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericForeignKey" title="django.contrib.contenttypes.fields.GenericForeignKey"><code class="xref py py-class docutils literal"><span class="pre">GenericForeignKey</span></code></a> isn’t a normal field object, these examples will <em>not</em> work:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># This will fail</span> <span class="o">>>></span> <span class="n">TaggedItem</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">content_object</span><span class="o">=</span><span class="n">guido</span><span class="p">)</span> <span class="c1"># This will also fail</span> <span class="o">>>></span> <span class="n">TaggedItem</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">content_object</span><span class="o">=</span><span class="n">guido</span><span class="p">)</span> </pre></div> </div> <p>Likewise, <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericForeignKey" title="django.contrib.contenttypes.fields.GenericForeignKey"><code class="xref py py-class docutils literal"><span class="pre">GenericForeignKey</span></code></a>s does not appear in <a class="reference internal" href="../../topics/forms/modelforms.html#django.forms.ModelForm" title="django.forms.ModelForm"><code class="xref py py-class docutils literal"><span class="pre">ModelForm</span></code></a>s.</p> <div class="section" id="s-reverse-generic-relations"> <span id="reverse-generic-relations"></span><h3>Reverse generic relations<a class="headerlink" href="#reverse-generic-relations" title="Permalink to this headline">¶</a></h3> <dl class="class"> <dt id="django.contrib.contenttypes.fields.GenericRelation"> <em class="property">class </em><code class="descname">GenericRelation</code><a class="headerlink" href="#django.contrib.contenttypes.fields.GenericRelation" title="Permalink to this definition">¶</a></dt> <dd><dl class="attribute"> <dt id="django.contrib.contenttypes.fields.GenericRelation.related_query_name"> <code class="descname">related_query_name</code><a class="headerlink" href="#django.contrib.contenttypes.fields.GenericRelation.related_query_name" title="Permalink to this definition">¶</a></dt> <dd><div class="versionadded"> <span class="title">New in Django 1.7.</span> </div> <p>The relation on the related object back to this object doesn’t exist by default. Setting <code class="docutils literal"><span class="pre">related_query_name</span></code> creates a relation from the related object back to this one. This allows querying and filtering from the related object.</p> </dd></dl> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 1.7: </span>This class was moved from <code class="docutils literal"><span class="pre">django.contrib.contenttypes.generic</span></code> to <code class="docutils literal"><span class="pre">django.contrib.contenttypes.fields</span></code>. Support for importing from the old location will be removed in Django 1.9.</p> </div> </dd></dl> <p>If you know which models you’ll be using most often, you can also add a “reverse” generic relationship to enable an additional API. For example:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span> <span class="kn">from</span> <span class="nn">django.contrib.contenttypes.fields</span> <span class="k">import</span> <span class="n">GenericRelation</span> <span class="k">class</span> <span class="nc">Bookmark</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span> <span class="n">url</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">URLField</span><span class="p">()</span> <span class="n">tags</span> <span class="o">=</span> <span class="n">GenericRelation</span><span class="p">(</span><span class="n">TaggedItem</span><span class="p">)</span> </pre></div> </div> <p><code class="docutils literal"><span class="pre">Bookmark</span></code> instances will each have a <code class="docutils literal"><span class="pre">tags</span></code> attribute, which can be used to retrieve their associated <code class="docutils literal"><span class="pre">TaggedItems</span></code>:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">Bookmark</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="s1">'https://www.djangoproject.com/'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">b</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">t1</span> <span class="o">=</span> <span class="n">TaggedItem</span><span class="p">(</span><span class="n">content_object</span><span class="o">=</span><span class="n">b</span><span class="p">,</span> <span class="n">tag</span><span class="o">=</span><span class="s1">'django'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">t1</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">t2</span> <span class="o">=</span> <span class="n">TaggedItem</span><span class="p">(</span><span class="n">content_object</span><span class="o">=</span><span class="n">b</span><span class="p">,</span> <span class="n">tag</span><span class="o">=</span><span class="s1">'python'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">t2</span><span class="o">.</span><span class="n">save</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">b</span><span class="o">.</span><span class="n">tags</span><span class="o">.</span><span class="n">all</span><span class="p">()</span> <span class="go">[<TaggedItem: django>, <TaggedItem: python>]</span> </pre></div> </div> <div class="versionadded"> <span class="title">New in Django 1.7.</span> </div> <p>Defining <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericRelation" title="django.contrib.contenttypes.fields.GenericRelation"><code class="xref py py-class docutils literal"><span class="pre">GenericRelation</span></code></a> with <code class="docutils literal"><span class="pre">related_query_name</span></code> set allows querying from the related object:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">tags</span> <span class="o">=</span> <span class="n">GenericRelation</span><span class="p">(</span><span class="n">TaggedItem</span><span class="p">,</span> <span class="n">related_query_name</span><span class="o">=</span><span class="s1">'bookmarks'</span><span class="p">)</span> </pre></div> </div> <p>This enables filtering, ordering, and other query operations on <code class="docutils literal"><span class="pre">Bookmark</span></code> from <code class="docutils literal"><span class="pre">TaggedItem</span></code>:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="c1"># Get all tags belonging to bookmarks containing `django` in the url</span> <span class="gp">>>> </span><span class="n">TaggedItem</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">bookmarks__url__contains</span><span class="o">=</span><span class="s1">'django'</span><span class="p">)</span> <span class="go">[<TaggedItem: django>, <TaggedItem: python>]</span> </pre></div> </div> <p>Just as <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericForeignKey" title="django.contrib.contenttypes.fields.GenericForeignKey"><code class="xref py py-class docutils literal"><span class="pre">GenericForeignKey</span></code></a> accepts the names of the content-type and object-ID fields as arguments, so too does <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericRelation" title="django.contrib.contenttypes.fields.GenericRelation"><code class="xref py py-class docutils literal"><span class="pre">GenericRelation</span></code></a>; if the model which has the generic foreign key is using non-default names for those fields, you must pass the names of the fields when setting up a <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericRelation" title="django.contrib.contenttypes.fields.GenericRelation"><code class="xref py py-class docutils literal"><span class="pre">GenericRelation</span></code></a> to it. For example, if the <code class="docutils literal"><span class="pre">TaggedItem</span></code> model referred to above used fields named <code class="docutils literal"><span class="pre">content_type_fk</span></code> and <code class="docutils literal"><span class="pre">object_primary_key</span></code> to create its generic foreign key, then a <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericRelation" title="django.contrib.contenttypes.fields.GenericRelation"><code class="xref py py-class docutils literal"><span class="pre">GenericRelation</span></code></a> back to it would need to be defined like so:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">tags</span> <span class="o">=</span> <span class="n">GenericRelation</span><span class="p">(</span><span class="n">TaggedItem</span><span class="p">,</span> <span class="n">content_type_field</span><span class="o">=</span><span class="s1">'content_type_fk'</span><span class="p">,</span> <span class="n">object_id_field</span><span class="o">=</span><span class="s1">'object_primary_key'</span><span class="p">)</span> </pre></div> </div> <p>Of course, if you don’t add the reverse relationship, you can do the same types of lookups manually:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">Bookmark</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="s1">'https://www.djangoproject.com/'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">bookmark_type</span> <span class="o">=</span> <span class="n">ContentType</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_for_model</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">TaggedItem</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">content_type__pk</span><span class="o">=</span><span class="n">bookmark_type</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="gp">... </span> <span class="n">object_id</span><span class="o">=</span><span class="n">b</span><span class="o">.</span><span class="n">id</span><span class="p">)</span> <span class="go">[<TaggedItem: django>, <TaggedItem: python>]</span> </pre></div> </div> <p>Note that if the model in a <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericRelation" title="django.contrib.contenttypes.fields.GenericRelation"><code class="xref py py-class docutils literal"><span class="pre">GenericRelation</span></code></a> uses a non-default value for <code class="docutils literal"><span class="pre">ct_field</span></code> or <code class="docutils literal"><span class="pre">fk_field</span></code> in its <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericForeignKey" title="django.contrib.contenttypes.fields.GenericForeignKey"><code class="xref py py-class docutils literal"><span class="pre">GenericForeignKey</span></code></a> (for example, if you had a <code class="docutils literal"><span class="pre">Comment</span></code> model that uses <code class="docutils literal"><span class="pre">ct_field="object_pk"</span></code>), you’ll need to set <code class="docutils literal"><span class="pre">content_type_field</span></code> and/or <code class="docutils literal"><span class="pre">object_id_field</span></code> in the <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericRelation" title="django.contrib.contenttypes.fields.GenericRelation"><code class="xref py py-class docutils literal"><span class="pre">GenericRelation</span></code></a> to match the <code class="docutils literal"><span class="pre">ct_field</span></code> and <code class="docutils literal"><span class="pre">fk_field</span></code>, respectively, in the <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericForeignKey" title="django.contrib.contenttypes.fields.GenericForeignKey"><code class="xref py py-class docutils literal"><span class="pre">GenericForeignKey</span></code></a>:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">comments</span> <span class="o">=</span> <span class="n">fields</span><span class="o">.</span><span class="n">GenericRelation</span><span class="p">(</span><span class="n">Comment</span><span class="p">,</span> <span class="n">object_id_field</span><span class="o">=</span><span class="s2">"object_pk"</span><span class="p">)</span> </pre></div> </div> <p>Note also, that if you delete an object that has a <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericRelation" title="django.contrib.contenttypes.fields.GenericRelation"><code class="xref py py-class docutils literal"><span class="pre">GenericRelation</span></code></a>, any objects which have a <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericForeignKey" title="django.contrib.contenttypes.fields.GenericForeignKey"><code class="xref py py-class docutils literal"><span class="pre">GenericForeignKey</span></code></a> pointing at it will be deleted as well. In the example above, this means that if a <code class="docutils literal"><span class="pre">Bookmark</span></code> object were deleted, any <code class="docutils literal"><span class="pre">TaggedItem</span></code> objects pointing at it would be deleted at the same time.</p> <p>Unlike <a class="reference internal" href="../models/fields.html#django.db.models.ForeignKey" title="django.db.models.ForeignKey"><code class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></code></a>, <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericForeignKey" title="django.contrib.contenttypes.fields.GenericForeignKey"><code class="xref py py-class docutils literal"><span class="pre">GenericForeignKey</span></code></a> does not accept an <a class="reference internal" href="../models/fields.html#django.db.models.ForeignKey.on_delete" title="django.db.models.ForeignKey.on_delete"><code class="xref py py-attr docutils literal"><span class="pre">on_delete</span></code></a> argument to customize this behavior; if desired, you can avoid the cascade-deletion simply by not using <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericRelation" title="django.contrib.contenttypes.fields.GenericRelation"><code class="xref py py-class docutils literal"><span class="pre">GenericRelation</span></code></a>, and alternate behavior can be provided via the <a class="reference internal" href="../signals.html#django.db.models.signals.pre_delete" title="django.db.models.signals.pre_delete"><code class="xref py py-data docutils literal"><span class="pre">pre_delete</span></code></a> signal.</p> </div> <div class="section" id="s-generic-relations-and-aggregation"> <span id="generic-relations-and-aggregation"></span><h3>Generic relations and aggregation<a class="headerlink" href="#generic-relations-and-aggregation" title="Permalink to this headline">¶</a></h3> <p><a class="reference internal" href="../../topics/db/aggregation.html"><span class="doc">Django’s database aggregation API</span></a> works with a <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericRelation" title="django.contrib.contenttypes.fields.GenericRelation"><code class="xref py py-class docutils literal"><span class="pre">GenericRelation</span></code></a>. For example, you can find out how many tags all the bookmarks have:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">Bookmark</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">Count</span><span class="p">(</span><span class="s1">'tags'</span><span class="p">))</span> <span class="go">{'tags__count': 3}</span> </pre></div> </div> <span class="target" id="module-django.contrib.contenttypes.forms"></span></div> <div class="section" id="s-generic-relation-in-forms"> <span id="generic-relation-in-forms"></span><h3>Generic relation in forms<a class="headerlink" href="#generic-relation-in-forms" title="Permalink to this headline">¶</a></h3> <p>The <a class="reference internal" href="#module-django.contrib.contenttypes.forms" title="django.contrib.contenttypes.forms"><code class="xref py py-mod docutils literal"><span class="pre">django.contrib.contenttypes.forms</span></code></a> module provides:</p> <ul class="simple"> <li><a class="reference internal" href="#django.contrib.contenttypes.forms.BaseGenericInlineFormSet" title="django.contrib.contenttypes.forms.BaseGenericInlineFormSet"><code class="xref py py-class docutils literal"><span class="pre">BaseGenericInlineFormSet</span></code></a></li> <li>A formset factory, <a class="reference internal" href="#django.contrib.contenttypes.forms.generic_inlineformset_factory" title="django.contrib.contenttypes.forms.generic_inlineformset_factory"><code class="xref py py-func docutils literal"><span class="pre">generic_inlineformset_factory()</span></code></a>, for use with <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericForeignKey" title="django.contrib.contenttypes.fields.GenericForeignKey"><code class="xref py py-class docutils literal"><span class="pre">GenericForeignKey</span></code></a>.</li> </ul> <dl class="class"> <dt id="django.contrib.contenttypes.forms.BaseGenericInlineFormSet"> <em class="property">class </em><code class="descname">BaseGenericInlineFormSet</code><a class="headerlink" href="#django.contrib.contenttypes.forms.BaseGenericInlineFormSet" title="Permalink to this definition">¶</a></dt> <dd><div class="deprecated"> <p><span class="versionmodified">Deprecated since version 1.7: </span>This class was moved from <code class="docutils literal"><span class="pre">django.contrib.contenttypes.generic</span></code> to <code class="docutils literal"><span class="pre">django.contrib.contenttypes.forms</span></code>. Support for importing from the old location will be removed in Django 1.9.</p> </div> </dd></dl> <dl class="function"> <dt id="django.contrib.contenttypes.forms.generic_inlineformset_factory"> <code class="descname">generic_inlineformset_factory</code>(<em>model</em>, <em>form=ModelForm</em>, <em>formset=BaseGenericInlineFormSet</em>, <em>ct_field="content_type"</em>, <em>fk_field="object_id"</em>, <em>fields=None</em>, <em>exclude=None</em>, <em>extra=3</em>, <em>can_order=False</em>, <em>can_delete=True</em>, <em>max_num=None</em>, <em>formfield_callback=None</em>, <em>validate_max=False</em>, <em>for_concrete_model=True</em>, <em>min_num=None</em>, <em>validate_min=False</em>)<a class="headerlink" href="#django.contrib.contenttypes.forms.generic_inlineformset_factory" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a <code class="docutils literal"><span class="pre">GenericInlineFormSet</span></code> using <a class="reference internal" href="../forms/models.html#django.forms.models.modelformset_factory" title="django.forms.models.modelformset_factory"><code class="xref py py-func docutils literal"><span class="pre">modelformset_factory()</span></code></a>.</p> <p>You must provide <code class="docutils literal"><span class="pre">ct_field</span></code> and <code class="docutils literal"><span class="pre">fk_field</span></code> if they are different from the defaults, <code class="docutils literal"><span class="pre">content_type</span></code> and <code class="docutils literal"><span class="pre">object_id</span></code> respectively. Other parameters are similar to those documented in <a class="reference internal" href="../forms/models.html#django.forms.models.modelformset_factory" title="django.forms.models.modelformset_factory"><code class="xref py py-func docutils literal"><span class="pre">modelformset_factory()</span></code></a> and <a class="reference internal" href="../forms/models.html#django.forms.models.inlineformset_factory" title="django.forms.models.inlineformset_factory"><code class="xref py py-func docutils literal"><span class="pre">inlineformset_factory()</span></code></a>.</p> <p>The <code class="docutils literal"><span class="pre">for_concrete_model</span></code> argument corresponds to the <a class="reference internal" href="#django.contrib.contenttypes.fields.GenericForeignKey.for_concrete_model" title="django.contrib.contenttypes.fields.GenericForeignKey.for_concrete_model"><code class="xref py py-class docutils literal"><span class="pre">for_concrete_model</span></code></a> argument on <code class="docutils literal"><span class="pre">GenericForeignKey</span></code>.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 1.7: </span>This function was moved from <code class="docutils literal"><span class="pre">django.contrib.contenttypes.generic</span></code> to <code class="docutils literal"><span class="pre">django.contrib.contenttypes.forms</span></code>. Support for importing from the old location will be removed in Django 1.9.</p> </div> <div class="versionchanged"> <span class="title">Changed in Django 1.7:</span> <p><code class="docutils literal"><span class="pre">min_num</span></code> and <code class="docutils literal"><span class="pre">validate_min</span></code> were added.</p> </div> </dd></dl> <span class="target" id="module-django.contrib.contenttypes.admin"></span></div> <div class="section" id="s-generic-relations-in-admin"> <span id="generic-relations-in-admin"></span><h3>Generic relations in admin<a class="headerlink" href="#generic-relations-in-admin" title="Permalink to this headline">¶</a></h3> <p>The <a class="reference internal" href="#module-django.contrib.contenttypes.admin" title="django.contrib.contenttypes.admin"><code class="xref py py-mod docutils literal"><span class="pre">django.contrib.contenttypes.admin</span></code></a> module provides <a class="reference internal" href="#django.contrib.contenttypes.admin.GenericTabularInline" title="django.contrib.contenttypes.admin.GenericTabularInline"><code class="xref py py-class docutils literal"><span class="pre">GenericTabularInline</span></code></a> and <a class="reference internal" href="#django.contrib.contenttypes.admin.GenericStackedInline" title="django.contrib.contenttypes.admin.GenericStackedInline"><code class="xref py py-class docutils literal"><span class="pre">GenericStackedInline</span></code></a> (subclasses of <a class="reference internal" href="#django.contrib.contenttypes.admin.GenericInlineModelAdmin" title="django.contrib.contenttypes.admin.GenericInlineModelAdmin"><code class="xref py py-class docutils literal"><span class="pre">GenericInlineModelAdmin</span></code></a>)</p> <p>These classes and functions enable the use of generic relations in forms and the admin. See the <a class="reference internal" href="../../topics/forms/modelforms.html"><span class="doc">model formset</span></a> and <a class="reference internal" href="admin/index.html#using-generic-relations-as-an-inline"><span class="std std-ref">admin</span></a> documentation for more information.</p> <dl class="class"> <dt id="django.contrib.contenttypes.admin.GenericInlineModelAdmin"> <em class="property">class </em><code class="descname">GenericInlineModelAdmin</code><a class="headerlink" href="#django.contrib.contenttypes.admin.GenericInlineModelAdmin" title="Permalink to this definition">¶</a></dt> <dd><p>The <a class="reference internal" href="#django.contrib.contenttypes.admin.GenericInlineModelAdmin" title="django.contrib.contenttypes.admin.GenericInlineModelAdmin"><code class="xref py py-class docutils literal"><span class="pre">GenericInlineModelAdmin</span></code></a> class inherits all properties from an <a class="reference internal" href="admin/index.html#django.contrib.admin.InlineModelAdmin" title="django.contrib.admin.InlineModelAdmin"><code class="xref py py-class docutils literal"><span class="pre">InlineModelAdmin</span></code></a> class. However, it adds a couple of its own for working with the generic relation:</p> <dl class="attribute"> <dt id="django.contrib.contenttypes.admin.GenericInlineModelAdmin.ct_field"> <code class="descname">ct_field</code><a class="headerlink" href="#django.contrib.contenttypes.admin.GenericInlineModelAdmin.ct_field" title="Permalink to this definition">¶</a></dt> <dd><p>The name of the <a class="reference internal" href="#django.contrib.contenttypes.models.ContentType" title="django.contrib.contenttypes.models.ContentType"><code class="xref py py-class docutils literal"><span class="pre">ContentType</span></code></a> foreign key field on the model. Defaults to <code class="docutils literal"><span class="pre">content_type</span></code>.</p> </dd></dl> <dl class="attribute"> <dt id="django.contrib.contenttypes.admin.GenericInlineModelAdmin.ct_fk_field"> <code class="descname">ct_fk_field</code><a class="headerlink" href="#django.contrib.contenttypes.admin.GenericInlineModelAdmin.ct_fk_field" title="Permalink to this definition">¶</a></dt> <dd><p>The name of the integer field that represents the ID of the related object. Defaults to <code class="docutils literal"><span class="pre">object_id</span></code>.</p> </dd></dl> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 1.7: </span>This class was moved from <code class="docutils literal"><span class="pre">django.contrib.contenttypes.generic</span></code> to <code class="docutils literal"><span class="pre">django.contrib.contenttypes.admin</span></code>. Support for importing from the old location will be removed in Django 1.9.</p> </div> </dd></dl> <dl class="class"> <dt id="django.contrib.contenttypes.admin.GenericTabularInline"> <em class="property">class </em><code class="descname">GenericTabularInline</code><a class="headerlink" href="#django.contrib.contenttypes.admin.GenericTabularInline" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <dl class="class"> <dt id="django.contrib.contenttypes.admin.GenericStackedInline"> <em class="property">class </em><code class="descname">GenericStackedInline</code><a class="headerlink" href="#django.contrib.contenttypes.admin.GenericStackedInline" title="Permalink to this definition">¶</a></dt> <dd><p>Subclasses of <a class="reference internal" href="#django.contrib.contenttypes.admin.GenericInlineModelAdmin" title="django.contrib.contenttypes.admin.GenericInlineModelAdmin"><code class="xref py py-class docutils literal"><span class="pre">GenericInlineModelAdmin</span></code></a> with stacked and tabular layouts, respectively.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 1.7: </span>These classes was moved from <code class="docutils literal"><span class="pre">django.contrib.contenttypes.generic</span></code> to <code class="docutils literal"><span class="pre">django.contrib.contenttypes.admin</span></code>. Support for importing from the old location will be removed in Django 1.9.</p> </div> </dd></dl> </div> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">The contenttypes framework</a><ul> <li><a class="reference internal" href="#overview">Overview</a></li> <li><a class="reference internal" href="#installing-the-contenttypes-framework">Installing the contenttypes framework</a></li> <li><a class="reference internal" href="#the-contenttype-model">The <code class="docutils literal"><span class="pre">ContentType</span></code> model</a></li> <li><a class="reference internal" href="#methods-on-contenttype-instances">Methods on <code class="docutils literal"><span class="pre">ContentType</span></code> instances</a><ul> <li><a class="reference internal" href="#the-contenttypemanager">The <code class="docutils literal"><span class="pre">ContentTypeManager</span></code></a></li> </ul> </li> <li><a class="reference internal" href="#generic-relations">Generic relations</a><ul> <li><a class="reference internal" href="#reverse-generic-relations">Reverse generic relations</a></li> <li><a class="reference internal" href="#generic-relations-and-aggregation">Generic relations and aggregation</a></li> <li><a class="reference internal" href="#generic-relation-in-forms">Generic relation in forms</a></li> <li><a class="reference internal" href="#generic-relations-in-admin">Generic relations in admin</a></li> </ul> </li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="auth.html"><code class="docutils literal"><span class="pre">django.contrib.auth</span></code></a></li> <li>Next: <a href="flatpages.html">The flatpages app</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../index.html">Django 1.8.19 documentation</a> <ul><li><a href="../index.html">API Reference</a> <ul><li><a href="index.html"><code class="docutils literal"><span class="pre">contrib</span></code> packages</a> <ul><li>The contenttypes framework</li></ul> </li></ul></li></ul> </li> </ul> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/ref/contrib/contenttypes.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <form class="search" action="../../search.html" method="get"> <div><input type="text" name="q" /></div> <div><input type="submit" value="Go" /></div> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Mar 10, 2018</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="auth.html" title="<code class="docutils literal"><span class="pre">django.contrib.auth</span></code>">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="flatpages.html" title="The flatpages app">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>