{"id":6819,"date":"2024-11-14T12:28:55","date_gmt":"2024-11-14T04:28:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6819\/"},"modified":"2024-11-14T12:28:55","modified_gmt":"2024-11-14T04:28:55","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-django-orm-%e5%9c%a8%e6%a8%a1%e5%9e%8b%e5%ad%97%e6%ae%b5%e4%b8%ad%e5%ad%98%e5%82%a8-mysql-now-%e5%87%bd%e6%95%b0%e7%9a%84%e5%bd%93%e5%89%8d%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6819\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Django ORM \u5728\u6a21\u578b\u5b57\u6bb5\u4e2d\u5b58\u50a8 MySQL NOW() \u51fd\u6570\u7684\u5f53\u524d\u65f6\u95f4\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172994762235966.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 Django ORM \u5728\u6a21\u578b\u5b57\u6bb5\u4e2d\u5b58\u50a8 MySQL NOW() \u51fd\u6570\u7684\u5f53\u524d\u65f6\u95f4\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 Django ORM \u5728\u6a21\u578b\u5b57\u6bb5\u4e2d\u5b58\u50a8 MySQL NOW() \u51fd\u6570\u7684\u5f53\u524d\u65f6\u95f4\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 django orm \u5b9e\u73b0  now() \u51fd\u6570<\/strong><\/p>\n<p>\u5728 django \u4e2d\u4f7f\u7528 mysql now() \u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5f53\u524d\u65f6\u95f4\u5b58\u50a8\u5728\u6a21\u578b\u5b57\u6bb5\u4e2d\uff0c\u907f\u514d\u4f7f\u7528\u540e\u7aef\u7684\u673a\u5668\u65f6\u95f4\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u5728\u6a21\u578b\u7c7b\u4e2d\u5b9a\u4e49\u5b57\u6bb5\uff0c\u5e76\u6307\u5b9a <strong>auto_now_add<\/strong> \u503c\u4e3a <strong>true<\/strong>\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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>auto_now_add<\/strong> \u53c2\u6570\u6307\u793a django \u5728\u6a21\u578b\u521b\u5efa\u65f6\uff0c\u5c06\u5b57\u6bb5\u503c\u8bbe\u7f6e\u4e3a mysql \u7684 now() \u51fd\u6570\u7684\u5f53\u524d\u65f6\u95f4\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 <strong>auto_now_add<\/strong> \u65f6\uff0c\u786e\u4fdd\u6570\u636e\u5e93\u8868\u4e2d\u76f8\u5e94\u5b57\u6bb5\u7684\u5b57\u6bb5\u7c7b\u578b\u4e3a <strong>datetime<\/strong> \u6216 <strong>timestamp<\/strong>\u3002<\/p>\n<p><strong>\u6587\u6863\u53c2\u8003\uff1a<\/strong><\/p>\n<p>\u6709\u5173 django orm \u4e2d\u6570\u636e\u5e93\u51fd\u6570\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605 django \u6587\u6863\uff1a<\/p>\n<ul>\n<li>\u300adjango documentation | database functions &#8211; now\u300b<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 Django ORM \u5728\u6a21\u578b\u5b57\u6bb5\u4e2d\u5b58\u50a8 MySQL NOW() \u51fd\u6570\u7684\u5f53\u524d\u65f6\u95f4\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 django orm \u5b9e\u73b0 now() \u51fd\u6570 \u5728 django \u4e2d\u4f7f\u7528 mysql now() \u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5f53\u524d\u65f6\u95f4\u5b58\u50a8\u5728\u6a21\u578b\u5b57\u6bb5\u4e2d\uff0c\u907f\u514d\u4f7f\u7528\u540e\u7aef\u7684\u673a\u5668\u65f6\u95f4\u3002 \u89e3\u51b3\u65b9\u6cd5\uff1a \u9996\u5148\uff0c\u5728\u6a21\u578b\u7c7b\u4e2d\u5b9a\u4e49\u5b57\u6bb5\uff0c\u5e76\u6307\u5b9a auto_now_add \u503c\u4e3a true\uff0c\u5982\u4e0b\u6240\u793a\uff1a from django.db import models class MyModel(models.Model): created_at = models.DateTimeField(auto_now_add=True) \u767b\u5f55\u540e\u590d\u5236 auto_now_add \u53c2\u6570\u6307\u793a django \u5728\u6a21\u578b\u521b\u5efa\u65f6\uff0c\u5c06\u5b57\u6bb5\u503c\u8bbe\u7f6e\u4e3a mysql \u7684 now() \u51fd\u6570\u7684\u5f53\u524d\u65f6\u95f4\u3002 \u6ce8\u610f\uff1a \u4f7f\u7528 auto_now_add \u65f6\uff0c\u786e\u4fdd\u6570\u636e\u5e93\u8868\u4e2d\u76f8\u5e94\u5b57\u6bb5\u7684\u5b57\u6bb5\u7c7b\u578b\u4e3a datetime \u6216 timestamp\u3002 \u6587\u6863\u53c2\u8003\uff1a \u6709\u5173 django orm \u4e2d\u6570\u636e\u5e93\u51fd\u6570\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605 django \u6587\u6863\uff1a \u300adjango documentation | database functions &#8211; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-6819","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=6819"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6819\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}