{"id":1896,"date":"2022-05-15T00:13:03","date_gmt":"2022-05-14T15:13:03","guid":{"rendered":"http:\/\/www.agile-software.site\/?p=1896"},"modified":"2022-07-16T17:23:58","modified_gmt":"2022-07-16T08:23:58","slug":"django%e3%81%a7you-are-trying-to-add-a-non-nullable-field%e3%81%a8%e8%a1%a8%e7%a4%ba%e3%81%95%e3%82%8c%e3%81%9f%e3%81%a8%e3%81%8d%e3%81%ae%e5%af%be%e7%ad%96","status":"publish","type":"post","link":"https:\/\/agile-software.net\/?p=1896","title":{"rendered":"Django\u3067You are Trying to add a non-nullable field\u3068\u8868\u793a\u3055\u308c\u305f\u3068\u304d\u306e\u5bfe\u7b56"},"content":{"rendered":"\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code><\/code><\/pre><\/div>\n\n\n\n<p>\u65e2\u306b\u30ec\u30b3\u30fc\u30c9\u304c\u5b58\u5728\u3059\u308b\u72b6\u614b\u3067\u3001NULL\u7981\u6b62\u304b\u3064\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u6307\u5b9a\u306a\u3057\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u3068\u3053\u3046\u306a\u308b\u3002<br>\u30c7\u30d5\u30a9\u30eb\u30c8\u6307\u5b9a\u3057\u3066\u3044\u306a\u3044\u306e\u3067\u3001\u65e2\u5b58\u306e\u30ec\u30b3\u30fc\u30c9\u306b\u306fNULL\u7981\u6b62\u3067\u3042\u308b\u306b\u3082\u95a2\u308f\u3089\u305a\u3001NULL\u304c\u5165\u3063\u3066\u3057\u307e\u3046\u3002\u305d\u3053\u3067\u65e2\u5b58\u306e\u30ec\u30b3\u30fc\u30c9\u306f\u3069\u3046\u3059\u308b\u304b\u805e\u3044\u3066\u3044\u308b\u3002\u4e0e\u3048\u3089\u308c\u305f\u9078\u629e\u80a2\u306f2\u3064\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8b66\u544a\u6587\u306b\u5fdc\u3058\u308b(1\u5ea6\u9650\u308a\u306edefault\u3092\u6307\u5b9a\u3059\u308b)<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>from django.db import models\n\nclass Topic(models.Model):\n\n    comment = models.CharField(verbose_name=&quot;\u30b3\u30e1\u30f3\u30c8&quot;,max_length=2000)\n\n    def __str__(self):\n        return self.comment<\/code><\/pre><\/div>\n\n\n\n<p>\u30e2\u30c7\u30eb\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u8ffd\u52a0<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>from django.db import models\n\nclass Topic(models.Model):\n\n    comment = models.CharField(verbose_name=&quot;\u30b3\u30e1\u30f3\u30c8&quot;,max_length=2000)\n    dt      = models.DateTimeField(verbose_name=&quot;\u6295\u7a3f\u65e5\u6642&quot;)\n\n    def __str__(self):\n        return self.comment<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>You are trying to add a non-nullable field &#39;dt&#39; to topic without a default; we can&#39;t do that (the database needs something to populate existing rows).\nPlease select a fix:\n 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)\n 2) Quit, and let me add a default in models.py\nSelect an option:<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>timezone.now()<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u30d5\u30a3\u30fc\u30eb\u30c9\u30aa\u30d7\u30b7\u30e7\u30f3\u306edefault\u3092\u8ffd\u52a0\u3059\u308b<\/h2>\n\n\n\n<p><code>models.py<\/code>\u306e\u65b0\u3057\u304f\u8ffd\u52a0\u3057\u305fdt\u306b\u30d5\u30a3\u30fc\u30eb\u30c9\u30aa\u30d7\u30b7\u30e7\u30f3\u306e<code>default<\/code>\u3092\u6307\u5b9a\u3059\u308b\u3002<code>DatetimeField<\/code>\u306e\u5834\u5408\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b<code>default<\/code>\u3092\u6307\u5b9a\u3059\u308b\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>from django.db import models\nfrom django.utils import timezone\n\nclass Topic(models.Model):\n\n    comment = models.CharField(verbose_name=&quot;\u30b3\u30e1\u30f3\u30c8&quot;,max_length=2000)\n    dt      = models.DateTimeField(verbose_name=&quot;\u6295\u7a3f\u65e5\u6642&quot;,default=timezone.now)\n\n    def __str__(self):\n        return self.comment<\/code><\/pre><\/div>\n\n\n\n<p>\u5b9f\u884c\u3059\u308b\u3068\u304d\u306b\u5024\u304c\u5909\u308f\u308b\u3001\u30e1\u30bd\u30c3\u30c9\u306e<code>timezone.now()<\/code>\u3067\u306f\u306a\u304f\u3001\u305d\u306e\u95a2\u6570\u305d\u306e\u3082\u306e\u3092\u610f\u5473\u3059\u308b\u5c5e\u6027\u5024\u306e<code>timezone.now<\/code>\u3092\u6307\u5b9a\u3059\u308b\u3002<code>timezone<\/code>\u306f<code>django.utils<\/code>\u306e\u4e2d\u306b\u542b\u307e\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u5192\u982d\u3067\u30a4\u30f3\u30dd\u30fc\u30c8\u3055\u305b\u308b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65e2\u306b\u30ec\u30b3\u30fc\u30c9\u304c\u5b58\u5728\u3059\u308b\u72b6\u614b\u3067\u3001NULL\u7981\u6b62\u304b\u3064\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u6307\u5b9a\u306a\u3057\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u3068\u3053\u3046\u306a\u308b\u3002\u30c7\u30d5\u30a9\u30eb\u30c8\u6307\u5b9a\u3057\u3066\u3044\u306a\u3044\u306e\u3067\u3001\u65e2\u5b58\u306e\u30ec\u30b3\u30fc\u30c9\u306b\u306fNULL\u7981\u6b62\u3067\u3042\u308b\u306b\u3082\u95a2\u308f\u3089\u305a\u3001NULL\u304c\u5165\u3063\u3066\u3057\u307e\u3046\u3002\u305d\u3053\u3067\u65e2\u5b58\u306e\u30ec\u30b3<\/p>\n","protected":false},"author":1,"featured_media":2049,"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-1896","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\/1896","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=1896"}],"version-history":[{"count":1,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/1896\/revisions"}],"predecessor-version":[{"id":1897,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/posts\/1896\/revisions\/1897"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=\/wp\/v2\/media\/2049"}],"wp:attachment":[{"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agile-software.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}