[{"data":1,"prerenderedAt":36},["ShallowReactive",2],{"article-django-genericforeignkey-with-genericrelation-1p54":3},{"type_of":4,"id":5,"title":6,"description":7,"readable_publish_date":8,"slug":9,"path":10,"url":11,"comments_count":12,"public_reactions_count":13,"collection_id":14,"published_timestamp":15,"language":14,"subforem_id":14,"positive_reactions_count":13,"cover_image":16,"social_image":17,"canonical_url":11,"created_at":15,"edited_at":14,"crossposted_at":14,"published_at":15,"last_comment_at":15,"reading_time_minutes":18,"tag_list":19,"tags":20,"body_html":25,"body_markdown":26,"user":27},"article",353064,"Django GenericForeignKey with GenericRelation","Introduction   I hope it's no offense that, Django is a powerful tool for the web. Today I'l...","Jun 11 '20","django-genericforeignkey-with-genericrelation-1p54","\u002Fashraful\u002Fdjango-genericforeignkey-with-genericrelation-1p54","https:\u002F\u002Fdev.to\u002Fashraful\u002Fdjango-genericforeignkey-with-genericrelation-1p54",0,17,null,"2020-06-11T09:50:54Z","https:\u002F\u002Fmedia2.dev.to\u002Fdynamic\u002Fimage\u002Fwidth=1000,height=420,fit=cover,gravity=auto,format=auto\u002Fhttps%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9jigqpd1f45ht5d5qo6c.jpg","https:\u002F\u002Fmedia2.dev.to\u002Fdynamic\u002Fimage\u002Fwidth=1000,height=500,fit=cover,gravity=auto,format=auto\u002Fhttps%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9jigqpd1f45ht5d5qo6c.jpg",2,"python, django, orm, webdev",[21,22,23,24],"python","django","orm","webdev","\u003Ch2>\n  \u003Ca name=\"introduction\" href=\"#introduction\">\n  \u003C\u002Fa>\n  Introduction\n\u003C\u002Fh2>\n\n\u003Cp>I hope it's no offense that, Django is a powerful tool for the web. Today I'll talk about \u003Ccode>GenericForeignKey\u003C\u002Fcode> and \u003Ccode>GenericRelation\u003C\u002Fcode>. End of the article you will know about \u003Ccode>GenericForeignKey\u003C\u002Fcode> and \u003Ccode>GenericRelation\u003C\u002Fcode>.\u003C\u002Fp>\n\n\u003Ch2>\n  \u003Ca name=\"what-is-genericforeignkey\" href=\"#what-is-genericforeignkey\">\n  \u003C\u002Fa>\n  What is GenericForeignKey?\n\u003C\u002Fh2>\n\n\u003Cp>\u003Ccode>GenericForeignKey\u003C\u002Fcode> is a foreign key with any model with the help of content type. Still confusing? Let's see some example.\u003C\u002Fp>\n\n\u003Cp>Suppose we have book system where there are some models like following,\u003Cbr>\n\u003C\u002Fp>\n\n\u003Cdiv class=\"highlight js-code-highlight\">\n\u003Cpre class=\"highlight python\">\u003Ccode>\u003Cspan class=\"k\">class\u003C\u002Fspan> \u003Cspan class=\"nc\">Author\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\u003Cspan class=\"n\">models\u003C\u002Fspan>\u003Cspan class=\"p\">.\u003C\u002Fspan>\u003Cspan class=\"n\">Model\u003C\u002Fspan>\u003Cspan class=\"p\">):\u003C\u002Fspan>\n    \u003Cspan class=\"n\">name\u003C\u002Fspan> \u003Cspan class=\"o\">=\u003C\u002Fspan> \u003Cspan class=\"n\">models\u003C\u002Fspan>\u003Cspan class=\"p\">.\u003C\u002Fspan>\u003Cspan class=\"nc\">CharField\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\u003Cspan class=\"n\">max_length\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"mi\">64\u003C\u002Fspan>\u003Cspan class=\"p\">)\u003C\u002Fspan>\n    \u003Cspan class=\"c1\"># Other fields\n\u003C\u002Fspan>\n\n\u003Cspan class=\"k\">class\u003C\u002Fspan> \u003Cspan class=\"nc\">Reader\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\u003Cspan class=\"n\">models\u003C\u002Fspan>\u003Cspan class=\"p\">.\u003C\u002Fspan>\u003Cspan class=\"n\">Model\u003C\u002Fspan>\u003Cspan class=\"p\">):\u003C\u002Fspan>\n    \u003Cspan class=\"n\">name\u003C\u002Fspan> \u003Cspan class=\"o\">=\u003C\u002Fspan> \u003Cspan class=\"n\">models\u003C\u002Fspan>\u003Cspan class=\"p\">.\u003C\u002Fspan>\u003Cspan class=\"nc\">CharField\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\u003Cspan class=\"n\">max_length\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"mi\">64\u003C\u002Fspan>\u003Cspan class=\"p\">)\u003C\u002Fspan>\n    \u003Cspan class=\"c1\"># Other fields\n\u003C\u002Fspan>\n\n\u003Cspan class=\"k\">class\u003C\u002Fspan> \u003Cspan class=\"nc\">ActivityLog\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\u003Cspan class=\"n\">models\u003C\u002Fspan>\u003Cspan class=\"p\">.\u003C\u002Fspan>\u003Cspan class=\"n\">Model\u003C\u002Fspan>\u003Cspan class=\"p\">):\u003C\u002Fspan>\n    \u003Cspan class=\"c1\"># Here we want to track everything that other users do. So, I'll add GenericForeignKey\n\u003C\u002Fspan>    \u003Cspan class=\"n\">content_type\u003C\u002Fspan> \u003Cspan class=\"o\">=\u003C\u002Fspan> \u003Cspan class=\"n\">models\u003C\u002Fspan>\u003Cspan class=\"p\">.\u003C\u002Fspan>\u003Cspan class=\"nc\">ForeignKey\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\n        \u003Cspan class=\"n\">ContentType\u003C\u002Fspan>\u003Cspan class=\"p\">,\u003C\u002Fspan> \u003Cspan class=\"n\">default\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"bp\">None\u003C\u002Fspan>\u003Cspan class=\"p\">,\u003C\u002Fspan> \u003Cspan class=\"n\">null\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"bp\">True\u003C\u002Fspan>\u003Cspan class=\"p\">,\u003C\u002Fspan> \u003Cspan class=\"n\">on_delete\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"n\">models\u003C\u002Fspan>\u003Cspan class=\"p\">.\u003C\u002Fspan>\u003Cspan class=\"n\">SET_NULL\u003C\u002Fspan>\u003Cspan class=\"p\">,\u003C\u002Fspan> \u003Cspan class=\"n\">related_name\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"s\">activity_logs\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"p\">)\u003C\u002Fspan>\n    \u003Cspan class=\"n\">object_id\u003C\u002Fspan> \u003Cspan class=\"o\">=\u003C\u002Fspan> \u003Cspan class=\"n\">models\u003C\u002Fspan>\u003Cspan class=\"p\">.\u003C\u002Fspan>\u003Cspan class=\"nc\">BigIntegerField\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\u003Cspan class=\"n\">default\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"bp\">None\u003C\u002Fspan>\u003Cspan class=\"p\">,\u003C\u002Fspan> \u003Cspan class=\"n\">null\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"bp\">True\u003C\u002Fspan>\u003Cspan class=\"p\">)\u003C\u002Fspan>\n    \u003Cspan class=\"nb\">object\u003C\u002Fspan> \u003Cspan class=\"o\">=\u003C\u002Fspan> \u003Cspan class=\"nc\">GenericForeignKey\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\u003Cspan class=\"n\">ct_field\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"sh\">\"\u003C\u002Fspan>\u003Cspan class=\"s\">content_type\u003C\u002Fspan>\u003Cspan class=\"sh\">\"\u003C\u002Fspan>\u003Cspan class=\"p\">,\u003C\u002Fspan> \u003Cspan class=\"n\">fk_field\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"sh\">\"\u003C\u002Fspan>\u003Cspan class=\"s\">object_id\u003C\u002Fspan>\u003Cspan class=\"sh\">\"\u003C\u002Fspan>\u003Cspan class=\"p\">)\u003C\u002Fspan>\n    \u003Cspan class=\"c1\"># Other fields\n\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cdiv class=\"highlight__panel js-actions-panel\">\n\u003Cdiv class=\"highlight__panel-action js-fullscreen-code-action\">\n    \u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\">\u003Ctitle>Enter fullscreen mode\u003C\u002Ftitle>\n    \u003Cpath d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\">\u003C\u002Fpath>\n\u003C\u002Fsvg>\n\n    \u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\">\u003Ctitle>Exit fullscreen mode\u003C\u002Ftitle>\n    \u003Cpath d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\">\u003C\u002Fpath>\n\u003C\u002Fsvg>\n\n\u003C\u002Fdiv>\n\u003C\u002Fdiv>\n\u003C\u002Fdiv>\n\n\n\n\u003Cul>\n\u003Cli>Here \u003Ccode>content_type\u003C\u002Fcode> is the FireignKey relation with django content type model. If you want to know more about content type framework of Django then \u003Ca href=\"https:\u002F\u002Fdev.tohere%20it%20is\">https:\u002F\u002Fdocs.djangoproject.com\u002Fen\u002F3.0\u002Fref\u002Fcontrib\u002Fcontenttypes\u002F#module-django.contrib.contenttypes\u003C\u002Fa>.\u003C\u002Fli>\n\u003Cli>\n\u003Ccode>object_id\u003C\u002Fcode> is just a big integer field for holding the related object id.\u003C\u002Fli>\n\u003Cli>\n\u003Ccode>object\u003C\u002Fcode> is a relation for accessing the generic object. This field will not create column on database. It just model level mapping with the generic object.\u003C\u002Fli>\n\u003C\u002Ful>\n\n\u003Ch3>\n  \u003Ca name=\"what-we-can-do-now\" href=\"#what-we-can-do-now\">\n  \u003C\u002Fa>\n  What we can do now?\n\u003C\u002Fh3>\n\n\u003Cp>\u003Cstrong>Data access:\u003C\u002Fstrong> We can access data using dot[.] operator like,\u003Cbr>\n\u003C\u002Fp>\n\n\u003Cdiv class=\"highlight js-code-highlight\">\n\u003Cpre class=\"highlight plaintext\">\u003Ccode>&gt;&gt;&gt; _log = ActivityLog.objects.first()\n&gt;&gt;&gt; _log.object\n&lt;Author object&gt;\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cdiv class=\"highlight__panel js-actions-panel\">\n\u003Cdiv class=\"highlight__panel-action js-fullscreen-code-action\">\n    \u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\">\u003Ctitle>Enter fullscreen mode\u003C\u002Ftitle>\n    \u003Cpath d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\">\u003C\u002Fpath>\n\u003C\u002Fsvg>\n\n    \u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\">\u003Ctitle>Exit fullscreen mode\u003C\u002Ftitle>\n    \u003Cpath d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\">\u003C\u002Fpath>\n\u003C\u002Fsvg>\n\n\u003C\u002Fdiv>\n\u003C\u002Fdiv>\n\u003C\u002Fdiv>\n\n\n\n\u003Ch3>\n  \u003Ca name=\"what-we-cant-do\" href=\"#what-we-cant-do\">\n  \u003C\u002Fa>\n  What we can't do?\n\u003C\u002Fh3>\n\n\u003Cp>We won't query like \u003Ccode>ActivityLog.objects.filter(object__name__contains='mr x')\u003C\u002Fcode> on the ActivityLog model. But this is not impossible. Here comes \u003Ccode>GenericRelation\u003C\u002Fcode>.\u003C\u002Fp>\n\n\u003Ch2>\n  \u003Ca name=\"how-to-use-genericrelation-with-genericforeignkey\" href=\"#how-to-use-genericrelation-with-genericforeignkey\">\n  \u003C\u002Fa>\n  How to use GenericRelation with GenericForeignkey?\n\u003C\u002Fh2>\n\n\u003Cp>Just need to modify a little bit of our models(those models need to access over queryset.). Let's do it.\u003Cbr>\n\u003C\u002Fp>\n\n\u003Cdiv class=\"highlight js-code-highlight\">\n\u003Cpre class=\"highlight python\">\u003Ccode>\u003Cspan class=\"k\">class\u003C\u002Fspan> \u003Cspan class=\"nc\">Author\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\u003Cspan class=\"n\">models\u003C\u002Fspan>\u003Cspan class=\"p\">.\u003C\u002Fspan>\u003Cspan class=\"n\">Model\u003C\u002Fspan>\u003Cspan class=\"p\">):\u003C\u002Fspan>\n    \u003Cspan class=\"n\">name\u003C\u002Fspan> \u003Cspan class=\"o\">=\u003C\u002Fspan> \u003Cspan class=\"n\">models\u003C\u002Fspan>\u003Cspan class=\"p\">.\u003C\u002Fspan>\u003Cspan class=\"nc\">CharField\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\u003Cspan class=\"n\">max_length\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"mi\">64\u003C\u002Fspan>\u003Cspan class=\"p\">)\u003C\u002Fspan>\n    \u003Cspan class=\"c1\"># This will not create field on db\n\u003C\u002Fspan>    \u003Cspan class=\"n\">activity_logs\u003C\u002Fspan> \u003Cspan class=\"o\">=\u003C\u002Fspan> \u003Cspan class=\"nc\">GenericRelation\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\u003Cspan class=\"n\">ActivityLog\u003C\u002Fspan> \u003Cspan class=\"p\">,\u003C\u002Fspan>\u003Cspan class=\"n\">content_type_field\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"s\">content_type\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"p\">,\u003C\u002Fspan>\n        \u003Cspan class=\"n\">object_id_field\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"s\">object_id\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"p\">,\u003C\u002Fspan> \u003Cspan class=\"n\">related_query_name\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"s\">author\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"p\">)\u003C\u002Fspan>\n    \u003Cspan class=\"c1\"># Other fields\n\u003C\u002Fspan>\n\n\u003Cspan class=\"k\">class\u003C\u002Fspan> \u003Cspan class=\"nc\">Reader\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\u003Cspan class=\"n\">models\u003C\u002Fspan>\u003Cspan class=\"p\">.\u003C\u002Fspan>\u003Cspan class=\"n\">Model\u003C\u002Fspan>\u003Cspan class=\"p\">):\u003C\u002Fspan>\n    \u003Cspan class=\"n\">name\u003C\u002Fspan> \u003Cspan class=\"o\">=\u003C\u002Fspan> \u003Cspan class=\"n\">models\u003C\u002Fspan>\u003Cspan class=\"p\">.\u003C\u002Fspan>\u003Cspan class=\"nc\">CharField\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\u003Cspan class=\"n\">max_length\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"mi\">64\u003C\u002Fspan>\u003Cspan class=\"p\">)\u003C\u002Fspan>\n    \u003Cspan class=\"c1\"># This will not create field on db\n\u003C\u002Fspan>    \u003Cspan class=\"n\">activity_logs\u003C\u002Fspan> \u003Cspan class=\"o\">=\u003C\u002Fspan> \u003Cspan class=\"nc\">GenericRelation\u003C\u002Fspan>\u003Cspan class=\"p\">(\u003C\u002Fspan>\u003Cspan class=\"n\">ActivityLog\u003C\u002Fspan> \u003Cspan class=\"p\">,\u003C\u002Fspan>\u003Cspan class=\"n\">content_type_field\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"s\">content_type\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"p\">,\u003C\u002Fspan>\n        \u003Cspan class=\"n\">object_id_field\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"s\">object_id\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"p\">,\u003C\u002Fspan> \u003Cspan class=\"n\">related_query_name\u003C\u002Fspan>\u003Cspan class=\"o\">=\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"s\">reader\u003C\u002Fspan>\u003Cspan class=\"sh\">'\u003C\u002Fspan>\u003Cspan class=\"p\">)\u003C\u002Fspan>\n    \u003Cspan class=\"c1\"># Other fields\n\u003C\u002Fspan>\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cdiv class=\"highlight__panel js-actions-panel\">\n\u003Cdiv class=\"highlight__panel-action js-fullscreen-code-action\">\n    \u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\">\u003Ctitle>Enter fullscreen mode\u003C\u002Ftitle>\n    \u003Cpath d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\">\u003C\u002Fpath>\n\u003C\u002Fsvg>\n\n    \u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\">\u003Ctitle>Exit fullscreen mode\u003C\u002Ftitle>\n    \u003Cpath d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\">\u003C\u002Fpath>\n\u003C\u002Fsvg>\n\n\u003C\u002Fdiv>\n\u003C\u002Fdiv>\n\u003C\u002Fdiv>\n\n\n\n\u003Cp>That's it. We can now write like following...\u003Cbr>\n\u003C\u002Fp>\n\n\u003Cdiv class=\"highlight js-code-highlight\">\n\u003Cpre class=\"highlight plaintext\">\u003Ccode>&gt;&gt;&gt; ActivityLog.objects.filter(author__name__contains='mr x')  # This is for author.\n&gt;&gt;&gt; ActivityLog.objects.filter(reader__name__contains='mr x'). # This is for reader.\n\u003C\u002Fcode>\u003C\u002Fpre>\n\u003Cdiv class=\"highlight__panel js-actions-panel\">\n\u003Cdiv class=\"highlight__panel-action js-fullscreen-code-action\">\n    \u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\">\u003Ctitle>Enter fullscreen mode\u003C\u002Ftitle>\n    \u003Cpath d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\">\u003C\u002Fpath>\n\u003C\u002Fsvg>\n\n    \u003Csvg xmlns=\"http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\">\u003Ctitle>Exit fullscreen mode\u003C\u002Ftitle>\n    \u003Cpath d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\">\u003C\u002Fpath>\n\u003C\u002Fsvg>\n\n\u003C\u002Fdiv>\n\u003C\u002Fdiv>\n\u003C\u002Fdiv>\n\n\n\n\u003Cp>Hope this helps.\u003C\u002Fp>\n\n\u003Cblockquote>\n\u003Cp>Originally posted on \u003Ca href=\"https:\u002F\u002Fashraful.dev\u002Fposts\u002Fdjango-genericforeignkey-with-generic-relation.html\" target=\"_blank\" rel=\"noopener noreferrer\">Ashraful's Blog\u003C\u002Fa>\u003C\u002Fp>\n\u003C\u002Fblockquote>\n\n","## Introduction\nI hope it's no offense that, Django is a powerful tool for the web. Today I'll talk about `GenericForeignKey` and `GenericRelation`. End of the article you will know about `GenericForeignKey` and `GenericRelation`.\n\n## What is GenericForeignKey?\n`GenericForeignKey` is a foreign key with any model with the help of content type. Still confusing? Let's see some example.\n\nSuppose we have book system where there are some models like following,\n\n```python\nclass Author(models.Model):\n    name = models.CharField(max_length=64)\n    # Other fields\n\n\nclass Reader(models.Model):\n    name = models.CharField(max_length=64)\n    # Other fields\n\n\nclass ActivityLog(models.Model):\n    # Here we want to track everything that other users do. So, I'll add GenericForeignKey\n    content_type = models.ForeignKey(\n        ContentType, default=None, null=True, on_delete=models.SET_NULL, related_name='activity_logs')\n    object_id = models.BigIntegerField(default=None, null=True)\n    object = GenericForeignKey(ct_field=\"content_type\", fk_field=\"object_id\")\n    # Other fields\n```\n\n* Here `content_type` is the FireignKey relation with django content type model. If you want to know more about content type framework of Django then [https:\u002F\u002Fdocs.djangoproject.com\u002Fen\u002F3.0\u002Fref\u002Fcontrib\u002Fcontenttypes\u002F#module-django.contrib.contenttypes](here it is).\n* `object_id` is just a big integer field for holding the related object id.\n* `object` is a relation for accessing the generic object. This field will not create column on database. It just model level mapping with the generic object.\n\n### What we can do now?\n**Data access:** We can access data using dot[.] operator like, \n```\n>>> _log = ActivityLog.objects.first()\n>>> _log.object\n\u003CAuthor object>\n```\n\n### What we can't do?\nWe won't query like `ActivityLog.objects.filter(object__name__contains='mr x')` on the ActivityLog model. But this is not impossible. Here comes `GenericRelation`.\n\n## How to use GenericRelation with GenericForeignkey?\nJust need to modify a little bit of our models(those models need to access over queryset.). Let's do it.\n```python\nclass Author(models.Model):\n    name = models.CharField(max_length=64)\n    # This will not create field on db\n    activity_logs = GenericRelation(ActivityLog ,content_type_field='content_type',\n        object_id_field='object_id', related_query_name='author')\n    # Other fields\n\n\nclass Reader(models.Model):\n    name = models.CharField(max_length=64)\n    # This will not create field on db\n    activity_logs = GenericRelation(ActivityLog ,content_type_field='content_type',\n        object_id_field='object_id', related_query_name='reader')\n    # Other fields\n\n```\n\nThat's it. We can now write like following...\n\n```\n>>> ActivityLog.objects.filter(author__name__contains='mr x')  # This is for author.\n>>> ActivityLog.objects.filter(reader__name__contains='mr x'). # This is for reader.\n```\n\nHope this helps.\n> Originally posted on [Ashraful's Blog](https:\u002F\u002Fashraful.dev\u002Fposts\u002Fdjango-genericforeignkey-with-generic-relation.html)",{"name":28,"username":29,"twitter_username":30,"github_username":31,"user_id":32,"website_url":33,"profile_image":34,"profile_image_90":35},"Mohamad Ashraful Islam","ashraful","_ashraful","iashraful",30490,"https:\u002F\u002Fashraful.dev","https:\u002F\u002Fmedia2.dev.to\u002Fdynamic\u002Fimage\u002Fwidth=640,height=640,fit=cover,gravity=auto,format=auto\u002Fhttps%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F30490%2F9dc1ef64-0233-43b5-9127-79affd83cc43.jpg","https:\u002F\u002Fmedia2.dev.to\u002Fdynamic\u002Fimage\u002Fwidth=90,height=90,fit=cover,gravity=auto,format=auto\u002Fhttps%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F30490%2F9dc1ef64-0233-43b5-9127-79affd83cc43.jpg",1784633124089]