{"id":7208,"date":"2026-09-11T18:12:28","date_gmt":"2026-09-11T10:12:28","guid":{"rendered":"https:\/\/t.n-years.com\/?p=7208"},"modified":"2026-09-11T18:12:28","modified_gmt":"2026-09-11T10:12:28","slug":"django-%e6%a1%86%e6%9e%b6%e7%9a%84%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=7208","title":{"rendered":"Django \u6846\u67b6\u7684\u57fa\u7840\u77e5\u8bc6"},"content":{"rendered":"<p>\u5f53\u7136\u3002\u9605\u8bfb Django \u9879\u76ee\u524d\uff0c\u5148\u638c\u63e1\u8fd9\u4e9b\u6982\u5ff5\u5c31\u591f\u7528\u4e86\uff1a<\/p>\n<p><strong>\u6838\u5fc3\u6982\u5ff5<\/strong><\/p>\n<ul>\n<li><strong>Django<\/strong>\uff1aPython \u7684 Web \u6846\u67b6\uff0c\u4e3b\u6253\u201c\u81ea\u5e26\u7535\u6c60\u201d\uff08ORM\u3001\u540e\u53f0\u7ba1\u7406\u3001\u8ba4\u8bc1\u3001\u8def\u7531\u7b49\u5747\u5185\u7f6e\uff09\u3002<\/li>\n<li><strong>\u9879\u76ee\uff08project\uff09<\/strong>\uff1a\u6574\u4e2a\u670d\u52a1\u7684\u914d\u7f6e\u5bb9\u5668\uff0c\u901a\u5e38\u5305\u542b <code>settings.py<\/code>\u3001\u6839\u8def\u7531 <code>urls.py<\/code>\u3001<code>wsgi.py<\/code> \/ <code>asgi.py<\/code>\u3002<\/li>\n<li><strong>\u5e94\u7528\uff08app\uff09<\/strong>\uff1a\u6309\u4e1a\u52a1\u62c6\u5206\u7684\u6a21\u5757\uff0c\u5982\u7528\u6237\u3001\u8ba2\u5355\u3001\u8d44\u4ea7\uff1b\u901a\u5e38\u542b <code>models.py<\/code>\u3001<code>views.py<\/code>\u3001<code>urls.py<\/code>\u3001<code>admin.py<\/code>\u3002<\/li>\n<li><strong>MVT \u6a21\u5f0f<\/strong>\uff1a<code>Model<\/code> \u7ba1\u6570\u636e\uff0c<code>View<\/code> \u5904\u7406\u8bf7\u6c42\u548c\u4e1a\u52a1\uff0c<code>Template<\/code> \u6e32\u67d3\u9875\u9762\uff1bAPI \u9879\u76ee\u901a\u5e38\u76f4\u63a5\u8fd4\u56de JSON\uff0c\u4e0d\u4e00\u5b9a\u6709\u6a21\u677f\u3002<\/li>\n<\/ul>\n<p><strong>\u8bf7\u6c42\u94fe\u8def<\/strong><\/p>\n<pre><code class=\"language-text\">HTTP \u8bf7\u6c42\n  \u2192 \u6839 urls.py\n  \u2192 \u5e94\u7528 urls.py\n  \u2192 view\uff08\u51fd\u6570\u6216\u7c7b\uff09\n  \u2192 serializer \/ service \/ model\n  \u2192 \u6570\u636e\u5e93\n  \u2192 HTTP \/ JSON \u54cd\u5e94<\/code><\/pre>\n<p>\u770b\u63a5\u53e3\u4ee3\u7801\u65f6\uff0c\u901a\u5e38\u4ece\u8def\u7531\u5165\u624b\uff0c\u8ffd\u5230 View\uff0c\u518d\u770b\u5b83\u8c03\u7528\u7684\u5e8f\u5217\u5316\u5668\u3001\u4e1a\u52a1\u903b\u8f91\u4e0e\u6a21\u578b\u3002<\/p>\n<p><strong>\u5173\u952e\u6587\u4ef6<\/strong><\/p>\n<ul>\n<li><code>settings.py<\/code>\uff1a\u5e94\u7528\u6ce8\u518c\u3001\u6570\u636e\u5e93\u3001\u7f13\u5b58\u3001\u4e2d\u95f4\u4ef6\u3001\u8ba4\u8bc1\u3001\u73af\u5883\u914d\u7f6e\u3002<\/li>\n<li><code>urls.py<\/code>\uff1aURL \u4e0e\u89c6\u56fe\u7684\u6620\u5c04\uff0c\u4f8b\u5982 <code>path(&quot;users\/&quot;, ...)<\/code>\u3002<\/li>\n<li><code>views.py<\/code>\uff1a\u8bf7\u6c42\u5165\u53e3\uff1b\u51fd\u6570\u89c6\u56fe\u7528 <code>def<\/code>\uff0c\u7c7b\u89c6\u56fe\u5e38\u7ee7\u627f Django \u6216 DRF \u7684\u57fa\u7c7b\u3002<\/li>\n<li><code>models.py<\/code>\uff1a\u6570\u636e\u5e93\u8868\u7684 Python \u5b9a\u4e49\u3002\u4e00\u4e2a <code>models.Model<\/code> \u5b50\u7c7b\u901a\u5e38\u5bf9\u5e94\u4e00\u5f20\u8868\u3002<\/li>\n<li><code>migrations\/<\/code>\uff1a\u6570\u636e\u5e93\u7ed3\u6784\u53d8\u66f4\u8bb0\u5f55\uff1b\u4fee\u6539\u6a21\u578b\u540e\u751f\u6210\u5e76\u6267\u884c\u8fc1\u79fb\u3002<\/li>\n<li><code>admin.py<\/code>\uff1aDjango \u7ba1\u7406\u540e\u53f0\u4e2d\u6ce8\u518c\u548c\u914d\u7f6e\u6a21\u578b\u3002<\/li>\n<li><code>manage.py<\/code>\uff1a\u9879\u76ee\u547d\u4ee4\u5165\u53e3\uff0c\u5982 <code>runserver<\/code>\u3001<code>migrate<\/code>\u3001<code>shell<\/code>\u3002<\/li>\n<\/ul>\n<p><strong>ORM\uff08\u6570\u636e\u5e93\u64cd\u4f5c\uff09<\/strong><br \/>\nDjango \u7528 ORM \u5c06 Python \u5bf9\u8c61\u6620\u5c04\u4e3a\u6570\u636e\u5e93\u8bb0\u5f55\uff1a<\/p>\n<pre><code class=\"language-python\">User.objects.get(id=1)                 # \u67e5\u8be2\u4e00\u6761\nUser.objects.filter(is_active=True)    # \u67e5\u8be2\u96c6\u5408\nUser.objects.create(username=&quot;alice&quot;)  # \u65b0\u589e\nuser.save()                            # \u66f4\u65b0\nuser.delete()                          # \u5220\u9664<\/code><\/pre>\n<p>\u5e38\u89c1\u5b57\u6bb5\u5173\u7cfb\uff1a<\/p>\n<ul>\n<li><code>ForeignKey<\/code>\uff1a\u591a\u5bf9\u4e00\uff0c\u4f8b\u5982\u591a\u4e2a\u8ba2\u5355\u5c5e\u4e8e\u4e00\u4e2a\u7528\u6237\u3002<\/li>\n<li><code>OneToOneField<\/code>\uff1a\u4e00\u5bf9\u4e00\uff0c\u4f8b\u5982\u7528\u6237\u4e0e\u6269\u5c55\u8d44\u6599\u3002<\/li>\n<li><code>ManyToManyField<\/code>\uff1a\u591a\u5bf9\u591a\uff0c\u4f8b\u5982\u7528\u6237\u4e0e\u89d2\u8272\u3002<\/li>\n<\/ul>\n<p><strong>\u914d\u7f6e\u4e0e\u542f\u52a8<\/strong><\/p>\n<ul>\n<li><code>INSTALLED_APPS<\/code>\uff1a\u542f\u7528\u54ea\u4e9b Django \u5e94\u7528\u3002<\/li>\n<li><code>MIDDLEWARE<\/code>\uff1a\u8bf7\u6c42\/\u54cd\u5e94\u5fc5\u7ecf\u7684\u5904\u7406\u94fe\uff0c\u5982\u8ba4\u8bc1\u3001CORS\u3001\u5f02\u5e38\u5904\u7406\u3002<\/li>\n<li><code>DATABASES<\/code>\uff1a\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u3002<\/li>\n<li><code>DEBUG<\/code>\uff1a\u5f00\u53d1\u73af\u5883\u901a\u5e38\u4e3a <code>True<\/code>\uff0c\u751f\u4ea7\u5fc5\u987b\u5173\u95ed\u3002<\/li>\n<li><code>python manage.py migrate<\/code>\uff1a\u5e94\u7528\u6570\u636e\u5e93\u8fc1\u79fb\u3002<\/li>\n<li><code>python manage.py runserver<\/code>\uff1a\u672c\u5730\u542f\u52a8\u5f00\u53d1\u670d\u52a1\u3002<\/li>\n<\/ul>\n<p><strong>\u82e5\u9879\u76ee\u4f7f\u7528 Django REST Framework\uff08DRF\uff09<\/strong><br \/>\n\u540e\u7aef API \u9879\u76ee\u5f88\u5e38\u89c1\uff1a<\/p>\n<ul>\n<li><code>Serializer<\/code>\uff1a\u8d1f\u8d23\u6a21\u578b\u5bf9\u8c61\u4e0e JSON \u4e4b\u95f4\u7684\u8f6c\u6362\u53ca\u53c2\u6570\u6821\u9a8c\u3002<\/li>\n<li><code>APIView<\/code> \/ <code>ViewSet<\/code>\uff1a\u5904\u7406 HTTP \u65b9\u6cd5\u6216\u4e00\u7ec4\u8d44\u6e90\u64cd\u4f5c\u3002<\/li>\n<li><code>router<\/code>\uff1a\u81ea\u52a8\u4e3a <code>ViewSet<\/code> \u751f\u6210 REST \u8def\u7531\u3002<\/li>\n<li><code>permission_classes<\/code>\uff1a\u5b9a\u4e49\u63a5\u53e3\u6743\u9650\u3002<\/li>\n<li><code>authentication_classes<\/code>\uff1a\u5b9a\u4e49\u8ba4\u8bc1\u673a\u5236\uff0c\u4f8b\u5982 Token\/JWT\u3002<\/li>\n<li><code>Response<\/code>\uff1a\u8fd4\u56de JSON \u54cd\u5e94\u3002<\/li>\n<\/ul>\n<p>\u4e00\u4e2a\u5178\u578b API \u7684\u9605\u8bfb\u987a\u5e8f\uff1a<\/p>\n<pre><code class=\"language-text\">urls.py \/ router\n  \u2192 ViewSet \u6216 APIView\n  \u2192 Serializer\uff08\u5165\u53c2\u6821\u9a8c\u3001\u51fa\u53c2\u683c\u5f0f\uff09\n  \u2192 Model \/ Service\uff08\u6301\u4e45\u5316\u4e0e\u4e1a\u52a1\u89c4\u5219\uff09\n  \u2192 Permission \/ Authentication\uff08\u6743\u9650\u4e0e\u8eab\u4efd\uff09<\/code><\/pre>\n<p><strong>\u6700\u503c\u5f97\u4f18\u5148\u5173\u6ce8\u7684\u5751<\/strong><\/p>\n<ul>\n<li>Django \u7684 QuerySet \u662f<strong>\u60f0\u6027\u6267\u884c<\/strong>\u7684\uff1a\u771f\u6b63\u904d\u5386\u3001\u5207\u7247\u6216\u5e8f\u5217\u5316\u65f6\u624d\u53ef\u80fd\u67e5\u5e93\u3002<\/li>\n<li>\u5173\u8054\u5b57\u6bb5\u8bbf\u95ee\u53ef\u80fd\u5e26\u6765 <strong>N+1 \u67e5\u8be2<\/strong>\uff1b\u6ce8\u610f <code>select_related()<\/code> \u4e0e <code>prefetch_related()<\/code>\u3002<\/li>\n<li>\u533a\u5206 <code>request.GET<\/code>\uff08\u67e5\u8be2\u53c2\u6570\uff09\u3001<code>request.POST<\/code>\uff08\u8868\u5355\uff09\u548c API \u4e2d\u7684 <code>request.data<\/code>\uff08DRF JSON \u8bf7\u6c42\u4f53\uff09\u3002<\/li>\n<li>\u6570\u636e\u5e93\u7ed3\u6784\u4ee5 <strong>migration<\/strong> \u4e3a\u51c6\uff0c\u4e0d\u53ea\u770b\u5f53\u524d <code>models.py<\/code>\u3002<\/li>\n<li>\u914d\u7f6e\u53ef\u80fd\u4ece\u73af\u5883\u53d8\u91cf\u3001<code>.env<\/code> \u6216\u6309\u73af\u5883\u62c6\u5206\u7684 settings \u6587\u4ef6\u8bfb\u53d6\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u7136\u3002\u9605\u8bfb Django \u9879\u76ee\u524d\uff0c\u5148\u638c\u63e1\u8fd9\u4e9b\u6982\u5ff5\u5c31\u591f\u7528\u4e86\uff1a \u6838\u5fc3\u6982\u5ff5 Django\uff1aPython \u7684 Web &#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=7208\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[],"class_list":["post-7208","post","type-post","status-publish","format-standard","hentry","category-123"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/7208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7208"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/7208\/revisions"}],"predecessor-version":[{"id":7209,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/7208\/revisions\/7209"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}