{"id":61199,"date":"2024-10-13T09:15:16","date_gmt":"2024-10-13T01:15:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/61199\/"},"modified":"2024-10-13T09:15:16","modified_gmt":"2024-10-13T01:15:16","slug":"django%e5%bc%95%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9a%e6%9b%b4%e9%ab%98%e6%95%88%e7%9a%84%e6%95%b8%e6%93%9a%e5%ad%98%e5%84%b2%e7%ae%a1%e7%90%86-django-%e5%bc%95%e5%85%a5%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/61199\/","title":{"rendered":"Django\u5f15\u5165\u6578\u64da\u5eab\uff1a\u66f4\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u7ba1\u7406 (django \u5f15\u5165\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"django%e5%bc%95%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9a%e6%9b%b4%e9%ab%98%e6%95%88%e7%9a%84%e6%95%b8%e6%93%9a%e5%ad%98%e5%84%b2%e7%ae%a1%e7%90%86-GBzgtRICGx\">Django\u5f15\u5165\u6578\u64da\u5eab\uff1a\u66f4\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u7ba1\u7406<\/h1>\n<p>Django\u662f\u4e00\u500b\u9ad8\u6548\u7684Python\u7db2\u9801\u6846\u67b6\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u958b\u767c\u5404\u985e\u578b\u7684\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5f0f\u3002\u5176\u5f37\u5927\u7684\u6578\u64da\u5eab\u7ba1\u7406\u529f\u80fd\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u8655\u7406\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728Django\u4e2d\u5f15\u5165\u6578\u64da\u5eab\uff0c\u4e26\u4ecb\u7d39\u5176\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u7ba1\u7406\u65b9\u5f0f\u3002<\/p>\n<h2 id=\"django%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%94%af%e6%8c%81-GBzgtRICGx\">Django\u7684\u6578\u64da\u5eab\u652f\u6301<\/h2>\n<p>Django\u652f\u6301\u591a\u7a2e\u6578\u64da\u5eab\uff0c\u5305\u62ecSQLite\u3001PostgreSQL\u3001MySQL\u548cOracle\u7b49\u3002\u9019\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9805\u76ee\u7684\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u3002Django\u7684ORM\uff08\u5c0d\u8c61\u95dc\u4fc2\u6620\u5c04\uff09\u7cfb\u7d71\u4f7f\u5f97\u6578\u64da\u5eab\u64cd\u4f5c\u8b8a\u5f97\u7c21\u55ae\u800c\u76f4\u89c0\uff0c\u958b\u767c\u8005\u7121\u9700\u7de8\u5beb\u8907\u96dc\u7684SQL\u8a9e\u53e5\u5373\u53ef\u9032\u884c\u6578\u64da\u64cd\u4f5c\u3002<\/p>\n<h3 id=\"%e9%85%8d%e7%bd%ae%e6%95%b8%e6%93%9a%e5%ba%ab-GBzgtRICGx\">\u914d\u7f6e\u6578\u64da\u5eab<\/h3>\n<p>\u5728Django\u4e2d\u914d\u7f6e\u6578\u64da\u5eab\u975e\u5e38\u7c21\u55ae\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u9805\u76ee\u7684\u8a2d\u5b9a\u6587\u4ef6\uff08settings.py\uff09\u4e2d\u6307\u5b9a\u6578\u64da\u5eab\u7684\u985e\u578b\u548c\u9023\u63a5\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528PostgreSQL\u7684\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nDATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.postgresql',\n        'NAME': 'your_database_name',\n        'USER': 'your_username',\n        'PASSWORD': 'your_password',\n        'HOST': 'localhost',\n        'PORT': '5432',\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u914d\u7f6e\u5b8c\u6210\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Django\u7684\u547d\u4ee4\u884c\u5de5\u5177\u4f86\u5275\u5efa\u6578\u64da\u5eab\u8868\uff1a<\/p>\n<pre><code>\npython manage.py migrate\n<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8django%e7%9a%84orm%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e6%93%8d%e4%bd%9c-GBzgtRICGx\">\u4f7f\u7528Django\u7684ORM\u9032\u884c\u6578\u64da\u64cd\u4f5c<\/h2>\n<p>Django\u7684ORM\u5141\u8a31\u958b\u767c\u8005\u4f7f\u7528Python\u5c0d\u8c61\u4f86\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u9019\u6a23\u4e0d\u50c5\u63d0\u9ad8\u4e86\u958b\u767c\u6548\u7387\uff0c\u9084\u6e1b\u5c11\u4e86\u56e0\u624b\u52d5\u7de8\u5bebSQL\u8a9e\u53e5\u800c\u53ef\u80fd\u51fa\u73fe\u7684\u932f\u8aa4\u3002<\/p>\n<h3 id=\"%e5%89%b5%e5%bb%ba%e6%a8%a1%e5%9e%8b-GBzgtRICGx\">\u5275\u5efa\u6a21\u578b<\/h3>\n<p>\u5728Django\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u904e\u5275\u5efa\u6a21\u578b\u4f86\u5b9a\u7fa9\u6578\u64da\u7d50\u69cb\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u6a21\u578b\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nfrom django.db import models\n\nclass Article(models.Model):\n    title = models.CharField(max_length=200)\n    content = models.TextField()\n    created_at = models.DateTimeField(auto_now_add=True)\n<\/code><\/pre>\n<p>\u9019\u500b\u6a21\u578b\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70baArticle\u7684\u6578\u64da\u8868\uff0c\u5305\u542b\u6a19\u984c\u3001\u5167\u5bb9\u548c\u5275\u5efa\u6642\u9593\u7b49\u5b57\u6bb5\u3002<\/p>\n<h3 id=\"%e6%95%b8%e6%93%9a%e7%9a%84%e5%a2%9e%e5%88%aa%e6%94%b9%e6%9f%a5-GBzgtRICGx\">\u6578\u64da\u7684\u589e\u522a\u6539\u67e5<\/h3>\n<p>\u4f7f\u7528Django\u7684ORM\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u9032\u884c\u6578\u64da\u7684\u589e\u522a\u6539\u67e5\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li><strong>\u65b0\u589e\u6578\u64da\uff1a<\/strong><\/li>\n<pre><code>\narticle = Article(title='\u65b0\u6587\u7ae0', content='\u9019\u662f\u4e00\u7bc7\u65b0\u6587\u7ae0\u3002')\narticle.save()\n<\/code><\/pre>\n<li><strong>\u67e5\u8a62\u6578\u64da\uff1a<\/strong><\/li>\n<pre><code>\narticles = Article.objects.all()\n<\/code><\/pre>\n<li><strong>\u66f4\u65b0\u6578\u64da\uff1a<\/strong><\/li>\n<pre><code>\narticle = Article.objects.get(id=1)\narticle.title = '\u66f4\u65b0\u5f8c\u7684\u6a19\u984c'\narticle.save()\n<\/code><\/pre>\n<li><strong>\u522a\u9664\u6578\u64da\uff1a<\/strong><\/li>\n<pre><code>\narticle = Article.objects.get(id=1)\narticle.delete()\n<\/code><\/pre>\n<\/ul>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e9%81%b7%e7%a7%bb-GBzgtRICGx\">\u6578\u64da\u5eab\u9077\u79fb<\/h2>\n<p>\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u6578\u64da\u6a21\u578b\u53ef\u80fd\u6703\u767c\u751f\u8b8a\u5316\u3002Django\u63d0\u4f9b\u4e86\u6578\u64da\u5eab\u9077\u79fb\u529f\u80fd\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u8f15\u9b06\u7ba1\u7406\u6578\u64da\u5eab\u7d50\u69cb\u7684\u8b8a\u66f4\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210\u9077\u79fb\u6587\u4ef6\uff1a<\/p>\n<pre><code>\npython manage.py makemigrations\n<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u61c9\u7528\u9077\u79fb\uff1a<\/p>\n<pre><code>\npython manage.py migrate\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-GBzgtRICGx\">\u7e3d\u7d50<\/h2>\n<p>Django\u7684\u6578\u64da\u5eab\u7ba1\u7406\u529f\u80fd\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u9ad8\u6548\u5730\u9032\u884c\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\u3002\u901a\u904e\u7c21\u55ae\u7684\u914d\u7f6e\u548c\u5f37\u5927\u7684ORM\u7cfb\u7d71\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u9032\u884c\u6578\u64da\u7684\u589e\u522a\u6539\u67e5\u64cd\u4f5c\uff0c\u4e26\u4e14\u80fd\u5920\u9748\u6d3b\u5730\u61c9\u5c0d\u6578\u64da\u6a21\u578b\u7684\u8b8a\u5316\u3002\u5c0d\u65bc\u9700\u8981\u7a69\u5b9a\u548c\u9ad8\u6548\u6578\u64da\u5b58\u5132\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684VPS\u89e3\u6c7a\u65b9\u6848\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u548c<a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a>\u7684\u8cc7\u8a0a\uff0c\u5c07\u6709\u52a9\u65bc\u60a8\u5728\u958b\u767c\u904e\u7a0b\u4e2d\u505a\u51fa\u66f4\u660e\u667a\u7684\u9078\u64c7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Django\u5f15\u5165\u6578\u64da\u5eab\u7684\u9ad8\u6548\u6578\u64da\u5b58\u5132\u7ba1\u7406\u65b9\u6cd5\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u6578\u64da\u8655\u7406\u80fd\u529b\uff0c\u7c21\u5316\u958b\u767c\u6d41\u7a0b\u3002<\/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-61199","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61199","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=61199"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61199\/revisions"}],"predecessor-version":[{"id":61200,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61199\/revisions\/61200"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=61199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=61199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=61199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}