{"id":200434,"date":"2025-05-06T13:09:33","date_gmt":"2025-05-06T05:09:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200434\/"},"modified":"2025-05-06T13:09:33","modified_gmt":"2025-05-06T05:09:33","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8-django-orm-%e5%9c%a8-mysql-%e4%b8%ad%e4%bd%bf%e7%94%a8-now-%e5%87%bd%e6%95%b0%e8%ae%be%e7%bd%ae%e6%97%b6%e9%97%b4%e6%88%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200434\/","title":{"rendered":"\u5982\u4f55\u5229\u7528 Django ORM \u5728 MySQL \u4e2d\u4f7f\u7528 NOW() \u51fd\u6570\u8bbe\u7f6e\u65f6\u95f4\u6233\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5229\u7528 Django ORM \u5728 MySQL \u4e2d\u4f7f\u7528 NOW() \u51fd\u6570\u8bbe\u7f6e\u65f6\u95f4\u6233\uff1f<\/h1>\n<p>\u6570\u636e\u5e93\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u5982\u4f55\u5229\u7528 Django ORM \u5728 MySQL \u4e2d\u4f7f\u7528 NOW() \u51fd\u6570\u8bbe\u7f6e\u65f6\u95f4\u6233\uff1f\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241105\/17307695196729726fa4abd.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u5728 django orm \u4e2d\u4f7f\u7528 mysql now \u51fd\u6570<\/strong><\/p>\n<p>\u5728 django \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 now() \u51fd\u6570\u8f7b\u677e\u5229\u7528 mysql \u7684 now() \u51fd\u6570\u3002\u6b64\u51fd\u6570\u53ef\u8ba9\u4f60\u5728\u63d2\u5165\u6570\u636e\u5e93\u8bb0\u5f55\u65f6\u81ea\u52a8\u8bbe\u7f6e\u65f6\u95f4\u6233\uff0c\u800c\u65e0\u9700\u4ece\u540e\u7aef\u673a\u5668\u83b7\u53d6\u65f6\u95f4\u3002<\/p>\n<p><strong>\u5b9e\u73b0\uff1a<\/strong><\/p>\n<p>\u8981\u4f7f\u7528 now() \u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u4f60\u7684\u6a21\u578b\u7c7b\u4e2d\u58f0\u660e\u4e00\u4e2a\u65f6\u95f4\u6233\u5b57\u6bb5\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>from django.db import models\n\nclass MyModel(models.Model):\n    created_at = models.DateTimeField(auto_now_add=True)<\/pre>\n<p>auto_now_add=true \u53c2\u6570\u6307\u793a django \u5728\u6bcf\u6b21\u521b\u5efa\u65b0\u8bb0\u5f55\u65f6\uff0c\u4f7f\u7528 now() \u51fd\u6570\u8bbe\u7f6e\u5b57\u6bb5\u7684\u503c\u3002<\/p>\n<p>\u4f7f\u7528 now() \u51fd\u6570\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u5b83\u6d88\u9664\u4e86\u65f6\u95f4\u5dee\u5f02\u7684\u95ee\u9898\u3002\u4ece\u540e\u7aef\u673a\u5668\u83b7\u53d6\u65f6\u95f4\u53ef\u80fd\u4f1a\u5bfc\u81f4\u670d\u52a1\u5668\u4e4b\u95f4\u7684\u4e0d\u4e00\u81f4\uff0c\u800c now() \u51fd\u6570\u901a\u8fc7\u8ba9\u6570\u636e\u5e93\u670d\u52a1\u5668\u5904\u7406\u65f6\u95f4\u6233\u6765\u786e\u4fdd\u7cbe\u5ea6\u3002<\/p>\n<p>\u5728\u4f7f\u7528 now() \u51fd\u6570\u65f6\uff0c\u8bf7\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u786e\u4fdd\u4f60\u7684\u6570\u636e\u5e93\u7248\u672c\u652f\u6301 now() \u51fd\u6570\u3002<\/li>\n<li>now() \u51fd\u6570\u4e0d\u4f1a\u968f\u65f6\u95f4\u66f4\u65b0\uff0c\u56e0\u6b64\u5982\u679c\u4f60\u9700\u8981\u53ef\u53d8\u7684\u65f6\u95f4\u6233\uff0c\u9700\u8981\u5bfb\u627e\u5176\u4ed6\u65b9\u6cd5\u3002<\/li>\n<li>\u4f7f\u7528 auto_now_add=true \u4f1a\u786e\u4fdd\u5728\u521b\u5efa\u8bb0\u5f55\u65f6\u8bbe\u7f6e\u65f6\u95f4\u6233\uff0c\u4f46\u4e0d\u4f1a\u5728\u66f4\u65b0\u8bb0\u5f55\u65f6\u5bf9\u5176\u8fdb\u884c\u66f4\u65b0\u3002<\/li>\n<\/ul>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0cnow() \u51fd\u6570\u662f\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u53ef\u8ba9\u4f60\u5728 django orm \u4e2d\u5229\u7528 mysql \u7684 now() \u51fd\u6570\uff0c\u5e76\u786e\u4fdd\u65f6\u95f4\u6233\u7684\u51c6\u786e\u6027\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6570\u636e\u5e93\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5229\u7528 Django ORM &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200434","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200434"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200434\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}