﻿{"id":1443,"date":"2023-01-16T22:16:54","date_gmt":"2023-01-16T14:16:54","guid":{"rendered":"https:\/\/www.tansoo.cn\/?p=1443"},"modified":"2023-01-16T22:16:54","modified_gmt":"2023-01-16T14:16:54","slug":"phpbb-70","status":"publish","type":"post","link":"https:\/\/www.tansoo.cn\/?p=1443","title":{"rendered":"Django\u7684Model\u5b57\u6bb5\u5e38\u7528\u53c2\u6570\u4ecb\u7ecd"},"content":{"rendered":"<p><span style=\"font-size:150%;line-height:116%\"><strong class=\"text-strong\">1.\u5e38\u7528\u53c2\u6570\u9009\u9879<\/strong><br \/>\n<span style=\"color:#0000FF\">null<\/span>\uff1a\u5982\u679c\u4e3a True\uff0c\u5f53\u8be5\u5b57\u6bb5\u4e3a\u7a7a\u65f6\uff0cDjango \u4f1a\u5c06\u6570\u636e\u5e93\u4e2d\u8be5\u5b57\u6bb5\u8bbe\u7f6e\u4e3a NULL\u3002\u9ed8\u8ba4\u4e3a False \u3002<\/p>\n<p><span style=\"color:#0040FF\">blank<\/span>\uff1a\u5982\u679c\u8bbe\u7f6e\u4e3a True\uff0c\u8be5\u5b57\u6bb5\u5141\u8bb8\u4e3a\u7a7a\u3002\u9ed8\u8ba4\u4e3a False\u3002<\/p>\n<p>    null \u9009\u9879\u4ec5\u4ec5\u662f\u6570\u636e\u5e93\u5c42\u9762\u7684\u8bbe\u7f6e\uff0c\u7136\u800c blank\u662f\u6d89\u53ca\u8868\u5355\u9a8c\u8bc1\u65b9\u9762\u3002\u5982\u679c\u4e00\u4e2a\u5b57\u6bb5\u8bbe\u7f6e\u4e3a blank=True \uff0c\u5728\u8fdb\u884c\u8868\u5355\u9a8c\u8bc1\u65f6\uff0c\u63a5\u6536\u7684\u6570\u636e\u8be5\u5b57\u6bb5\u503c\u5141\u8bb8\u4e3a\u7a7a\uff0c\u800c\u8bbe\u7f6e\u4e3a blank=False \u65f6\uff0c\u4e0d\u5141\u8bb8\u4e3a\u7a7a\u3002<\/p>\n<p><span style=\"color:#0040FF\">choice<\/span>\uff1a\u4e8c\u5143\u7ec4<\/p>\n<p><span style=\"color:#0040FF\">default<\/span>\uff1a\u9ed8\u8ba4\u503c<\/p>\n<p><span style=\"color:#0040FF\">help_text<\/span>\uff1a\u989d\u5916\u7684\u201c\u5e2e\u52a9\u201d\u6587\u672c\uff0c\u968f\u8868\u5355\u63a7\u4ef6\u4e00\u540c\u663e\u793a\u3002\u5373\u4fbf\u4f60\u7684\u5b57\u6bb5\u672a\u7528\u4e8e\u8868\u5355\uff0c\u5b83\u5bf9\u4e8e\u751f\u6210\u6587\u6863\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002<\/p>\n<p><span style=\"color:#0040FF\">primary_key<\/span>\uff1a\u5982\u679c\u8bbe\u7f6e\u4e3a True \uff0c\u5c06\u8be5\u5b57\u6bb5\u8bbe\u7f6e\u4e3a\u8be5\u6a21\u578b\u7684\u4e3b\u952e\u3002<\/p>\n<p>    \u5728\u4e00\u4e2a\u6a21\u578b\u4e2d\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u5bf9\u4efb\u4f55\u4e00\u4e2a\u5b57\u6bb5\u8bbe\u7f6e primary_key=True \u9009\u9879\u3002 Django \u4f1a\u81ea\u52a8\u6dfb\u52a0\u4e00\u4e2a IntegerField \u5b57\u6bb5\uff0c\u5e76\u8bbe\u7f6e\u4e3a\u4e3b\u952e\uff0c\u4e3b\u952e\u5b57\u6bb5\u662f\u53ea\u53ef\u8bfb\u7684\u3002<\/p>\n<p><span style=\"color:#0040FF\">unique<\/span>\uff1a\u662f\u5426\u552f\u4e00\uff0c\u9ed8\u8ba4False<\/p>\n<p><span style=\"color:#0040FF\">verbose_name<\/span>\uff1a\u5b57\u6bb5\u5907\u6ce8\u540d\uff0c\u4e3a\u8bbe\u7f6e\uff0cdjango\u4f1a\u5c06\u5b57\u6bb5\u7684\u4e0b\u5212\u7ebf\u8f6c\u6362\u4e3a\u7a7a\u683c\u505a\u4e3a\u5907\u6ce8<\/p>\n<p><strong class=\"text-strong\">2.\u5173\u8054\u5173\u7cfb<\/strong><br \/>\n<span style=\"color:#0000FF\">ForeignKey<\/span>\uff1a\u4e00\u5bf9\u591a<\/p>\n<p><span style=\"color:#0000FF\">ManyToManyField<\/span>\uff1a\u591a\u5bf9\u591a<\/p>\n<p><strong class=\"text-strong\">3.Meta\u9009\u9879<\/strong><br \/>\n\u6a21\u578b\u7684\u5143\u6570\u636e\u5373\u201c\u6240\u6709\u4e0d\u662f\u5b57\u6bb5\u7684\u4e1c\u897f\u201d\uff0c\u6bd4\u5982\u6392\u5e8f\u9009\u9879\uff08 ordering \uff09\uff0c\u6570\u636e\u5e93\u8868\u540d\uff08 db_table \uff09\uff0c\u6216\u662f\u9605\u8bfb\u53cb\u597d\u7684\u5355\u590d\u6570\u540d\uff08 verbose_name \u548c verbose_name_plural \uff09\u3002\u8fd9\u4e9b\u90fd\u4e0d\u662f\u5fc5\u987b\u7684\uff0c\u5e76\u4e14\u5728\u6a21\u578b\u5f53\u4e2d\u6dfb\u52a0 Meta\u7c7b \u4e5f\u5b8c\u5168\u662f\u53ef\u9009\u7684\u3002<\/p>\n<p><strong class=\"text-strong\">4.\u6a21\u578b\u65b9\u6cd5<\/strong><br \/>\n1.__str__()\uff1a\u8fd4\u56de\u53cb\u597d\u7684\u5bf9\u8c61\u6253\u5370\u503c<\/p>\n<p><strong class=\"text-strong\">5.\u6a21\u578b\u7ee7\u627f<\/strong><br \/>\n1.\u62bd\u8c61\u57fa\u7c7b<\/p>\n<p>\u62bd\u8c61\u57fa\u7c7b\u5728\u4f60\u8981\u5c06\u516c\u5171\u4fe1\u606f\u653e\u5165\u5f88\u591a\u6a21\u578b\u65f6\u4f1a\u5f88\u6709\u7528\u3002\u7f16\u5199\u4f60\u7684\u57fa\u7c7b\uff0c\u5e76\u5728 Meta \u7c7b\u4e2d\u586b\u5165 abstract=True\u3002\u8be5\u6a21\u578b\u5c06\u4e0d\u4f1a\u521b\u5efa\u4efb\u4f55\u6570\u636e\u8868\u3002\u5f53\u5176\u7528\u4f5c\u5176\u5b83\u6a21\u578b\u7c7b\u7684\u57fa\u7c7b\u65f6\uff0c\u5b83\u7684\u5b57\u6bb5\u4f1a\u81ea\u52a8\u6dfb\u52a0\u81f3\u5b50\u7c7b\u3002<\/span><\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">from<\/span> django.<span style=\"color: black;\">db<\/span> <span style=\"color: #ff7700;font-weight:bold;\">import<\/span> models<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">class<\/span> CommonInfo<span style=\"color: black;\">&#40;<\/span>models.<span style=\"color: black;\">Model<\/span><span style=\"color: black;\">&#41;<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; name <span style=\"color: #66cc66;\">=<\/span> models.<span style=\"color: black;\">CharField<\/span><span style=\"color: black;\">&#40;<\/span>max_length<span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff4500;\">100<\/span><span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; age <span style=\"color: #66cc66;\">=<\/span> models.<span style=\"color: black;\">PositiveIntegerField<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; <span style=\"color: #ff7700;font-weight:bold;\">class<\/span> Meta:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; &nbsp; &nbsp; abstract <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: #008000;\">True<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp;<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">class<\/span> Student<span style=\"color: black;\">&#40;<\/span>CommonInfo<span style=\"color: black;\">&#41;<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; home_group <span style=\"color: #66cc66;\">=<\/span> models.<span style=\"color: black;\">CharField<\/span><span style=\"color: black;\">&#40;<\/span>max_length<span style=\"color: #66cc66;\">=<\/span><span style=\"color: #ff4500;\">5<\/span><span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p>[\/size]<\/p>\n<p><small>\u539f\u6587\u4f5c\u8005: BG6RSH | <a href=\"https:\/\/bbs.tansoo.cn\/viewtopic.php?t=70\" target=\"_blank\" rel=\"noopener\">\u67e5\u770b\u539f\u5e16<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u5e38\u7528\u53c2\u6570\u9009\u9879 null\uff1a\u5982\u679c\u4e3a True\uff0c\u5f53\u8be5\u5b57\u6bb5\u4e3a\u7a7a\u65f6\uff0cDjango \u4f1a\u5c06 &hellip; <a href=\"https:\/\/www.tansoo.cn\/?p=1443\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-1443","post","type-post","status-publish","format-standard","hentry","category-105"],"_links":{"self":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1443"}],"version-history":[{"count":0,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1443\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}