{"id":981,"date":"2024-12-13T09:01:45","date_gmt":"2024-12-13T01:01:45","guid":{"rendered":"http:\/\/www.chan.ink\/?p=981"},"modified":"2024-12-13T09:01:47","modified_gmt":"2024-12-13T01:01:47","slug":"%e3%80%90djangoorm%e3%80%91nulltrue-%e5%92%8c-blanktrue","status":"publish","type":"post","link":"http:\/\/www.chan.ink\/index.php\/2024\/12\/13\/%e3%80%90djangoorm%e3%80%91nulltrue-%e5%92%8c-blanktrue\/","title":{"rendered":"\u3010Django|ORM\u3011null=True \u548c blank=True"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 Django \u7684 ORM \u4e2d\uff0c<code>null=True<\/code> \u548c <code>blank=True<\/code> \u90fd\u662f\u7528\u4e8e\u5b57\u6bb5\u5b9a\u4e49\u65f6\u7684\u9009\u9879\uff0c\u5b83\u4eec\u5206\u522b\u63a7\u5236\u7740\u6a21\u578b\u5b57\u6bb5\u5728\u6570\u636e\u5e93\u548c\u8868\u5355\u4e2d\u7684\u884c\u4e3a\u3002\u5b83\u4eec\u7684\u5177\u4f53\u542b\u4e49\u548c\u533a\u522b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong><code>null=True<\/code><\/strong>\uff1a\u63a7\u5236\u6570\u636e\u5e93\u4e2d\u7684\u7a7a\u503c<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>\uff1a\u63a7\u5236\u6570\u636e\u5e93\u4e2d\u8be5\u5b57\u6bb5\u662f\u5426\u53ef\u4ee5\u5b58\u50a8 <code>NULL<\/code> \u503c\uff08\u5373\u6570\u636e\u5e93\u4e2d\u7684\u7a7a\u503c\uff09\u3002<\/li>\n\n\n\n<li><strong>\u4f5c\u7528<\/strong>\uff1a\u5982\u679c\u4f60\u5c06 <code>null=True<\/code> \u6dfb\u52a0\u5230\u67d0\u4e2a\u5b57\u6bb5\uff0c\u8fd9\u610f\u5473\u7740\u8be5\u5b57\u6bb5\u5728\u6570\u636e\u5e93\u4e2d\u53ef\u4ee5\u4fdd\u5b58 <code>NULL<\/code>\uff08\u7a7a\u503c\uff09\u3002\u5426\u5219\uff0cDjango \u4f1a\u5728\u6570\u636e\u5e93\u4e2d\u4e3a\u8be5\u5b57\u6bb5\u8bbe\u7f6e\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff0c\u901a\u5e38\u662f\u7a7a\u5b57\u7b26\u4e32\uff08\u5bf9\u4e8e <code>CharField<\/code> \u548c <code>TextField<\/code>\uff09\u6216 <code>0<\/code>\uff08\u5bf9\u4e8e <code>IntegerField<\/code> \u7b49\u6570\u5b57\u5b57\u6bb5\uff09\uff0c\u800c\u4e0d\u4f1a\u662f <code>NULL<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u9002\u7528\u8303\u56f4<\/strong>\uff1a\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5e93\u5c42\u9762\u7684\u63a7\u5236\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u6570\u5b57\u5b57\u6bb5\u548c\u5916\u952e\u5b57\u6bb5\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyModel(models.Model):\n    name = models.CharField(max_length=100, null=True)  # \u5141\u8bb8\u6570\u636e\u5e93\u4e2d\u4e3a NULL\n    age = models.IntegerField(null=True)  # \u5141\u8bb8\u6570\u636e\u5e93\u4e2d\u4e3a NULL\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>name<\/code> \u548c <code>age<\/code> \u5b57\u6bb5\u5728\u6570\u636e\u5e93\u4e2d\u53ef\u4ee5\u5b58\u50a8 <code>NULL<\/code> \u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>blank=True<\/code><\/strong>\uff1a\u63a7\u5236\u8868\u5355\u4e2d\u7684\u7a7a\u503c<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u9014<\/strong>\uff1a\u63a7\u5236\u5b57\u6bb5\u662f\u5426\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u8fd9\u4e2a\u9009\u9879\u4e3b\u8981\u5f71\u54cd Django \u8868\u5355\u9a8c\u8bc1\u3002<\/li>\n\n\n\n<li><strong>\u4f5c\u7528<\/strong>\uff1a\u5982\u679c\u4f60\u5c06 <code>blank=True<\/code> \u6dfb\u52a0\u5230\u67d0\u4e2a\u5b57\u6bb5\uff0c\u8868\u793a\u8be5\u5b57\u6bb5\u5728\u8868\u5355\u63d0\u4ea4\u65f6\u662f\u5141\u8bb8\u4e3a\u7a7a\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728 Django \u8868\u5355\u4e2d\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u4e0d\u586b\u5199\u8be5\u5b57\u6bb5\uff0c\u800c\u4e0d\u4f1a\u88ab\u89c6\u4e3a\u9a8c\u8bc1\u9519\u8bef\u3002\u4e0e <code>null=True<\/code> \u4e0d\u540c\uff0c<code>blank=True<\/code> \u4ec5\u5728\u8868\u5355\u9a8c\u8bc1\u548c\u6a21\u578b\u7684 <code>clean()<\/code> \u65b9\u6cd5\u4e2d\u8d77\u4f5c\u7528\uff0c\u4e0d\u5f71\u54cd\u6570\u636e\u5e93\u4e2d\u7684\u5b58\u50a8\u3002<\/li>\n\n\n\n<li><strong>\u9002\u7528\u8303\u56f4<\/strong>\uff1a\u4e3b\u8981\u7528\u4e8e\u8868\u5355\u9a8c\u8bc1\u6216\u6a21\u578b\u5b57\u6bb5\u7684\u9a8c\u8bc1\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyModel(models.Model):\n    name = models.CharField(max_length=100, blank=True)  # \u8868\u5355\u5141\u8bb8\u4e3a\u7a7a\n    age = models.IntegerField(blank=True)  # \u8868\u5355\u5141\u8bb8\u4e3a\u7a7a\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>name<\/code> \u548c <code>age<\/code> \u5b57\u6bb5\u5728 Django \u8868\u5355\u63d0\u4ea4\u65f6\u53ef\u4ee5\u4e0d\u586b\uff0c\u4f46\u4e0d\u4ee3\u8868\u5b83\u4eec\u5728\u6570\u636e\u5e93\u4e2d\u53ef\u4ee5\u5b58\u50a8 <code>NULL<\/code>\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u8bbe\u7f6e <code>null=True<\/code>\uff0c\u5b83\u4eec\u4f1a\u5b58\u50a8\u4e3a\u7a7a\u5b57\u7b26\u4e32\u6216\u96f6\uff08\u53d6\u51b3\u4e8e\u5b57\u6bb5\u7c7b\u578b\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4e24\u8005\u7ec4\u5408\uff1a<code>null=True<\/code> \u548c <code>blank=True<\/code> \u540c\u65f6\u4f7f\u7528<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u4f60\u540c\u65f6\u8bbe\u7f6e <code>null=True<\/code> \u548c <code>blank=True<\/code> \u65f6\uff0c\u5b57\u6bb5\u5728\u6570\u636e\u5e93\u4e2d\u53ef\u4ee5\u5b58\u50a8 <code>NULL<\/code> \u503c\uff0c\u5e76\u4e14\u5728\u8868\u5355\u63d0\u4ea4\u65f6\u8be5\u5b57\u6bb5\u53ef\u4ee5\u4e3a\u7a7a\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyModel(models.Model):\n    name = models.CharField(max_length=100, null=True, blank=True)  # \u5141\u8bb8\u6570\u636e\u5e93\u4e3a NULL\uff0c\u4e5f\u5141\u8bb8\u8868\u5355\u4e3a\u7a7a\n    age = models.IntegerField(null=True, blank=True)  # \u5141\u8bb8\u6570\u636e\u5e93\u4e3a NULL\uff0c\u8868\u5355\u5141\u8bb8\u4e3a\u7a7a\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<code>name<\/code> \u548c <code>age<\/code> \u5b57\u6bb5\u90fd\u53ef\u4ee5\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8 <code>NULL<\/code>\uff0c\u5e76\u4e14\u5728\u8868\u5355\u63d0\u4ea4\u65f6\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u4e0d\u586b\u5199\u8fd9\u4e9b\u5b57\u6bb5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u533a\u522b\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9009\u9879<\/th><th>\u5f71\u54cd\u8303\u56f4<\/th><th>\u89e3\u91ca<\/th><\/tr><\/thead><tbody><tr><td><code>null=True<\/code><\/td><td>\u6570\u636e\u5e93\u5c42<\/td><td>\u63a7\u5236\u6570\u636e\u5e93\u4e2d\u8be5\u5b57\u6bb5\u662f\u5426\u53ef\u4ee5\u5b58\u50a8 <code>NULL<\/code>\uff08\u7a7a\u503c\uff09\u3002<\/td><\/tr><tr><td><code>blank=True<\/code><\/td><td>\u8868\u5355\u9a8c\u8bc1\u5c42<\/td><td>\u63a7\u5236\u5b57\u6bb5\u662f\u5426\u53ef\u4ee5\u5728\u8868\u5355\u63d0\u4ea4\u65f6\u4e3a\u7a7a\uff0c\u4e0d\u4e3a\u7a7a\u65f6\u901a\u8fc7\u8868\u5355\u9a8c\u8bc1\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u5e38\u89c1\u7684\u7ec4\u5408\u65b9\u5f0f<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4ec5\u8bbe\u7f6e <code>null=True<\/code><\/strong>\uff1a\u901a\u5e38\u7528\u4e8e\u90a3\u4e9b\u5e0c\u671b\u5728\u6570\u636e\u5e93\u4e2d\u652f\u6301 <code>NULL<\/code> \u503c\uff0c\u4f46\u5728\u8868\u5355\u4e2d\u4e0d\u5141\u8bb8\u4e3a\u7a7a\u7684\u5b57\u6bb5\u3002\u4f8b\u5982\uff1a\u5916\u952e\u5b57\u6bb5\u3002 <code>class MyModel(models.Model): category = models.ForeignKey(Category, null=True, on_delete=models.SET_NULL) # \u6570\u636e\u5e93\u53ef\u4ee5\u4e3a\u7a7a<\/code><\/li>\n\n\n\n<li><strong>\u4ec5\u8bbe\u7f6e <code>blank=True<\/code><\/strong>\uff1a\u901a\u5e38\u7528\u4e8e\u90a3\u4e9b\u4e0d\u5e0c\u671b\u6570\u636e\u5e93\u5b58\u50a8 <code>NULL<\/code> \u503c\uff0c\u4f46\u5e0c\u671b\u5728\u8868\u5355\u4e2d\u5141\u8bb8\u4e3a\u7a7a\u7684\u5b57\u6bb5\u3002\u4f8b\u5982\uff1a<code>CharField<\/code> \u548c <code>TextField<\/code>\u3002 <code>class MyModel(models.Model): description = models.TextField(blank=True) # \u8868\u5355\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u4f46\u6570\u636e\u5e93\u4e2d\u4e0d\u80fd\u4e3a\u7a7a\uff08\u4f1a\u5b58\u50a8\u7a7a\u5b57\u7b26\u4e32\uff09<\/code><\/li>\n\n\n\n<li><strong>\u540c\u65f6\u8bbe\u7f6e <code>null=True<\/code> \u548c <code>blank=True<\/code><\/strong>\uff1a\u9002\u7528\u4e8e\u90a3\u4e9b\u5728\u6570\u636e\u5e93\u4e2d\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5e76\u4e14\u8868\u5355\u63d0\u4ea4\u65f6\u4e5f\u5141\u8bb8\u4e3a\u7a7a\u7684\u5b57\u6bb5\u3002 <code>class MyModel(models.Model): name = models.CharField(max_length=100, null=True, blank=True)<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u7279\u522b\u6ce8\u610f<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u4e8e <code>CharField<\/code> \u548c <code>TextField<\/code>\uff0c\u5982\u679c\u4f60\u8bbe\u7f6e <code>null=True<\/code>\uff0c\u901a\u5e38\u5efa\u8bae\u540c\u65f6\u8bbe\u7f6e <code>blank=True<\/code>\u3002\u56e0\u4e3a\u5728 Django \u4e2d\uff0c\u7a7a\u5b57\u7b26\u4e32 <code>\"\"<\/code> \u548c <code>NULL<\/code> \u662f\u6709\u533a\u522b\u7684\u3002\u901a\u5e38\uff0cDjango \u8ba4\u4e3a\u7a7a\u5b57\u7b26\u4e32\u8868\u793a&#8221;\u6ca1\u6709\u503c&#8221;\uff08\u800c\u975e\u672a\u77e5\u503c\uff09\uff0c\u800c <code>NULL<\/code> \u8868\u793a\u8be5\u5b57\u6bb5\u6ca1\u6709\u5b9a\u4e49\u4efb\u4f55\u503c\u3002 \u5728 <code>CharField<\/code> \u548c <code>TextField<\/code> \u5b57\u6bb5\u4e0a\uff0c\u901a\u5e38\u4e0d\u63a8\u8350\u4f7f\u7528 <code>null=True<\/code>\uff0c\u800c\u662f\u76f4\u63a5\u4f7f\u7528\u7a7a\u5b57\u7b26\u4e32\u8868\u793a\u7a7a\u503c\uff0c\u9664\u975e\u6709\u7279\u5b9a\u9700\u6c42\u3002 <code># \u63a8\u8350\u5199\u6cd5\uff1a class MyModel(models.Model): name = models.CharField(max_length=100, blank=True) # \u4f7f\u7528\u7a7a\u5b57\u7b26\u4e32\u8868\u793a\u7a7a\u503c<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>null=True<\/code> \u5f71\u54cd\u6570\u636e\u5e93\u5c42\u9762\uff0c\u63a7\u5236\u5b57\u6bb5\u662f\u5426\u53ef\u4ee5\u5b58\u50a8 <code>NULL<\/code>\u3002<\/li>\n\n\n\n<li><code>blank=True<\/code> \u5f71\u54cd\u8868\u5355\u5c42\u9762\uff0c\u63a7\u5236\u5b57\u6bb5\u662f\u5426\u5728\u8868\u5355\u4e2d\u53ef\u4ee5\u4e3a\u7a7a\u3002<\/li>\n\n\n\n<li>\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u9009\u9879\uff0c\u63a7\u5236\u6570\u636e\u5e93\u548c\u8868\u5355\u7684\u884c\u4e3a\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Django \u7684 ORM \u4e2d\uff0cnull=True \u548c blank=True \u90fd\u662f\u7528\u4e8e\u5b57\u6bb5\u5b9a\u4e49\u65f6\u7684\u9009\u9879\uff0c\u5b83\u4eec [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-981","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/comments?post=981"}],"version-history":[{"count":1,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/981\/revisions"}],"predecessor-version":[{"id":982,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/981\/revisions\/982"}],"wp:attachment":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/media?parent=981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/categories?post=981"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/tags?post=981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}