{"id":1859,"date":"2022-05-10T20:28:51","date_gmt":"2022-05-10T11:28:51","guid":{"rendered":"http:\/\/www.agile-software.site\/?p=1859"},"modified":"2022-07-16T17:19:57","modified_gmt":"2022-07-16T08:19:57","slug":"on_delete%e3%82%aa%e3%83%97%e3%82%b7%e3%83%a7%e3%83%b3","status":"publish","type":"post","link":"https:\/\/agile-software.net\/?p=1859","title":{"rendered":"on_delete\u30aa\u30d7\u30b7\u30e7\u30f3"},"content":{"rendered":"\n<p>\u30e2\u30c7\u30eb\u540c\u58eb\u3092\u7d10\u3065\u3051\u308b\u3068\u304d\u306b\u3001<code>ForeignKey<\/code>\u3084<code>OneToOneField<\/code>\u3092\u4f7f\u3044\u307e\u3059\u304c\u3001\u305d\u306e\u969b\u3001\u30d0\u30fc\u30b8\u30e7\u30f32\u304b\u3089\u306f\u5f15\u6570\u3068\u3057\u3066<code>on_delete<\/code>\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u5fc5\u9808\u3068\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>on_delete\u3068\u306f\u3001\u53c2\u7167\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u524a\u9664\u3055\u308c\u305f\u3068\u304d\u306b\u3001\u305d\u308c\u3068\u7d10\u3065\u3051\u3089\u308c\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3082<strong>\u4e00\u7dd2\u306b\u524a\u9664\u3059\u308b<\/strong>\u306e\u304b\u3001\u305d\u308c\u3068\u3082<strong>\u305d\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u6b8b\u3057\u3066\u304a\u304f<\/strong>\u306e\u304b\u3092\u8a2d\u5b9a\u3059\u308b\u3082\u306e\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>on_delete\u30aa\u30d7\u30b7\u30e7\u30f3<\/strong><\/td><td><\/td><\/tr><tr><td>models.CASCADE<\/td><td>\u4e00\u7dd2\u306b\u524a\u9664\u3055\u308c\u308b<\/td><\/tr><tr><td>models.PROTECT<\/td><td>\u524a\u9664\u3067\u304d\u306a\u3044<\/td><\/tr><tr><td>models.SET_NULL<\/td><td>NULL\u304c\u30bb\u30c3\u30c8\u3055\u308c\u308b<\/td><\/tr><tr><td>models.SET_DEFAULT<\/td><td>\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u304c\u30bb\u30c3\u30c8\u3055\u308c\u308b<\/td><\/tr><tr><td>models.SET<\/td><td>\u4efb\u610f\u306e\u5024\u304c\u30bb\u30c3\u30c8\u3055\u308c\u308b<\/td><\/tr><tr><td>DO_NOTHING<\/td><td>\u4f55\u3082\u3057\u306a\u3044<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/djangobrothers.com\/blogs\/on_delete\/#%E5%AE%9F%E9%9A%9B%E3%81%AE%E4%BD%BF%E7%94%A8%E4%BE%8B\">\u4f7f\u7528\u4f8b<\/a><\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\" data-file=\"models.py\"><code>from django.db import models\n\nclass Author(models.Model):\n    &quot;&quot;&quot;\u4f5c\u8005&quot;&quot;&quot;\n    name = models.CharField(max_length=50)\n\n    def __str__(self):\n        return self.name\n\nclass Story(models.Model):\n    &quot;&quot;&quot;\u5c0f\u8aac&quot;&quot;&quot;\n    author = models.ForeignKey(Author, on_delete=models.CASCADE)\n    title = models.CharField(max_length=50)\n    text = models.TextField()\n\n    def __str__(self):\n        return self.title<\/code><\/pre><\/div>\n\n\n\n<p>\u3053\u306e\u8a2d\u8a08\u306b\u3088\u3063\u3066\u3053\u3093\u306aStorys\u30c6\u30fc\u30d6\u30eb\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"771\" height=\"286\" src=\"http:\/\/www.agile-software.site\/wp-content\/uploads\/2022\/05\/on_delete_1.png\" alt=\"\" class=\"wp-image-1862\"\/><\/figure>\n\n\n\n<p>\u4f8b\u3048\u3070\u3053\u3053\u3067Author\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff1a&#8221;\u5ddd\u7aef\u5eb7\u6210&#8221;\u3092\u524a\u9664\u3057\u305f\u3068\u304d\u306b\u3001\u305d\u308c\u3068\u7d10\u3065\u3044\u305fStory\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff1a&#8221;\u96ea\u56fd&#8221;\u3001&#8221;\u4f0a\u8c46\u306e\u8e0a\u308a\u5b50&#8221;\u3082\u524a\u9664\u3059\u308b\u306e\u304b\u3001\u305d\u308c\u3068\u3082\u6b8b\u3057\u3066\u304a\u304f\u306e\u304b\u3092\u8a2d\u5b9a\u3059\u308b\u306e\u304con_delete\u3067\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/djangobrothers.com\/blogs\/on_delete\/#CASCADE\">CASCADE<\/a><\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>author = models.ForeignKey(Author, on_delete=models.CASCADE)<\/code><\/pre><\/div>\n\n\n\n<p>\u524a\u9664\u3059\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u7d10\u3065\u3044\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u5168\u3066\u524a\u9664\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u753b\u50cf\u306e\u3088\u3046\u306bAuthor\u30aa\u30d6\u30b8\u30a7\u30af\u30c8&#8221;\u5ddd\u7aef\u5eb7\u6210&#8221;\u3092\u524a\u9664\u3057\u3088\u3046\u3068\u3057\u3066\u524a\u9664\u30dc\u30bf\u30f3\u3092\u62bc\u3059\u3068\u3001<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"908\" height=\"279\" src=\"http:\/\/www.agile-software.site\/wp-content\/uploads\/2022\/05\/on_delete_2.png\" alt=\"\" class=\"wp-image-1863\" srcset=\"https:\/\/agile-software.net\/wp-content\/uploads\/2022\/05\/on_delete_2.png 908w, https:\/\/agile-software.net\/wp-content\/uploads\/2022\/05\/on_delete_2-300x92.png 300w, https:\/\/agile-software.net\/wp-content\/uploads\/2022\/05\/on_delete_2-768x236.png 768w, http:\/\/www.agile-software.site\/wp-content\/uploads\/2022\/05\/on_delete_2.png 856w\" sizes=\"auto, (max-width: 908px) 100vw, 908px\" \/><\/figure>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306b\u300c\u95a2\u9023\u4ed8\u3051\u3089\u308c\u3066\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u524a\u9664\u3057\u307e\u3059\u300d\u3068\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u51fa\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"405\" src=\"http:\/\/www.agile-software.site\/wp-content\/uploads\/2022\/05\/on_delete_3.png\" alt=\"\" class=\"wp-image-1864\"\/><\/figure>\n\n\n\n<p>\u300c\u306f\u3044\u300d\u30dc\u30bf\u30f3\u3067\u3001Author\u30aa\u30d6\u30b8\u30a7\u30af\u30c8&#8221;\u5ddd\u7aef\u5eb7\u6210&#8221;\u304c\u524a\u9664\u3055\u308c\u308b\u306e\u3068\u540c\u6642\u306b\u3001\u305d\u308c\u306b\u7d10\u3065\u3044\u305fStory\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3082\u5168\u3066\u524a\u9664\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/djangobrothers.com\/blogs\/on_delete\/#PROTECT\">PROTECT<\/a><\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>author = models.ForeignKey(Author, on_delete=models.PROTECT)<\/code><\/pre><\/div>\n\n\n\n<p>\u95a2\u9023\u4ed8\u3051\u3089\u308c\u3066\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u3042\u308b\u3068\u3001\u524a\u9664\u3067\u304d\u307e\u305b\u3093<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"237\" src=\"http:\/\/www.agile-software.site\/wp-content\/uploads\/2022\/05\/on_delete_4.png\" alt=\"\" class=\"wp-image-1865\"\/><\/figure>\n\n\n\n<p>\u3053\u306e\u5834\u5408\u3001Author\u306b\u7d10\u3065\u3044\u305fStory\u304c\u3042\u308b\u306e\u3067Author\u3092\u524a\u9664\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093\u3002\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305fStory\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u5168\u3066\u524a\u9664\u3059\u308c\u3070Author\u3082\u524a\u9664\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/djangobrothers.com\/blogs\/on_delete\/#SET_NULL\">SET_NULL<\/a><\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>author = models.ForeignKey(\n    Author,\n    on_delete=models.SET_NULL,\n    null=True\n)<\/code><\/pre><\/div>\n\n\n\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u524a\u9664\u3055\u308c\u308b\u3068\u3001\u4ee3\u308f\u308a\u306bNULL\u3092\u30bb\u30c3\u30c8\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4eca\u56de\u306e\u4f8b\u3060\u3068\u3001Author\u3092\u524a\u9664\u3059\u308b\u3068Story\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306eauthor\u30d5\u30a3\u30fc\u30eb\u30c9\u304cNULL\uff08\u7a7a\u767d\uff09\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"263\" src=\"http:\/\/www.agile-software.site\/wp-content\/uploads\/2022\/05\/on_delete_5.png\" alt=\"\" class=\"wp-image-1866\"\/><\/figure>\n\n\n\n<p>\u3053\u3093\u306a\u611f\u3058\u3067\u3001Author\u306b\u8ab0\u3082\u767b\u9332\u3055\u308c\u3066\u3044\u306a\u3044\u72b6\u614b\u306b\u306a\u308a\u307e\u3059\u3002\u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001author\u30d5\u30a3\u30fc\u30eb\u30c9\u306fnull=True\u306b\u3057\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/djangobrothers.com\/blogs\/on_delete\/#SET_DEFAULT\">&nbsp;SET_DEFAULT<\/a><\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>author = models.ForeignKey(\n    Author,\n    on_delete=models.SET_DEFAULT,\n    default=Author.objects.get(name=&quot;\u533f\u540d&quot;)\n)<\/code><\/pre><\/div>\n\n\n\n<p>\u524a\u9664\u3055\u308c\u305f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u4ee3\u308f\u308a\u306b\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u304c\u5165\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002\u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001author\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u8a2d\u5b9a\u3057\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"253\" src=\"http:\/\/www.agile-software.site\/wp-content\/uploads\/2022\/05\/on_delete_6.png\" alt=\"\" class=\"wp-image-1867\"\/><\/figure>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306bauthor\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306e&#8221;\u533f\u540d&#8221;\u304c\u5165\u308a\u307e\u3059<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/djangobrothers.com\/blogs\/on_delete\/#SET()\">SET()<\/a><\/h2>\n\n\n\n<p>\u81ea\u5206\u3067\u51e6\u7406\u3092\u81ea\u7531\u306b\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u4eca\u56de\u306f\u3001&#8221;\u524a\u9664\u3055\u308c\u305f\u8457\u8005&#8221;\u3068\u3044\u3046\u6587\u5b57\u5217\u3092author\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u5165\u308c\u308b\u51e6\u7406\u3092\u3057\u307e\u3059<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>def get_deleted_user():\n    return Author.objects.get_or_create(name=&quot;\u524a\u9664\u3055\u308c\u305f\u4f5c\u8005&quot;)[0]\n\nclass Story(models.Model):\n    author = models.ForeignKey(Author, on_delete=models.SET(get_deleted_user))\n    title = models.CharField(max_length=50)\n    text = models.TextField()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"223\" src=\"http:\/\/www.agile-software.site\/wp-content\/uploads\/2022\/05\/on_delete_7.png\" alt=\"\" class=\"wp-image-1868\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/djangobrothers.com\/blogs\/on_delete\/#DO_NOTHING\">DO_NOTHING<\/a><\/h2>\n\n\n\n<p>\u4f55\u306e\u51e6\u7406\u3082\u3057\u307e\u305b\u3093\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30e2\u30c7\u30eb\u540c\u58eb\u3092\u7d10\u3065\u3051\u308b\u3068\u304d\u306b\u3001ForeignKey\u3084OneToOneField\u3092\u4f7f\u3044\u307e\u3059\u304c\u3001\u305d\u306e\u969b\u3001\u30d0\u30fc\u30b8\u30e7\u30f32\u304b\u3089\u306f\u5f15\u6570\u3068\u3057\u3066on_delete\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u5fc5\u9808\u3068\u306a\u308a\u307e\u3057\u305f\u3002 on_delete\u3068\u306f\u3001\u53c2\u7167\u3059\u308b\u30aa\u30d6\u30b8\u30a7<\/p>\n","protected":false},"author":1,"featured_media":2047,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jin_ogp_image_url":"","_jin_last_featured_id":0,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1859","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django"],"_links":{"self":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/1859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1859"}],"version-history":[{"count":3,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/1859\/revisions"}],"predecessor-version":[{"id":2048,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/1859\/revisions\/2048"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/media\/2047"}],"wp:attachment":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}