{"id":3173,"date":"2018-02-06T02:12:22","date_gmt":"2018-02-06T02:12:22","guid":{"rendered":"https:\/\/www.botreetechnologies.com\/blog\/?p=3173"},"modified":"2021-11-02T16:41:54","modified_gmt":"2021-11-02T11:11:54","slug":"supporting-multiple-roles-using-djangos-user-model","status":"publish","type":"post","link":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/","title":{"rendered":"Supporting Multiple Roles Using Django&#8217;s User Model"},"content":{"rendered":"\n<p>We often have multiple roles in our <a href=\"https:\/\/www.botreetechnologies.com\/django-development\" target=\"_blank\" rel=\"noreferrer noopener\">Django web applications<\/a>, and most of the time they have two common functionality of sign up and log in, others may completely vary based on the type of role.<\/p>\n\n\n\n<p>Django\u00a0provides a default User model which can handle sign up and log in functionality. However, for supporting multiple roles with common sign up or login functionality, the framework requires us to relate with the Django user model in a different way.<\/p>\n\n\n\n<p><b>Creating a One-To-One Link<\/b><\/p>\n\n\n\n<p>Let us say we have two roles an Intern and an HR each having a distinct set of fields. We need to extend the <i>User<\/i> model Django and create two separate models for <i>Intern<\/i> and <i>HR<\/i> each with one to one relationship with the extended <i>User<\/i> model<\/p>\n\n\n\n<p><script src=\"https:\/\/gist.github.com\/Nipun2016\/8a3c0a19c2349cdbd4e9c6cda192f74a.js\"><\/script><\/p>\n\n\n\n<p>Note that we have defined signals so that our <i>Profile<\/i> model gets created and updated whenever <em>Django custom<\/em> <i>User<\/i> model gets created or updated.<\/p>\n\n\n\n<p>Now let us how to set our <i>forms.py<\/i><\/p>\n\n\n\n<p><script src=\"https:\/\/gist.github.com\/Nipun2016\/2778b1251b57c104da64ececb07a15f8.js\"><\/script><\/p>\n\n\n\n<p>Now in the template part, we shall be using two forms one for the <i>User<\/i> and the other for <i>InternProfile<\/i> the same applies in the case of <i>HRProfile<\/i><\/p>\n\n\n\n<p><script src=\"https:\/\/gist.github.com\/Nipun2016\/f6cea5881c7de2b8811e0e3bb19f6985.js\"><\/script><\/p>\n\n\n\n<p>Finally the view part. In the view we first need to create the user in <i>User<\/i> model so using signal Django automatically creates the instance of <i>InternProfile<\/i>, we add intern specific data like bio and location to this instance and finally, we save it.<\/p>\n\n\n\n<p><script src=\"https:\/\/gist.github.com\/Nipun2016\/653574af261e0497b45f644de937e6ae.js\"><\/script><\/p>\n\n\n\n<p>Note that we have used <i>related_name<\/i> to access <i>InternProfile<\/i> fields from <i>User<\/i> instance.<\/p>\n\n\n\n<p>In a similar fashion, we can create a view for <i>HRProfile<\/i> also. This helps us to build Django user login model for two users.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Read Also: <a href=\"https:\/\/www.botreetechnologies.com\/blog\/top-10-python-use-cases-and-applications\/\" target=\"_blank\" rel=\"noreferrer noopener\">10 real-world Python Use Cases and Applications<\/a><\/p><\/blockquote>\n\n\n\n<p>An experienced Django development company like BoTree Technologies can help you build rapid Django apps with multiple user roles. Contact us today.<\/p>\n\n\n\n<p><strong><a href=\"https:\/\/www.botreetechnologies.com\/contact\">Consulting is free<\/a> &#8211; let us help you grow!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We often have multiple roles in our Django web applications,&#8230;<\/p>\n","protected":false},"author":12,"featured_media":14455,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68,10],"tags":[],"class_list":["post-3173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django","category-technology"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Supporting Multiple Roles Using Django&#039;s User Model<\/title>\n<meta name=\"description\" content=\"Django user model supports multiple roles. To learn how you can implement the Django custom user model, continue reading this article.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Supporting Multiple Roles Using Django&#039;s User Model\" \/>\n<meta property=\"og:description\" content=\"Django user model supports multiple roles. To learn how you can implement the Django custom user model, continue reading this article.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/\" \/>\n<meta property=\"og:site_name\" content=\"BoTree Technologies\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BoTreeTechnologies\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-06T02:12:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-02T11:11:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/djangos-user-model.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"852\" \/>\n\t<meta property=\"og:image:height\" content=\"479\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Nipun Brahmbhatt\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@BoTreeTech\" \/>\n<meta name=\"twitter:site\" content=\"@BoTreeTech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nipun Brahmbhatt\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/\"},\"author\":{\"name\":\"Nipun Brahmbhatt\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/8944ad04a5e0bffba6243cf8e02168f7\"},\"headline\":\"Supporting Multiple Roles Using Django&#8217;s User Model\",\"datePublished\":\"2018-02-06T02:12:22+00:00\",\"dateModified\":\"2021-11-02T11:11:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/\"},\"wordCount\":319,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/djangos-user-model.jpg\",\"articleSection\":[\"Django\",\"Technology\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/\",\"name\":\"Supporting Multiple Roles Using Django's User Model\",\"isPartOf\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/djangos-user-model.jpg\",\"datePublished\":\"2018-02-06T02:12:22+00:00\",\"dateModified\":\"2021-11-02T11:11:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/8944ad04a5e0bffba6243cf8e02168f7\"},\"description\":\"Django user model supports multiple roles. To learn how you can implement the Django custom user model, continue reading this article.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#primaryimage\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/djangos-user-model.jpg\",\"contentUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/djangos-user-model.jpg\",\"width\":852,\"height\":479,\"caption\":\"Multiple Roles Using Django\u2019s User Model\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.botreetechnologies.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Supporting Multiple Roles Using Django&#8217;s User Model\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#website\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/\",\"name\":\"BoTree Technologies\",\"description\":\"Committed to inspire generation.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.botreetechnologies.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/8944ad04a5e0bffba6243cf8e02168f7\",\"name\":\"Nipun Brahmbhatt\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/nipun-brahmbhatt-150x150.png\",\"url\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/nipun-brahmbhatt-150x150.png\",\"contentUrl\":\"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/nipun-brahmbhatt-150x150.png\",\"caption\":\"Nipun Brahmbhatt\"},\"description\":\"Nipun is a Python Team Lead and Machine Learning enthusiastic. He has also worked extensively on Android. He is always keen to explore the latest Machine Learning trends, tools and algorithms. He loves mentoring his colleagues in Python and Django.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Supporting Multiple Roles Using Django's User Model","description":"Django user model supports multiple roles. To learn how you can implement the Django custom user model, continue reading this article.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/","og_locale":"en_US","og_type":"article","og_title":"Supporting Multiple Roles Using Django's User Model","og_description":"Django user model supports multiple roles. To learn how you can implement the Django custom user model, continue reading this article.","og_url":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/","og_site_name":"BoTree Technologies","article_publisher":"https:\/\/www.facebook.com\/BoTreeTechnologies\/","article_published_time":"2018-02-06T02:12:22+00:00","article_modified_time":"2021-11-02T11:11:54+00:00","og_image":[{"width":852,"height":479,"url":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/djangos-user-model.jpg","type":"image\/jpeg"}],"author":"Nipun Brahmbhatt","twitter_card":"summary_large_image","twitter_creator":"@BoTreeTech","twitter_site":"@BoTreeTech","twitter_misc":{"Written by":"Nipun Brahmbhatt","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#article","isPartOf":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/"},"author":{"name":"Nipun Brahmbhatt","@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/8944ad04a5e0bffba6243cf8e02168f7"},"headline":"Supporting Multiple Roles Using Django&#8217;s User Model","datePublished":"2018-02-06T02:12:22+00:00","dateModified":"2021-11-02T11:11:54+00:00","mainEntityOfPage":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/"},"wordCount":319,"commentCount":0,"image":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#primaryimage"},"thumbnailUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/djangos-user-model.jpg","articleSection":["Django","Technology"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/","url":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/","name":"Supporting Multiple Roles Using Django's User Model","isPartOf":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#primaryimage"},"image":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#primaryimage"},"thumbnailUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/djangos-user-model.jpg","datePublished":"2018-02-06T02:12:22+00:00","dateModified":"2021-11-02T11:11:54+00:00","author":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/8944ad04a5e0bffba6243cf8e02168f7"},"description":"Django user model supports multiple roles. To learn how you can implement the Django custom user model, continue reading this article.","breadcrumb":{"@id":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#primaryimage","url":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/djangos-user-model.jpg","contentUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2018\/02\/djangos-user-model.jpg","width":852,"height":479,"caption":"Multiple Roles Using Django\u2019s User Model"},{"@type":"BreadcrumbList","@id":"https:\/\/www.botreetechnologies.com\/blog\/supporting-multiple-roles-using-djangos-user-model\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.botreetechnologies.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Supporting Multiple Roles Using Django&#8217;s User Model"}]},{"@type":"WebSite","@id":"https:\/\/www.botreetechnologies.com\/blog\/#website","url":"https:\/\/www.botreetechnologies.com\/blog\/","name":"BoTree Technologies","description":"Committed to inspire generation.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.botreetechnologies.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.botreetechnologies.com\/blog\/#\/schema\/person\/8944ad04a5e0bffba6243cf8e02168f7","name":"Nipun Brahmbhatt","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/nipun-brahmbhatt-150x150.png","url":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/nipun-brahmbhatt-150x150.png","contentUrl":"https:\/\/www.botreetechnologies.com\/blog\/wp-content\/uploads\/2020\/10\/nipun-brahmbhatt-150x150.png","caption":"Nipun Brahmbhatt"},"description":"Nipun is a Python Team Lead and Machine Learning enthusiastic. He has also worked extensively on Android. He is always keen to explore the latest Machine Learning trends, tools and algorithms. He loves mentoring his colleagues in Python and Django."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/3173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=3173"}],"version-history":[{"count":4,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/3173\/revisions"}],"predecessor-version":[{"id":16629,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/3173\/revisions\/16629"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/media\/14455"}],"wp:attachment":[{"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=3173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=3173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.botreetechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=3173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}