{"id":636,"date":"2020-12-08T11:00:10","date_gmt":"2020-12-08T08:00:10","guid":{"rendered":"https:\/\/www.etemkeskin.com\/?p=636"},"modified":"2020-12-08T11:08:28","modified_gmt":"2020-12-08T08:08:28","slug":"adding-extra-fields-to-the-django-user-model-using-a-one-to-one-relationship","status":"publish","type":"post","link":"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/08\/adding-extra-fields-to-the-django-user-model-using-a-one-to-one-relationship\/","title":{"rendered":"Adding Extra Fields to the Django User Model Using a One-to-One Relationship"},"content":{"rendered":"\n<p>In the ready-made model structure that comes in the Django Framework, if new fields are needed in the User model, we can solve it in two ways.<\/p>\n\n\n\n<p>One of them is extending the <strong>AbstractBaseUser<\/strong> model.<\/p>\n\n\n\n<p>The second is to create a new model and connect it to the User model with a one-to-one relationship.<\/p>\n\n\n\n<p>In this article, I will explain how we can provide extra fields to the User model using the second way.<\/p>\n\n\n\n<p>Extra fields that we will need in the User model will be defined in the Profile model. In our Profile model, user&#8217;s information such as phone, address, picture will be kept. When each user is created in the database, a row with one-to-one relationship to the User model must be created in the Profile model.<\/p>\n\n\n\n<p>For this, we define a function named <strong>user_is_created()<\/strong> in the <strong>model.py<\/strong> file included in our models.<\/p>\n\n\n\n<p>We will use the <strong>post_save<\/strong> signal to detect if there is a create action that will occur in the user table.<\/p>\n\n\n\n<p>We will do this by using the <strong>@receiver<\/strong> decorator on top of our user_is_created () function.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from django.db import models\nfrom django.contrib.auth.models import User\nfrom django.dispatch import receiver\nfrom django.db.models.signals import post_save # Produce a signal if there is any database action.\n\nclass Profile(models.Model):\n    user = models.OneToOneField(User, on_delete= models.CASCADE )\n    occupation = models.CharField(max_length=150, null=True, blank=True)\n    phone = models.CharField(max_length=30, null=True, blank=True)\n    address = models.CharField(max_length=250, null=True, blank=True)\n    city = models.CharField(max_length=150, null=True, blank=True)\n    country = models.CharField(max_length=150, null=True, blank=True)\n    image = models.ImageField(upload_to='pictures\/%Y\/%m\/%d\/' , max_length=255, null=True, blank=True)\n\n    def __str__(self):\n        return \"{0}\".format(self.user.email)\n\n# When any User instance created, Profile object instance is created automatically linked by User \n@receiver(post_save, sender = User)\ndef user_is_created(sender, instance, created, **kwargs):\n    if created:\n        Profile.objects.create(user= instance)\n    else:\n        instance.profile.save()\n<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-20.png\" alt=\"Topluluk Taraf\u0131ndan Do\u011fruland\u0131 simgesi\" class=\"wp-image-642\" width=\"20\" height=\"20\"\/><\/figure>\n\n\n\n<p>DJANGO VERS\u0130YON : 3.1.1<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"20\" height=\"20\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-19.png\" alt=\"Topluluk Taraf\u0131ndan Do\u011fruland\u0131 simgesi\" class=\"wp-image-638\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In the ready-made model structure that comes in the Django Framework, if new fields are needed in the User model, we can solve it in two ways. One of them is extending the AbstractBaseUser model. The second is to create a new model and connect it to the User model with a one-to-one relationship. In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Adding Extra Fields to the Django User Model Using a One-to-One Relationship - blog website<\/title>\n<meta name=\"description\" content=\"Adding Extra Fields to the Django User Model Using a One-to-One Relationship\" \/>\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.etemkeskin.com\/index.php\/2020\/12\/08\/adding-extra-fields-to-the-django-user-model-using-a-one-to-one-relationship\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Adding Extra Fields to the Django User Model Using a One-to-One Relationship - blog website\" \/>\n<meta property=\"og:description\" content=\"Adding Extra Fields to the Django User Model Using a One-to-One Relationship\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/08\/adding-extra-fields-to-the-django-user-model-using-a-one-to-one-relationship\/\" \/>\n<meta property=\"og:site_name\" content=\"blog website\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-08T08:00:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-08T08:08:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-20.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.etemkeskin.com\/#website\",\"url\":\"https:\/\/www.etemkeskin.com\/\",\"name\":\"blog website\",\"description\":\"Etem KESK\\u0130N\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.etemkeskin.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"tr\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/08\/adding-extra-fields-to-the-django-user-model-using-a-one-to-one-relationship\/#primaryimage\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-20.png\",\"contentUrl\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-20.png\",\"width\":20,\"height\":20},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/08\/adding-extra-fields-to-the-django-user-model-using-a-one-to-one-relationship\/#webpage\",\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/08\/adding-extra-fields-to-the-django-user-model-using-a-one-to-one-relationship\/\",\"name\":\"Adding Extra Fields to the Django User Model Using a One-to-One Relationship - blog website\",\"isPartOf\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/08\/adding-extra-fields-to-the-django-user-model-using-a-one-to-one-relationship\/#primaryimage\"},\"datePublished\":\"2020-12-08T08:00:10+00:00\",\"dateModified\":\"2020-12-08T08:08:28+00:00\",\"author\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\"},\"description\":\"Adding Extra Fields to the Django User Model Using a One-to-One Relationship\",\"breadcrumb\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/08\/adding-extra-fields-to-the-django-user-model-using-a-one-to-one-relationship\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/08\/adding-extra-fields-to-the-django-user-model-using-a-one-to-one-relationship\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/08\/adding-extra-fields-to-the-django-user-model-using-a-one-to-one-relationship\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\/\/www.etemkeskin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Adding Extra Fields to the Django User Model Using a One-to-One Relationship\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\",\"name\":\"etemkeskin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/#personlogo\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"caption\":\"etemkeskin\"},\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/author\/etemkeskinyahoo-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/636"}],"collection":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/comments?post=636"}],"version-history":[{"count":13,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/636\/revisions"}],"predecessor-version":[{"id":656,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/636\/revisions\/656"}],"wp:attachment":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/media?parent=636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/categories?post=636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/tags?post=636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}