{"id":451,"date":"2015-04-29T22:58:02","date_gmt":"2015-04-29T13:58:02","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2015\/04\/29\/django\/"},"modified":"2015-04-29T22:58:02","modified_gmt":"2015-04-29T13:58:02","slug":"django","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2015\/04\/29\/django\/","title":{"rendered":"Django \u65e5\u6642\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u6bd4\u8f03\u3092\u884c\u3046"},"content":{"rendered":"<p>Python2.7 + Django 1.7 \u3067\u65e5\u6642\u30d5\u30a3\u30fc\u30eb\u30c9(DateTimeField) \u306e\u6bd4\u8f03\u3092\u884c\u3046\u3002<\/p>\n<p>\u671f\u9650\u30921\u65e5\u5f8c\u306b\u8a2d\u5b9a\u3057\u3001\u305d\u306e\u671f\u9593\u306b\u5165\u3063\u3066\u3044\u308b\u304b\u3092\u8abf\u3079\u308b\u3068\u3044\u3046\u3053\u3068\u3092\u3084\u308a\u305f\u3044\u3002<\/p>\n<p>&#160;<\/p>\n<p>\u5217\u306e\u5b9a\u7fa9\u306f\u3053\u3093\u306a\u611f\u3058\u3002<\/p>\n<pre class=\"prettyprint linenums\">activate_date_time = models.DateTimeField(null=True)<\/pre>\n<p>\u3042\u307e\u308a\u8003\u3048\u305a\u306b\u3001<\/p>\n<pre class=\"prettyprint linenums\">from datetime import datetime,timedelta\r\nuser.activate_limit_date_time = datetime.now() + timedelta(days=1)\r\nuser.save()<\/pre>\n<p>\u3066\u306a\u611f\u3058\u3067\u8a2d\u5b9a\u3057\u3066\u3001\u6bd4\u8f03\u3059\u308b\u3068\u3082\u308c\u306a\u304f <\/p>\n<pre class=\"prettyprint linenums\">if datetime.now() &lt;= user.activate_limit_date_time:\r\n\t#OK\r\nelse:\r\n\t#NG<\/pre>\n<p>\u3053\u3093\u306a\u30a8\u30e9\u30fc <\/p>\n<pre class=\"prettyprint linenums\">can't compare offset-naive and offset-aware datetimes<\/pre>\n<p>\u65e5\u4ed8\u306e\u6bd4\u8f03\u306b\u306f\u3001\u30bf\u30a4\u30e0\u30be\u30fc\u30f3\u60c5\u5831\u3092\u6301\u3063\u3066\u3044\u308b\u3082\u306e\u3068\u6301\u3063\u3066\u3044\u306a\u3044\u7269\u3092\u6bd4\u8f03\u3059\u308b\u3068\u3053\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u3068\u306a\u308b\u3088\u3046\u3060\u3002 <\/p>\n<p><a href=\"http:\/\/django-docs-ja.readthedocs.org\/en\/latest\/topics\/i18n\/timezones.html\">http:\/\/django-docs-ja.readthedocs.org\/en\/latest\/topics\/i18n\/timezones.html<\/a><\/p>\n<p><a href=\"http:\/\/www.amazon.co.jp\/gp\/product\/479804315X\/ref=as_li_ss_il?ie=UTF8&amp;camp=247&amp;creative=7399&amp;creativeASIN=479804315X&amp;linkCode=as2&amp;tag=typea09-22\"><img decoding=\"async\" border=\"0\" src=\"http:\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=479804315X&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\" \/><\/a><img loading=\"lazy\" decoding=\"async\" style=\"border-top-style: none !important; border-bottom-style: none !important; border-right-style: none !important; margin: 0px; border-left-style: none !important\" border=\"0\" alt=\"\" src=\"http:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=as2&amp;o=9&amp;a=479804315X\" width=\"1\" height=\"1\" \/> <\/p>\n<p>&#160;<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u5185\u90e8\u7684\u306b\u306f\u3001UTC\u3067\u3082\u3063\u3066\u304a\u3044\u3066\u3001\u30e6\u30fc\u30b6\u30fc\u306b\u8868\u793a\u3059\u308b\u3068\u304d\u306b\u30ed\u30fc\u30ab\u30eb\u6642\u9593\u306b\u8868\u793a\u3059\u308b\u306e\u304c\u826f\u3055\u305d\u3046\u3060\u3002<\/p>\n<pre class=\"prettyprint linenums\">from datetime import datetime,timedelta\r\nfrom django.utils.timezone import utc\r\n\r\nutc_now = datetime.utcnow().replace(tzinfo=utc)\r\nuser.activate_limit_date_time = utc_now + timedelta(days=1)\r\nuser.save()<\/pre>\n<p>\u3068\u3044\u3046\u611f\u3058\u306b\u66f8\u304d\u63db\u3048\u3066\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u6bd4\u8f03\u3059\u308b<\/p>\n<pre class=\"prettyprint linenums\">utc_now = datetime.utcnow().replace(tzinfo=utc)\r\nif utc_now &lt;= user.activate_limit_date_time:\r\n\t#OK\r\nelse:\r\n\t#NG<\/pre>\n<p>\u30d7\u30ea\u30f3\u30c8\u3059\u308b\u3068\u3001\u305d\u308c\u305e\u308c\u4ee5\u4e0b<\/p>\n<pre class=\"prettyprint linenums\">#\u767b\u9332\u6642\r\n2015-04-29 13:43:37.829000+00:00\r\n2015-04-30 13:43:37.829000+00:00\r\n\r\n#\u6bd4\u8f03\u6642\r\n2015-04-30 13:43:37.829000+00:00\r\n2015-04-30 13:43:37.829000+00:00<\/pre>\n<p>\u60f3\u5b9a\u3069\u304a\u308a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python2.7 + Django 1.7 \u3067\u65e5\u6642\u30d5\u30a3\u30fc\u30eb\u30c9(DateTimeField) \u306e\u6bd4\u8f03\u3092\u884c\u3046\u3002 \u671f\u9650\u30921\u65e5\u5f8c\u306b\u8a2d\u5b9a\u3057\u3001\u305d\u306e\u671f\u9593\u306b\u5165\u3063\u3066\u3044\u308b\u304b\u3092\u8abf\u3079\u308b\u3068\u3044\u3046\u3053\u3068\u3092\u3084\u308a\u305f\u3044\u3002 &#160; \u5217\u306e\u5b9a\u7fa9\u306f\u3053\u3093\u306a\u611f\u3058 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[29,20],"tags":[],"class_list":["post-451","post","type-post","status-publish","format-standard","hentry","category-django","category-python"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=451"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/451\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}