{"id":651,"date":"2020-12-08T11:05:41","date_gmt":"2020-12-08T08:05:41","guid":{"rendered":"https:\/\/www.etemkeskin.com\/?p=651"},"modified":"2020-12-08T11:05:45","modified_gmt":"2020-12-08T08:05:45","slug":"birebir-iliski-kullanarak-django-user-modeline-ekstra-alanlar-ekleme","status":"publish","type":"post","link":"https:\/\/www.etemkeskin.com\/index.php\/tr\/2020\/12\/08\/birebir-iliski-kullanarak-django-user-modeline-ekstra-alanlar-ekleme\/","title":{"rendered":"Birebir \u0130li\u015fki Kullanarak Django User Modeline Ekstra Alanlar Ekleme"},"content":{"rendered":"\n<p>Django Framework \u00fcnde gelen haz\u0131r model yap\u0131s\u0131nda User modelinde e\u011fer yeni alanlara ihtiya\u00e7 duyulursa bunu iki \u015fekilde \u00e7\u00f6zebiliriz.<\/p>\n\n\n\n<p>Bunlardan biri <strong>AbstractBaseUser<\/strong> modelini extend etmek.<\/p>\n\n\n\n<p>\u0130kincisi yeni bir model olu\u015fturup bunu birebir ili\u015fki ile(OneToOne) User modeline ba\u011flamak.<\/p>\n\n\n\n<p>Bu yaz\u0131da ikinci yolu kullanarak User modeline ekstra alanlar nas\u0131l sa\u011flar\u0131z bunu anlataca\u011f\u0131m.<\/p>\n\n\n\n<p>User modelinde ihtiya\u00e7 duyaca\u011f\u0131m\u0131z ekstra alanlar Profile modelinde bulunacak. Profile modelimizde user \u0131n telefon, adres, resim gibi bilgileri tutulacak. Veri taban\u0131nda her bir user olu\u015fturuldu\u011funda Profile modelinde User modeline birebir ili\u015fki ile(OneToOne) ba\u011fl\u0131 kayd\u0131n olu\u015fmas\u0131 gerekiyor.<\/p>\n\n\n\n<p>Bunun i\u00e7in modellerimizin bulundu\u011fu <strong>model.py<\/strong> dosyas\u0131nda <strong>user_is_created()<\/strong> ad\u0131nda bir fonksiyon tan\u0131ml\u0131yoruz.<\/p>\n\n\n\n<p>User tablosunda olu\u015facak create aksiyonu olup olmad\u0131\u011f\u0131n\u0131 alg\u0131lamak i\u00e7in <strong>post_save<\/strong> signalini kullanaca\u011f\u0131z.<\/p>\n\n\n\n<p>Bunu i\u00e7in <strong>user_is_created()<\/strong> \u00a0fonksiyonumuzun \u00fcst\u00fcne <strong>@receiver<\/strong> decorator\u00fc kullanarak ger\u00e7ekle\u015ftirece\u011fiz.<\/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>Django Framework \u00fcnde gelen haz\u0131r model yap\u0131s\u0131nda User modelinde e\u011fer yeni alanlara ihtiya\u00e7 duyulursa bunu iki \u015fekilde \u00e7\u00f6zebiliriz. Bunlardan biri AbstractBaseUser modelini extend etmek. \u0130kincisi yeni bir model olu\u015fturup bunu birebir ili\u015fki ile(OneToOne) User modeline ba\u011flamak. Bu yaz\u0131da ikinci yolu kullanarak User modeline ekstra alanlar nas\u0131l sa\u011flar\u0131z bunu anlataca\u011f\u0131m. User modelinde ihtiya\u00e7 duyaca\u011f\u0131m\u0131z ekstra alanlar [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Birebir \u0130li\u015fki Kullanarak Django User Modeline Ekstra Alanlar Ekleme - blog website<\/title>\n<meta name=\"description\" content=\"Birebir \u0130li\u015fki Kullanarak Django User Modeline Ekstra Alanlar EklemeDjango Framework \u00fcnde gelen haz\u0131r model yap\u0131s\u0131nda User modelinde e\u011fer yeni alanlara ihtiya\u00e7 duyulursa bunu iki \u015fekilde \u00e7\u00f6zebiliriz.\" \/>\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\/tr\/2020\/12\/08\/birebir-iliski-kullanarak-django-user-modeline-ekstra-alanlar-ekleme\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Birebir \u0130li\u015fki Kullanarak Django User Modeline Ekstra Alanlar Ekleme - blog website\" \/>\n<meta property=\"og:description\" content=\"Birebir \u0130li\u015fki Kullanarak Django User Modeline Ekstra Alanlar EklemeDjango Framework \u00fcnde gelen haz\u0131r model yap\u0131s\u0131nda User modelinde e\u011fer yeni alanlara ihtiya\u00e7 duyulursa bunu iki \u015fekilde \u00e7\u00f6zebiliriz.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.etemkeskin.com\/index.php\/tr\/2020\/12\/08\/birebir-iliski-kullanarak-django-user-modeline-ekstra-alanlar-ekleme\/\" \/>\n<meta property=\"og:site_name\" content=\"blog website\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-08T08:05:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-08T08:05:45+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\/tr\/2020\/12\/08\/birebir-iliski-kullanarak-django-user-modeline-ekstra-alanlar-ekleme\/#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\/tr\/2020\/12\/08\/birebir-iliski-kullanarak-django-user-modeline-ekstra-alanlar-ekleme\/#webpage\",\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/tr\/2020\/12\/08\/birebir-iliski-kullanarak-django-user-modeline-ekstra-alanlar-ekleme\/\",\"name\":\"Birebir \\u0130li\\u015fki Kullanarak Django User Modeline Ekstra Alanlar Ekleme - blog website\",\"isPartOf\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/tr\/2020\/12\/08\/birebir-iliski-kullanarak-django-user-modeline-ekstra-alanlar-ekleme\/#primaryimage\"},\"datePublished\":\"2020-12-08T08:05:41+00:00\",\"dateModified\":\"2020-12-08T08:05:45+00:00\",\"author\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\"},\"description\":\"Birebir \\u0130li\\u015fki Kullanarak Django User Modeline Ekstra Alanlar EklemeDjango Framework \\u00fcnde gelen haz\\u0131r model yap\\u0131s\\u0131nda User modelinde e\\u011fer yeni alanlara ihtiya\\u00e7 duyulursa bunu iki \\u015fekilde \\u00e7\\u00f6zebiliriz.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/tr\/2020\/12\/08\/birebir-iliski-kullanarak-django-user-modeline-ekstra-alanlar-ekleme\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.etemkeskin.com\/index.php\/tr\/2020\/12\/08\/birebir-iliski-kullanarak-django-user-modeline-ekstra-alanlar-ekleme\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/tr\/2020\/12\/08\/birebir-iliski-kullanarak-django-user-modeline-ekstra-alanlar-ekleme\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\/\/www.etemkeskin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Birebir \\u0130li\\u015fki Kullanarak Django User Modeline Ekstra Alanlar Ekleme\"}]},{\"@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\/651"}],"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=651"}],"version-history":[{"count":4,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/651\/revisions"}],"predecessor-version":[{"id":655,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/651\/revisions\/655"}],"wp:attachment":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/media?parent=651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/categories?post=651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/tags?post=651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}