{"title":"The contenttypes framework","version":"4.1","locale":"en","docname":"ref/contrib/contenttypes","url":"/en/4.1/ref/contrib/contenttypes/","canonical":"https://djangodocs.dev/en/4.1/ref/contrib/contenttypes/","summary":"Django includes a contenttypes application that can track all of the models installed in your Django-powered project, providing a high-level, generic interface for…","html":"<span id=\"the-contenttypes-framework\"></span><h1>The contenttypes framework<a class=\"heading-anchor\" href=\"#module-django.contrib.contenttypes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<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 notranslate\">contenttypes</code></a> application that can\ntrack all of the models installed in your Django-powered project, providing a\nhigh-level, generic interface for working with your models.</p>\n<section id=\"overview\">\n<h2>Overview<a class=\"heading-anchor\" href=\"#overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>At the heart of the contenttypes application is the\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> model, which lives at\n<code class=\"docutils literal notranslate\">django.contrib.contenttypes.models.ContentType</code>. Instances of\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> represent and store\ninformation about the models installed in your project, and new instances of\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> are automatically\ncreated whenever new models are installed.</p>\n<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 notranslate\">ContentType</code></a> have\nmethods for returning the model classes they represent and for querying objects\nfrom 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 notranslate\">ContentType</code></a>\nalso has a <a class=\"reference internal\" href=\"/en/4.1/topics/db/managers/#custom-managers\"><span class=\"std std-ref\">custom manager</span></a> that adds methods for\nworking 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 notranslate\">ContentType</code></a> and for\nobtaining 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 notranslate\">ContentType</code></a>\nfor a particular model.</p>\n<p>Relations between your models and\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> can also be used to\nenable “generic” relationships between an instance of one of your\nmodels and instances of any model you have installed.</p>\n</section>\n<section id=\"installing-the-contenttypes-framework\">\n<h2>Installing the contenttypes framework<a class=\"heading-anchor\" href=\"#installing-the-contenttypes-framework\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The contenttypes framework is included in the default\n<a class=\"reference internal\" href=\"/en/4.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> list created by <code class=\"docutils literal notranslate\">django-admin startproject</code>,\nbut if you’ve removed it or if you manually set up your\n<a class=\"reference internal\" href=\"/en/4.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> list, you can enable it by adding\n<code class=\"docutils literal notranslate\">'django.contrib.contenttypes'</code> to your <a class=\"reference internal\" href=\"/en/4.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> setting.</p>\n<p>It’s generally a good idea to have the contenttypes framework\ninstalled; several of Django’s other bundled applications require it:</p>\n<ul class=\"simple\">\n<li><p>The admin application uses it to log the history of each object\nadded or changed through the admin interface.</p></li>\n<li><p>Django’s <a class=\"reference internal\" href=\"/en/4.1/topics/auth/#module-django.contrib.auth\" title=\"django.contrib.auth: Django's authentication framework.\"><code class=\"xref py py-mod docutils literal notranslate\">authentication framework</code></a> uses it\nto tie user permissions to specific models.</p></li>\n</ul>\n</section>\n<section id=\"the-contenttype-model\">\n<h2>The <code class=\"docutils literal notranslate\">ContentType</code> model<a class=\"heading-anchor\" href=\"#the-contenttype-model\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.models.ContentType\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">ContentType</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.models.ContentType\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<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 notranslate\">ContentType</code></a>\nhas two fields which, taken together, uniquely describe an installed\nmodel:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.models.ContentType.app_label\">\n<span class=\"sig-name descname\">app_label</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.models.ContentType.app_label\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name of the application the model is part of. This is taken from\nthe <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 notranslate\">app_label</code></a> attribute of the model, and includes only the\n<em>last</em> part of the application’s Python import path;\n<code class=\"docutils literal notranslate\">django.contrib.contenttypes</code>, for example, becomes an\n<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 notranslate\">app_label</code></a> of <code class=\"docutils literal notranslate\">contenttypes</code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.models.ContentType.model\">\n<span class=\"sig-name descname\">model</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.models.ContentType.model\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name of the model class.</p>\n</dd></dl>\n\n<p>Additionally, the following property is available:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.models.ContentType.name\">\n<span class=\"sig-name descname\">name</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.models.ContentType.name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The human-readable name of the content type. This is taken from the\n<a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#django.db.models.Field.verbose_name\" title=\"django.db.models.Field.verbose_name\"><code class=\"xref py py-attr docutils literal notranslate\">verbose_name</code></a>\nattribute of the model.</p>\n</dd></dl>\n\n</dd></dl>\n\n<p>Let’s look at an example to see how this works. If you already have\nthe <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 notranslate\">contenttypes</code></a> application installed, and then add\n<a class=\"reference internal\" href=\"/en/4.1/ref/contrib/sites/#module-django.contrib.sites\" title=\"django.contrib.sites: Lets you operate multiple websites from the same database and Django project\"><code class=\"xref py py-mod docutils literal notranslate\">the sites application</code></a> to your\n<a class=\"reference internal\" href=\"/en/4.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a> setting and run <code class=\"docutils literal notranslate\">manage.py migrate</code> to install it,\nthe model <a class=\"reference internal\" href=\"/en/4.1/ref/contrib/sites/#django.contrib.sites.models.Site\" title=\"django.contrib.sites.models.Site\"><code class=\"xref py py-class docutils literal notranslate\">django.contrib.sites.models.Site</code></a> will be installed into\nyour database. Along with it a new instance of\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> will be\ncreated with the following values:</p>\n<ul class=\"simple\">\n<li><p><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 notranslate\">app_label</code></a>\nwill be set to <code class=\"docutils literal notranslate\">'sites'</code> (the last part of the Python\npath <code class=\"docutils literal notranslate\">django.contrib.sites</code>).</p></li>\n<li><p><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 notranslate\">model</code></a>\nwill be set to <code class=\"docutils literal notranslate\">'site'</code>.</p></li>\n</ul>\n</section>\n<section id=\"methods-on-contenttype-instances\">\n<h2>Methods on <code class=\"docutils literal notranslate\">ContentType</code> instances<a class=\"heading-anchor\" href=\"#methods-on-contenttype-instances\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<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 notranslate\">ContentType</code></a> instance has\nmethods that allow you to get from a\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> instance to the\nmodel it represents, or to retrieve objects from that model:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.models.ContentType.get_object_for_this_type\">\n<span class=\"sig-prename descclassname\">ContentType.</span><span class=\"sig-name descname\">get_object_for_this_type</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.models.ContentType.get_object_for_this_type\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Takes a set of valid <a class=\"reference internal\" href=\"/en/4.1/topics/db/queries/#field-lookups-intro\"><span class=\"std std-ref\">lookup arguments</span></a> for the\nmodel 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 notranslate\">ContentType</code></a>\nrepresents, and does\n<a class=\"reference internal\" href=\"/en/4.1/ref/models/querysets/#django.db.models.query.QuerySet.get\" title=\"django.db.models.query.QuerySet.get\"><code class=\"xref py py-meth docutils literal notranslate\">a get() lookup</code></a>\non that model, returning the corresponding object.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.models.ContentType.model_class\">\n<span class=\"sig-prename descclassname\">ContentType.</span><span class=\"sig-name descname\">model_class</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.models.ContentType.model_class\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns the model class represented by this\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> instance.</p>\n</dd></dl>\n\n<p>For example, we could look up the\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> for the\n<a class=\"reference internal\" href=\"/en/4.1/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a> model:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.contenttypes.models</span> <span class=\"kn\">import</span> ContentType\n<span class=\"gp\">&gt;&gt;&gt; </span>user_type <span class=\"o\">=</span> ContentType<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>app_label<span class=\"o\">=</span><span class=\"s1\">&#39;auth&#39;</span><span class=\"p\">,</span> model<span class=\"o\">=</span><span class=\"s1\">&#39;user&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>user_type\n<span class=\"go\">&lt;ContentType: user&gt;</span>\n</code></pre></div>\n<p>And then use it to query for a particular\n<a class=\"reference internal\" href=\"/en/4.1/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\">User</code></a>, or to get access\nto the <code class=\"docutils literal notranslate\">User</code> model class:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>user_type<span class=\"o\">.</span>model_class<span class=\"p\">()</span>\n<span class=\"go\">&lt;class &#39;django.contrib.auth.models.User&#39;&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>user_type<span class=\"o\">.</span>get_object_for_this_type<span class=\"p\">(</span>username<span class=\"o\">=</span><span class=\"s1\">&#39;Guido&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;User: Guido&gt;</span>\n</code></pre></div>\n<p>Together,\n<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 notranslate\">get_object_for_this_type()</code></a>\nand <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 notranslate\">model_class()</code></a> enable\ntwo extremely important use cases:</p>\n<ol class=\"arabic simple\">\n<li><p>Using these methods, you can write high-level generic code that\nperforms queries on any installed model – instead of importing and\nusing a single specific model class, you can pass an <code class=\"docutils literal notranslate\">app_label</code> and\n<code class=\"docutils literal notranslate\">model</code> into a\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> lookup at\nruntime, and then work with the model class or retrieve objects from it.</p></li>\n<li><p>You can relate another model to\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> as a way of\ntying instances of it to particular model classes, and use these methods\nto get access to those model classes.</p></li>\n</ol>\n<p>Several of Django’s bundled applications make use of the latter technique.\nFor example,\n<a class=\"reference internal\" href=\"/en/4.1/ref/contrib/auth/#django.contrib.auth.models.Permission\" title=\"django.contrib.auth.models.Permission\"><code class=\"xref py py-class docutils literal notranslate\">the permissions system</code></a> in\nDjango’s authentication framework uses a\n<a class=\"reference internal\" href=\"/en/4.1/ref/contrib/auth/#django.contrib.auth.models.Permission\" title=\"django.contrib.auth.models.Permission\"><code class=\"xref py py-class docutils literal notranslate\">Permission</code></a> model with a foreign\nkey 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 notranslate\">ContentType</code></a>; this lets\n<a class=\"reference internal\" href=\"/en/4.1/ref/contrib/auth/#django.contrib.auth.models.Permission\" title=\"django.contrib.auth.models.Permission\"><code class=\"xref py py-class docutils literal notranslate\">Permission</code></a> represent concepts like\n“can add blog entry” or “can delete news story”.</p>\n<section id=\"the-contenttypemanager\">\n<h3>The <code class=\"docutils literal notranslate\">ContentTypeManager</code><a class=\"heading-anchor\" href=\"#the-contenttypemanager\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.models.ContentTypeManager\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">ContentTypeManager</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.models.ContentTypeManager\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<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 notranslate\">ContentType</code></a> also has a custom\nmanager, <a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentTypeManager\" title=\"django.contrib.contenttypes.models.ContentTypeManager\"><code class=\"xref py py-class docutils literal notranslate\">ContentTypeManager</code></a>,\nwhich adds the following methods:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.models.ContentTypeManager.clear_cache\">\n<span class=\"sig-name descname\">clear_cache</span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.models.ContentTypeManager.clear_cache\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Clears an internal cache used by\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> to keep track\nof models for which it has created\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> instances. You\nprobably won’t ever need to call this method yourself; Django will call\nit automatically when it’s needed.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.models.ContentTypeManager.get_for_id\">\n<span class=\"sig-name descname\">get_for_id</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">id</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.models.ContentTypeManager.get_for_id\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<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 notranslate\">ContentType</code></a> by ID.\nSince this method uses the same shared cache as\n<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 notranslate\">get_for_model()</code></a>,\nit’s preferred to use this method over the usual\n<code class=\"docutils literal notranslate\">ContentType.objects.get(pk=id)</code></p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.models.ContentTypeManager.get_for_model\">\n<span class=\"sig-name descname\">get_for_model</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">model</em>, <em class=\"sig-param\">for_concrete_model<span class=\"o\">=</span><span class=\"default_value\">True</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.models.ContentTypeManager.get_for_model\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Takes either a model class or an instance of a model, and returns the\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> instance\nrepresenting that model. <code class=\"docutils literal notranslate\">for_concrete_model=False</code> allows fetching\nthe <a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> of a proxy\nmodel.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.models.ContentTypeManager.get_for_models\">\n<span class=\"sig-name descname\">get_for_models</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"o\">*</span>models</em>, <em class=\"sig-param\">for_concrete_models<span class=\"o\">=</span><span class=\"default_value\">True</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.models.ContentTypeManager.get_for_models\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Takes a variadic number of model classes, and returns a dictionary\nmapping the model classes to the\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> instances\nrepresenting them. <code class=\"docutils literal notranslate\">for_concrete_models=False</code> allows fetching the\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> of proxy\nmodels.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.models.ContentTypeManager.get_by_natural_key\">\n<span class=\"sig-name descname\">get_by_natural_key</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">app_label</em>, <em class=\"sig-param\">model</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.models.ContentTypeManager.get_by_natural_key\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<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 notranslate\">ContentType</code></a>\ninstance uniquely identified by the given application label and model\nname. The primary purpose of this method is to allow\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> objects to be\nreferenced via a <a class=\"reference internal\" href=\"/en/4.1/topics/serialization/#topics-serialization-natural-keys\"><span class=\"std std-ref\">natural key</span></a>\nduring deserialization.</p>\n</dd></dl>\n\n</dd></dl>\n\n<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 notranslate\">get_for_model()</code></a> method is especially\nuseful when you know you need to work with a\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> but don’t\nwant to go to the trouble of obtaining the model’s metadata to perform a manual\nlookup:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.models</span> <span class=\"kn\">import</span> User\n<span class=\"gp\">&gt;&gt;&gt; </span>ContentType<span class=\"o\">.</span>objects<span class=\"o\">.</span>get_for_model<span class=\"p\">(</span>User<span class=\"p\">)</span>\n<span class=\"go\">&lt;ContentType: user&gt;</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"generic-relations\">\n<span id=\"module-django.contrib.contenttypes.fields\"></span><span id=\"id1\"></span><h2>Generic relations<a class=\"heading-anchor\" href=\"#generic-relations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Adding a foreign key from one of your own models to\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> allows your model to\neffectively tie itself to another model class, as in the example of the\n<a class=\"reference internal\" href=\"/en/4.1/ref/contrib/auth/#django.contrib.auth.models.Permission\" title=\"django.contrib.auth.models.Permission\"><code class=\"xref py py-class docutils literal notranslate\">Permission</code></a> model above. But it’s possible\nto go one step further and use\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> to enable truly\ngeneric (sometimes called “polymorphic”) relationships between models.</p>\n<p>For example, it could be used for a tagging system like so:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.contenttypes.fields</span> <span class=\"kn\">import</span> GenericForeignKey\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.contenttypes.models</span> <span class=\"kn\">import</span> ContentType\n<span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">class</span> <span class=\"nc\">TaggedItem</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    tag <span class=\"o\">=</span> models<span class=\"o\">.</span>SlugField<span class=\"p\">()</span>\n    content_type <span class=\"o\">=</span> models<span class=\"o\">.</span>ForeignKey<span class=\"p\">(</span>ContentType<span class=\"p\">,</span> on_delete<span class=\"o\">=</span>models<span class=\"o\">.</span>CASCADE<span class=\"p\">)</span>\n    object_id <span class=\"o\">=</span> models<span class=\"o\">.</span>PositiveIntegerField<span class=\"p\">()</span>\n    content_object <span class=\"o\">=</span> GenericForeignKey<span class=\"p\">(</span><span class=\"s1\">&#39;content_type&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;object_id&#39;</span><span class=\"p\">)</span>\n\n    <span class=\"k\">def</span> <span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span>tag\n\n    <span class=\"k\">class</span> <span class=\"nc\">Meta</span><span class=\"p\">:</span>\n        indexes <span class=\"o\">=</span> <span class=\"p\">[</span>\n            models<span class=\"o\">.</span>Index<span class=\"p\">(</span>fields<span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s2\">&quot;content_type&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;object_id&quot;</span><span class=\"p\">]),</span>\n        <span class=\"p\">]</span>\n</code></pre></div>\n<p>A normal <a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> can only “point\nto” one other model, which means that if the <code class=\"docutils literal notranslate\">TaggedItem</code> model used a\n<a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a> it would have to\nchoose one and only one model to store tags for. The contenttypes\napplication provides a special field type (<code class=\"docutils literal notranslate\">GenericForeignKey</code>) which\nworks around this and allows the relationship to be with any\nmodel:</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.fields.GenericForeignKey\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">GenericForeignKey</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.fields.GenericForeignKey\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>There are three parts to setting up a\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.fields.GenericForeignKey\" title=\"django.contrib.contenttypes.fields.GenericForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">GenericForeignKey</code></a>:</p>\n<ol class=\"arabic simple\">\n<li><p>Give your model a <a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a>\nto <a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a>. The usual\nname for this field is “content_type”.</p></li>\n<li><p>Give your model a field that can store primary key values from the\nmodels you’ll be relating to. For most models, this means a\n<a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#django.db.models.PositiveIntegerField\" title=\"django.db.models.PositiveIntegerField\"><code class=\"xref py py-class docutils literal notranslate\">PositiveIntegerField</code></a>. The usual name\nfor this field is “object_id”.</p></li>\n<li><p>Give your model a\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.fields.GenericForeignKey\" title=\"django.contrib.contenttypes.fields.GenericForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">GenericForeignKey</code></a>, and\npass it the names of the two fields described above. If these fields\nare named “content_type” and “object_id”, you can omit this – those\nare the default field names\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.fields.GenericForeignKey\" title=\"django.contrib.contenttypes.fields.GenericForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">GenericForeignKey</code></a> will\nlook for.</p></li>\n</ol>\n<p>Unlike for the <a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a>, a database index is\n<em>not</em> automatically created on the\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.fields.GenericForeignKey\" title=\"django.contrib.contenttypes.fields.GenericForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">GenericForeignKey</code></a>, so it’s\nrecommended that you use\n<a class=\"reference internal\" href=\"/en/4.1/ref/models/options/#django.db.models.Options.indexes\" title=\"django.db.models.Options.indexes\"><code class=\"xref py py-attr docutils literal notranslate\">Meta.indexes</code></a> to add your own\nmultiple column index. This behavior <a class=\"extlink-ticket reference external\" href=\"https://code.djangoproject.com/ticket/23435\">may change</a> in the\nfuture.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.fields.GenericForeignKey.for_concrete_model\">\n<span class=\"sig-name descname\">for_concrete_model</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.fields.GenericForeignKey.for_concrete_model\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>If <code class=\"docutils literal notranslate\">False</code>, the field will be able to reference proxy models. Default\nis <code class=\"docutils literal notranslate\">True</code>. This mirrors the <code class=\"docutils literal notranslate\">for_concrete_model</code> argument to\n<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 notranslate\">get_for_model()</code></a>.</p>\n</dd></dl>\n\n</dd></dl>\n\n<aside class=\"admonition-primary-key-type-compatibility admonition\" role=\"note\">\n<p class=\"admonition-title\">Primary key type compatibility</p>\n<p>The “object_id” field doesn’t have to be the same type as the\nprimary key fields on the related models, but their primary key values\nmust be coercible to the same type as the “object_id” field by its\n<a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#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 notranslate\">get_db_prep_value()</code></a> method.</p>\n<p>For example, if you want to allow generic relations to models with either\n<a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#django.db.models.IntegerField\" title=\"django.db.models.IntegerField\"><code class=\"xref py py-class docutils literal notranslate\">IntegerField</code></a> or\n<a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a> primary key fields, you\ncan use <a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\">CharField</code></a> for the\n“object_id” field on your model since integers can be coerced to\nstrings by <a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#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 notranslate\">get_db_prep_value()</code></a>.</p>\n<p>For maximum flexibility you can use a\n<a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#django.db.models.TextField\" title=\"django.db.models.TextField\"><code class=\"xref py py-class docutils literal notranslate\">TextField</code></a> which doesn’t have a\nmaximum length defined, however this may incur significant performance\npenalties depending on your database backend.</p>\n<p>There is no one-size-fits-all solution for which field type is best. You\nshould evaluate the models you expect to be pointing to and determine\nwhich solution will be most effective for your use case.</p>\n</aside>\n<aside class=\"admonition-serializing-references-to-contenttype-objects admonition\" role=\"note\">\n<p class=\"admonition-title\">Serializing references to <code class=\"docutils literal notranslate\">ContentType</code> objects</p>\n<p>If you’re serializing data (for example, when generating\n<a class=\"reference internal\" href=\"/en/4.1/topics/testing/tools/#django.test.TransactionTestCase.fixtures\" title=\"django.test.TransactionTestCase.fixtures\"><code class=\"xref py py-class docutils literal notranslate\">fixtures</code></a>) from a model that implements\ngeneric relations, you should probably be using a natural key to uniquely\nidentify 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 notranslate\">ContentType</code></a>\nobjects. See <a class=\"reference internal\" href=\"/en/4.1/topics/serialization/#topics-serialization-natural-keys\"><span class=\"std std-ref\">natural keys</span></a> and\n<a class=\"reference internal\" href=\"/en/4.1/ref/django-admin/#cmdoption-dumpdata-natural-foreign\"><code class=\"xref std std-option docutils literal notranslate\">dumpdata --natural-foreign</code></a> for more information.</p>\n</aside>\n<p>This will enable an API similar to the one used for a normal\n<a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a>;\neach <code class=\"docutils literal notranslate\">TaggedItem</code> will have a <code class=\"docutils literal notranslate\">content_object</code> field that returns the\nobject it’s related to, and you can also assign to that field or use it when\ncreating a <code class=\"docutils literal notranslate\">TaggedItem</code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.models</span> <span class=\"kn\">import</span> User\n<span class=\"gp\">&gt;&gt;&gt; </span>guido <span class=\"o\">=</span> User<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>username<span class=\"o\">=</span><span class=\"s1\">&#39;Guido&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t <span class=\"o\">=</span> TaggedItem<span class=\"p\">(</span>content_object<span class=\"o\">=</span>guido<span class=\"p\">,</span> tag<span class=\"o\">=</span><span class=\"s1\">&#39;bdfl&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t<span class=\"o\">.</span>save<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t<span class=\"o\">.</span>content_object\n<span class=\"go\">&lt;User: Guido&gt;</span>\n</code></pre></div>\n<p>If the related object is deleted, the <code class=\"docutils literal notranslate\">content_type</code> and <code class=\"docutils literal notranslate\">object_id</code> fields\nremain set to their original values and the <code class=\"docutils literal notranslate\">GenericForeignKey</code> returns\n<code class=\"docutils literal notranslate\">None</code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>guido<span class=\"o\">.</span>delete<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t<span class=\"o\">.</span>content_object  <span class=\"c1\"># returns None</span>\n</code></pre></div>\n<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 notranslate\">GenericForeignKey</code></a>\nis implemented, you cannot use such fields directly with filters (<code class=\"docutils literal notranslate\">filter()</code>\nand <code class=\"docutils literal notranslate\">exclude()</code>, for example) via the database API. Because a\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.fields.GenericForeignKey\" title=\"django.contrib.contenttypes.fields.GenericForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">GenericForeignKey</code></a> isn’t a\nnormal field object, these examples will <em>not</em> work:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># This will fail</span>\n<span class=\"o\">&gt;&gt;&gt;</span> TaggedItem<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>content_object<span class=\"o\">=</span>guido<span class=\"p\">)</span>\n<span class=\"c1\"># This will also fail</span>\n<span class=\"o\">&gt;&gt;&gt;</span> TaggedItem<span class=\"o\">.</span>objects<span class=\"o\">.</span>get<span class=\"p\">(</span>content_object<span class=\"o\">=</span>guido<span class=\"p\">)</span>\n</code></pre></div>\n<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 notranslate\">GenericForeignKey</code></a>s\ndoes not appear in <a class=\"reference internal\" href=\"/en/4.1/topics/forms/modelforms/#django.forms.ModelForm\" title=\"django.forms.ModelForm\"><code class=\"xref py py-class docutils literal notranslate\">ModelForm</code></a>s.</p>\n<section id=\"reverse-generic-relations\">\n<h3>Reverse generic relations<a class=\"heading-anchor\" href=\"#reverse-generic-relations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.fields.GenericRelation\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">GenericRelation</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.fields.GenericRelation\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.fields.GenericRelation.related_query_name\">\n<span class=\"sig-name descname\">related_query_name</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.fields.GenericRelation.related_query_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The relation on the related object back to this object doesn’t exist by\ndefault. Setting <code class=\"docutils literal notranslate\">related_query_name</code> creates a relation from the\nrelated object back to this one. This allows querying and filtering\nfrom the related object.</p>\n</dd></dl>\n\n</dd></dl>\n\n<p>If you know which models you’ll be using most often, you can also add\na “reverse” generic relationship to enable an additional API. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.contenttypes.fields</span> <span class=\"kn\">import</span> GenericRelation\n<span class=\"kn\">from</span> <span class=\"nn\">django.db</span> <span class=\"kn\">import</span> models\n\n<span class=\"k\">class</span> <span class=\"nc\">Bookmark</span><span class=\"p\">(</span>models<span class=\"o\">.</span>Model<span class=\"p\">):</span>\n    url <span class=\"o\">=</span> models<span class=\"o\">.</span>URLField<span class=\"p\">()</span>\n    tags <span class=\"o\">=</span> GenericRelation<span class=\"p\">(</span>TaggedItem<span class=\"p\">)</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">Bookmark</code> instances will each have a <code class=\"docutils literal notranslate\">tags</code> attribute, which can\nbe used to retrieve their associated <code class=\"docutils literal notranslate\">TaggedItems</code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>b <span class=\"o\">=</span> Bookmark<span class=\"p\">(</span>url<span class=\"o\">=</span><span class=\"s1\">&#39;https://www.djangoproject.com/&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>b<span class=\"o\">.</span>save<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t1 <span class=\"o\">=</span> TaggedItem<span class=\"p\">(</span>content_object<span class=\"o\">=</span>b<span class=\"p\">,</span> tag<span class=\"o\">=</span><span class=\"s1\">&#39;django&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t1<span class=\"o\">.</span>save<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t2 <span class=\"o\">=</span> TaggedItem<span class=\"p\">(</span>content_object<span class=\"o\">=</span>b<span class=\"p\">,</span> tag<span class=\"o\">=</span><span class=\"s1\">&#39;python&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>t2<span class=\"o\">.</span>save<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>b<span class=\"o\">.</span>tags<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet [&lt;TaggedItem: django&gt;, &lt;TaggedItem: python&gt;]&gt;</span>\n</code></pre></div>\n<p>You can also use <code class=\"docutils literal notranslate\">add()</code>, <code class=\"docutils literal notranslate\">create()</code>, or <code class=\"docutils literal notranslate\">set()</code> to create\nrelationships:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>t3 <span class=\"o\">=</span> TaggedItem<span class=\"p\">(</span>tag<span class=\"o\">=</span><span class=\"s1\">&#39;Web development&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>b<span class=\"o\">.</span>tags<span class=\"o\">.</span>add<span class=\"p\">(</span>t3<span class=\"p\">,</span> bulk<span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>b<span class=\"o\">.</span>tags<span class=\"o\">.</span>create<span class=\"p\">(</span>tag<span class=\"o\">=</span><span class=\"s1\">&#39;Web framework&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;TaggedItem: Web framework&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>b<span class=\"o\">.</span>tags<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet [&lt;TaggedItem: django&gt;, &lt;TaggedItem: python&gt;, &lt;TaggedItem: Web development&gt;, &lt;TaggedItem: Web framework&gt;]&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>b<span class=\"o\">.</span>tags<span class=\"o\">.</span>set<span class=\"p\">([</span>t1<span class=\"p\">,</span> t3<span class=\"p\">])</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>b<span class=\"o\">.</span>tags<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet [&lt;TaggedItem: django&gt;, &lt;TaggedItem: Web development&gt;]&gt;</span>\n</code></pre></div>\n<p>The <code class=\"docutils literal notranslate\">remove()</code> call will bulk delete the specified model objects:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>b<span class=\"o\">.</span>tags<span class=\"o\">.</span>remove<span class=\"p\">(</span>t3<span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>b<span class=\"o\">.</span>tags<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet [&lt;TaggedItem: django&gt;]&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>TaggedItem<span class=\"o\">.</span>objects<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet [&lt;TaggedItem: django&gt;]&gt;</span>\n</code></pre></div>\n<p>The <code class=\"docutils literal notranslate\">clear()</code> method can be used to bulk delete all related objects for an\ninstance:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>b<span class=\"o\">.</span>tags<span class=\"o\">.</span>clear<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>b<span class=\"o\">.</span>tags<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet []&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>TaggedItem<span class=\"o\">.</span>objects<span class=\"o\">.</span>all<span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet []&gt;</span>\n</code></pre></div>\n<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 notranslate\">GenericRelation</code></a> with\n<code class=\"docutils literal notranslate\">related_query_name</code> set allows querying from the related object:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>tags <span class=\"o\">=</span> GenericRelation<span class=\"p\">(</span>TaggedItem<span class=\"p\">,</span> related_query_name<span class=\"o\">=</span><span class=\"s1\">&#39;bookmark&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>This enables filtering, ordering, and other query operations on <code class=\"docutils literal notranslate\">Bookmark</code>\nfrom <code class=\"docutils literal notranslate\">TaggedItem</code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"c1\"># Get all tags belonging to bookmarks containing `django` in the url</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>TaggedItem<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>bookmark__url__contains<span class=\"o\">=</span><span class=\"s1\">&#39;django&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;QuerySet [&lt;TaggedItem: django&gt;, &lt;TaggedItem: python&gt;]&gt;</span>\n</code></pre></div>\n<p>If you don’t add the <code class=\"docutils literal notranslate\">related_query_name</code>, you can do the same types of\nlookups manually:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>bookmarks <span class=\"o\">=</span> Bookmark<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>url__contains<span class=\"o\">=</span><span class=\"s1\">&#39;django&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>bookmark_type <span class=\"o\">=</span> ContentType<span class=\"o\">.</span>objects<span class=\"o\">.</span>get_for_model<span class=\"p\">(</span>Bookmark<span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>TaggedItem<span class=\"o\">.</span>objects<span class=\"o\">.</span>filter<span class=\"p\">(</span>content_type__pk<span class=\"o\">=</span>bookmark_type<span class=\"o\">.</span>id<span class=\"p\">,</span> object_id__in<span class=\"o\">=</span>bookmarks<span class=\"p\">)</span>\n<span class=\"go\">&lt;QuerySet [&lt;TaggedItem: django&gt;, &lt;TaggedItem: python&gt;]&gt;</span>\n</code></pre></div>\n<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 notranslate\">GenericForeignKey</code></a>\naccepts the names of the content-type and object-ID fields as\narguments, so too does\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.fields.GenericRelation\" title=\"django.contrib.contenttypes.fields.GenericRelation\"><code class=\"xref py py-class docutils literal notranslate\">GenericRelation</code></a>;\nif the model which has the generic foreign key is using non-default names\nfor those fields, you must pass the names of the fields when setting up a\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.fields.GenericRelation\" title=\"django.contrib.contenttypes.fields.GenericRelation\"><code class=\"xref py py-class docutils literal notranslate\">GenericRelation</code></a> to it. For example, if the <code class=\"docutils literal notranslate\">TaggedItem</code> model\nreferred to above used fields named <code class=\"docutils literal notranslate\">content_type_fk</code> and\n<code class=\"docutils literal notranslate\">object_primary_key</code> to create its generic foreign key, then a\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.fields.GenericRelation\" title=\"django.contrib.contenttypes.fields.GenericRelation\"><code class=\"xref py py-class docutils literal notranslate\">GenericRelation</code></a> back to it would need to be defined like so:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>tags <span class=\"o\">=</span> GenericRelation<span class=\"p\">(</span>\n    TaggedItem<span class=\"p\">,</span>\n    content_type_field<span class=\"o\">=</span><span class=\"s1\">&#39;content_type_fk&#39;</span><span class=\"p\">,</span>\n    object_id_field<span class=\"o\">=</span><span class=\"s1\">&#39;object_primary_key&#39;</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>Note also, that if you delete an object that has a\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.fields.GenericRelation\" title=\"django.contrib.contenttypes.fields.GenericRelation\"><code class=\"xref py py-class docutils literal notranslate\">GenericRelation</code></a>, any objects\nwhich 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 notranslate\">GenericForeignKey</code></a>\npointing at it will be deleted as well. In the example above, this means that\nif a <code class=\"docutils literal notranslate\">Bookmark</code> object were deleted, any <code class=\"docutils literal notranslate\">TaggedItem</code> objects pointing at\nit would be deleted at the same time.</p>\n<p>Unlike <a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">ForeignKey</code></a>,\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.fields.GenericForeignKey\" title=\"django.contrib.contenttypes.fields.GenericForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">GenericForeignKey</code></a> does not accept\nan <a class=\"reference internal\" href=\"/en/4.1/ref/models/fields/#django.db.models.ForeignKey.on_delete\" title=\"django.db.models.ForeignKey.on_delete\"><code class=\"xref py py-attr docutils literal notranslate\">on_delete</code></a> argument to customize this\nbehavior; if desired, you can avoid the cascade-deletion by not using\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.fields.GenericRelation\" title=\"django.contrib.contenttypes.fields.GenericRelation\"><code class=\"xref py py-class docutils literal notranslate\">GenericRelation</code></a>, and alternate\nbehavior can be provided via the <a class=\"reference internal\" href=\"/en/4.1/ref/signals/#django.db.models.signals.pre_delete\" title=\"django.db.models.signals.pre_delete\"><code class=\"xref py py-data docutils literal notranslate\">pre_delete</code></a>\nsignal.</p>\n</section>\n<section id=\"generic-relations-and-aggregation\">\n<h3>Generic relations and aggregation<a class=\"heading-anchor\" href=\"#generic-relations-and-aggregation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference internal\" href=\"/en/4.1/topics/db/aggregation/\"><span class=\"doc\">Django’s database aggregation API</span></a> works with a\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.fields.GenericRelation\" title=\"django.contrib.contenttypes.fields.GenericRelation\"><code class=\"xref py py-class docutils literal notranslate\">GenericRelation</code></a>. For example, you\ncan find out how many tags all the bookmarks have:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>Bookmark<span class=\"o\">.</span>objects<span class=\"o\">.</span>aggregate<span class=\"p\">(</span>Count<span class=\"p\">(</span><span class=\"s1\">&#39;tags&#39;</span><span class=\"p\">))</span>\n<span class=\"go\">{&#39;tags__count&#39;: 3}</span>\n</code></pre></div>\n</section>\n<section id=\"generic-relation-in-forms\">\n<span id=\"module-django.contrib.contenttypes.forms\"></span><h3>Generic relation in forms<a class=\"heading-anchor\" href=\"#generic-relation-in-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<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 notranslate\">django.contrib.contenttypes.forms</code></a> module provides:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"#django.contrib.contenttypes.forms.BaseGenericInlineFormSet\" title=\"django.contrib.contenttypes.forms.BaseGenericInlineFormSet\"><code class=\"xref py py-class docutils literal notranslate\">BaseGenericInlineFormSet</code></a></p></li>\n<li><p>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 notranslate\">generic_inlineformset_factory()</code></a>, for use with\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.fields.GenericForeignKey\" title=\"django.contrib.contenttypes.fields.GenericForeignKey\"><code class=\"xref py py-class docutils literal notranslate\">GenericForeignKey</code></a>.</p></li>\n</ul>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.forms.BaseGenericInlineFormSet\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">BaseGenericInlineFormSet</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.forms.BaseGenericInlineFormSet\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.forms.generic_inlineformset_factory\">\n<span class=\"sig-name descname\">generic_inlineformset_factory</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">model</em>, <em class=\"sig-param\">form<span class=\"o\">=</span><span class=\"default_value\">ModelForm</span></em>, <em class=\"sig-param\">formset<span class=\"o\">=</span><span class=\"default_value\">BaseGenericInlineFormSet</span></em>, <em class=\"sig-param\">ct_field<span class=\"o\">=</span><span class=\"default_value\">'content_type'</span></em>, <em class=\"sig-param\">fk_field<span class=\"o\">=</span><span class=\"default_value\">'object_id'</span></em>, <em class=\"sig-param\">fields<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">exclude<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">extra<span class=\"o\">=</span><span class=\"default_value\">3</span></em>, <em class=\"sig-param\">can_order<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">can_delete<span class=\"o\">=</span><span class=\"default_value\">True</span></em>, <em class=\"sig-param\">max_num<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">formfield_callback<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">validate_max<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">for_concrete_model<span class=\"o\">=</span><span class=\"default_value\">True</span></em>, <em class=\"sig-param\">min_num<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">validate_min<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">absolute_max<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">can_delete_extra<span class=\"o\">=</span><span class=\"default_value\">True</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.forms.generic_inlineformset_factory\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a <code class=\"docutils literal notranslate\">GenericInlineFormSet</code> using\n<a class=\"reference internal\" href=\"/en/4.1/ref/forms/models/#django.forms.models.modelformset_factory\" title=\"django.forms.models.modelformset_factory\"><code class=\"xref py py-func docutils literal notranslate\">modelformset_factory()</code></a>.</p>\n<p>You must provide <code class=\"docutils literal notranslate\">ct_field</code> and <code class=\"docutils literal notranslate\">fk_field</code> if they are different from\nthe defaults, <code class=\"docutils literal notranslate\">content_type</code> and <code class=\"docutils literal notranslate\">object_id</code> respectively. Other\nparameters are similar to those documented in\n<a class=\"reference internal\" href=\"/en/4.1/ref/forms/models/#django.forms.models.modelformset_factory\" title=\"django.forms.models.modelformset_factory\"><code class=\"xref py py-func docutils literal notranslate\">modelformset_factory()</code></a> and\n<a class=\"reference internal\" href=\"/en/4.1/ref/forms/models/#django.forms.models.inlineformset_factory\" title=\"django.forms.models.inlineformset_factory\"><code class=\"xref py py-func docutils literal notranslate\">inlineformset_factory()</code></a>.</p>\n<p>The <code class=\"docutils literal notranslate\">for_concrete_model</code> argument corresponds to the\n<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 notranslate\">for_concrete_model</code></a>\nargument on <code class=\"docutils literal notranslate\">GenericForeignKey</code>.</p>\n</dd></dl>\n\n</section>\n<section id=\"generic-relations-in-admin\">\n<span id=\"module-django.contrib.contenttypes.admin\"></span><h3>Generic relations in admin<a class=\"heading-anchor\" href=\"#generic-relations-in-admin\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<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 notranslate\">django.contrib.contenttypes.admin</code></a> module provides\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.admin.GenericTabularInline\" title=\"django.contrib.contenttypes.admin.GenericTabularInline\"><code class=\"xref py py-class docutils literal notranslate\">GenericTabularInline</code></a> and\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.admin.GenericStackedInline\" title=\"django.contrib.contenttypes.admin.GenericStackedInline\"><code class=\"xref py py-class docutils literal notranslate\">GenericStackedInline</code></a> (subclasses of\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.admin.GenericInlineModelAdmin\" title=\"django.contrib.contenttypes.admin.GenericInlineModelAdmin\"><code class=\"xref py py-class docutils literal notranslate\">GenericInlineModelAdmin</code></a>)</p>\n<p>These classes and functions enable the use of generic relations in forms\nand the admin. See the <a class=\"reference internal\" href=\"/en/4.1/topics/forms/modelforms/\"><span class=\"doc\">model formset</span></a> and\n<a class=\"reference internal\" href=\"/en/4.1/ref/contrib/admin/#using-generic-relations-as-an-inline\"><span class=\"std std-ref\">admin</span></a> documentation for more\ninformation.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.admin.GenericInlineModelAdmin\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">GenericInlineModelAdmin</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.admin.GenericInlineModelAdmin\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<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 notranslate\">GenericInlineModelAdmin</code></a>\nclass inherits all properties from an\n<a class=\"reference internal\" href=\"/en/4.1/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin\" title=\"django.contrib.admin.InlineModelAdmin\"><code class=\"xref py py-class docutils literal notranslate\">InlineModelAdmin</code></a> class. However,\nit adds a couple of its own for working with the generic relation:</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.admin.GenericInlineModelAdmin.ct_field\">\n<span class=\"sig-name descname\">ct_field</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.admin.GenericInlineModelAdmin.ct_field\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name of the\n<a class=\"reference internal\" href=\"#django.contrib.contenttypes.models.ContentType\" title=\"django.contrib.contenttypes.models.ContentType\"><code class=\"xref py py-class docutils literal notranslate\">ContentType</code></a> foreign key\nfield on the model. Defaults to <code class=\"docutils literal notranslate\">content_type</code>.</p>\n</dd></dl>\n\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.admin.GenericInlineModelAdmin.ct_fk_field\">\n<span class=\"sig-name descname\">ct_fk_field</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.admin.GenericInlineModelAdmin.ct_fk_field\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The name of the integer field that represents the ID of the related\nobject. Defaults to <code class=\"docutils literal notranslate\">object_id</code>.</p>\n</dd></dl>\n\n</dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.admin.GenericTabularInline\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">GenericTabularInline</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.admin.GenericTabularInline\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.contrib.contenttypes.admin.GenericStackedInline\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">GenericStackedInline</span><a class=\"heading-anchor\" href=\"#django.contrib.contenttypes.admin.GenericStackedInline\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<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 notranslate\">GenericInlineModelAdmin</code></a> with stacked and tabular\nlayouts, respectively.</p>\n</dd></dl>\n\n</section>\n</section>","rootId":"module-django.contrib.contenttypes","toc":[{"title":"Overview","anchor":"overview","children":[]},{"title":"Installing the contenttypes framework","anchor":"installing-the-contenttypes-framework","children":[]},{"title":"The ContentType model","anchor":"the-contenttype-model","children":[]},{"title":"Methods on ContentType instances","anchor":"methods-on-contenttype-instances","children":[{"title":"The ContentTypeManager","anchor":"the-contenttypemanager","children":[]}]},{"title":"Generic relations","anchor":"generic-relations","children":[{"title":"Reverse generic relations","anchor":"reverse-generic-relations","children":[]},{"title":"Generic relations and aggregation","anchor":"generic-relations-and-aggregation","children":[]},{"title":"Generic relation in forms","anchor":"generic-relation-in-forms","children":[]},{"title":"Generic relations in admin","anchor":"generic-relations-in-admin","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/en/4.1/ref/"},{"docname":"ref/contrib/index","title":"contrib packages","url":"/en/4.1/ref/contrib/"}],"prev":{"docname":"ref/contrib/auth","title":"django.contrib.auth","url":"/en/4.1/ref/contrib/auth/"},"next":{"docname":"ref/contrib/flatpages","title":"The flatpages app","url":"/en/4.1/ref/contrib/flatpages/"},"formats":{"html":"/en/4.1/ref/contrib/contenttypes/","markdown":"/en/4.1/ref/contrib/contenttypes.md","json":"/en/4.1/ref/contrib/contenttypes.json"},"source":"https://github.com/django/django/blob/stable/4.1.x/docs/ref/contrib/contenttypes.txt","official":"https://docs.djangoproject.com/en/4.1/ref/contrib/contenttypes/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}