{"id":967,"date":"2024-12-12T19:13:32","date_gmt":"2024-12-12T11:13:32","guid":{"rendered":"http:\/\/www.chan.ink\/?p=967"},"modified":"2024-12-12T19:13:35","modified_gmt":"2024-12-12T11:13:35","slug":"%e3%80%90django%e3%80%91modelform%e4%b8%addata%e5%8f%82%e6%95%b0%e4%b8%8einstance%e5%8f%82%e6%95%b0%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"http:\/\/www.chan.ink\/index.php\/2024\/12\/12\/%e3%80%90django%e3%80%91modelform%e4%b8%addata%e5%8f%82%e6%95%b0%e4%b8%8einstance%e5%8f%82%e6%95%b0%e7%9a%84%e5%8c%ba%e5%88%ab\/","title":{"rendered":"\u3010Django\u3011ModelForm\u4e2ddata\u53c2\u6570\u4e0einstance\u53c2\u6570\u7684\u533a\u522b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5355\u72ec\u4f7f\u7528instance\uff0c\u5e38\u7528\u4e8e\u5c55\u793a\u6570\u636e\u5e93\u5df2\u6709\u7684\u6570\u636e\u3002\u6216\u8005\u4fee\u6539\u6570\u636e\u7684\u524d\u7f6e\u5c55\u793a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7efc\u5408\u4f7f\u7528instance\u548cdata\uff0c\u5e38\u7528\u4e8e\u7f16\u8f91\u8868\u5355\u6570\u636e<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Django \u7684 <code>ModelForm<\/code> \u8868\u5355\u4e2d\uff0c<code>data<\/code> \u548c <code>instance<\/code> \u90fd\u7528\u4e8e\u4f20\u9012\u8868\u5355\u7684\u521d\u59cb\u6570\u636e\uff0c\u4f46\u5b83\u4eec\u6709\u4e0d\u540c\u7684\u7528\u9014\u548c\u884c\u4e3a\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong><code>data<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>data<\/code> \u662f\u4f20\u9012\u7ed9\u8868\u5355\u7684\u5b57\u5178\u6570\u636e\uff0c\u901a\u5e38\u7528\u4e8e\u521d\u59cb\u5316\u8868\u5355\u5b57\u6bb5\u7684\u503c\u3002\u8fd9\u4e2a\u5b57\u5178\u7684\u952e\u662f\u6a21\u578b\u5b57\u6bb5\u7684\u540d\u79f0\uff0c\u503c\u662f\u76f8\u5e94\u7684\u5b57\u6bb5\u503c\u3002<code>data<\/code> \u5728\u8868\u5355\u63d0\u4ea4\u65f6\uff0c\u8868\u793a\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u6cd5<\/strong>\uff1a<code>data<\/code> \u4e3b\u8981\u5728\u8868\u5355\u7684<strong>\u521d\u59cb\u5316<\/strong>\u65f6\u4f7f\u7528\uff0c\u5c24\u5176\u662f\u5f53\u4f60\u624b\u52a8\u4e3a\u8868\u5355\u63d0\u4f9b\u6570\u636e\u65f6\u3002\u5b83\u4e0d\u4f1a\u4e0e\u6570\u636e\u5e93\u4e2d\u7684\u73b0\u6709\u6a21\u578b\u5b9e\u4f8b\u8fdb\u884c\u5173\u8054\u3002<\/li>\n\n\n\n<li><strong>\u4f55\u65f6\u4f7f\u7528<\/strong>\uff1a\u4f60\u4f20\u9012 <code>data<\/code> \u53c2\u6570\u65f6\uff0c\u901a\u5e38\u662f\u4e3a\u4e86\u521b\u5efa\u4e00\u4e2a\u7a7a\u8868\u5355\u6216\u5728\u6ca1\u6709\u6a21\u578b\u5b9e\u4f8b\u7684\u60c5\u51b5\u4e0b\u521d\u59cb\u5316\u8868\u5355\u7684\u6570\u636e\uff08\u6bd4\u5982\u53ea\u4f20\u9012\u539f\u59cb\u7684\u5b57\u5178\u6570\u636e\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6ce8\u610f<\/strong>\uff1a<code>data<\/code> \u4e0d\u4f1a\u81ea\u52a8\u66f4\u65b0\u6570\u636e\u5e93\uff0c\u5b83\u53ea\u662f\u63d0\u4f9b\u8868\u5355\u5b57\u6bb5\u7684\u521d\u59cb\u503c\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>from django import forms\n\nclass MyModelForm(forms.ModelForm):\n    class Meta:\n        model = MyModel\n        fields = &#91;'field1', 'field2']\n\n# \u63d0\u4f9b\u5b57\u5178\u6570\u636e\ndata = {'field1': 'value1', 'field2': 'value2'}\nform = MyModelForm(data=data)  # \u4f20\u9012\u5b57\u5178\u6570\u636e\u521d\u59cb\u5316\u8868\u5355\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>instance<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>instance<\/code> \u662f\u4f20\u9012\u7ed9\u8868\u5355\u7684\u6a21\u578b\u5b9e\u4f8b\uff0c\u5b83\u7528\u4e8e\u5c06\u6570\u636e\u5e93\u4e2d\u7684\u73b0\u6709\u6570\u636e\u52a0\u8f7d\u5230\u8868\u5355\u4e2d\u3002\u5728\u4f60\u7f16\u8f91\u73b0\u6709\u5bf9\u8c61\u65f6\uff08\u5982\u4fee\u6539\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u6570\u636e\u5e93\u8bb0\u5f55\uff09\uff0c\u4f60\u5c06 <code>instance<\/code> \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u8868\u5355\uff0c\u8fd9\u6837\u8868\u5355\u4f1a\u81ea\u52a8\u4f7f\u7528\u8be5\u5b9e\u4f8b\u7684\u5b57\u6bb5\u503c\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7528\u6cd5<\/strong>\uff1a<code>instance<\/code> \u7528\u4e8e\u4ece\u6570\u636e\u5e93\u4e2d\u52a0\u8f7d\u4e00\u4e2a\u6a21\u578b\u5b9e\u4f8b\uff0c\u8868\u5355\u4f1a\u81ea\u52a8\u6839\u636e\u8be5\u5b9e\u4f8b\u7684\u5b57\u6bb5\u503c\u6765\u586b\u5145\u5b57\u6bb5\u3002<\/li>\n\n\n\n<li><strong>\u4f55\u65f6\u4f7f\u7528<\/strong>\uff1a\u5982\u679c\u4f60\u9700\u8981\u7f16\u8f91\u73b0\u6709\u7684\u6570\u636e\u5e93\u5bf9\u8c61\uff0c\u5219\u9700\u8981\u5c06 <code>instance<\/code> \u53c2\u6570\u4f20\u9012\u7ed9\u8868\u5355\u3002<\/li>\n\n\n\n<li><strong>\u6ce8\u610f<\/strong>\uff1a\u4f7f\u7528 <code>instance<\/code> \u4f1a\u5c06\u8868\u5355\u4e0e\u6570\u636e\u5e93\u4e2d\u7684\u6a21\u578b\u5bf9\u8c61\u5173\u8054\uff0c\u5728\u63d0\u4ea4\u8868\u5355\u65f6\uff0c\u6570\u636e\u4f1a\u66f4\u65b0\u5230\u8be5\u6a21\u578b\u5b9e\u4f8b\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>row_object = MyModel.objects.get(id=1)\n\n# \u4f7f\u7528 instance \u4f20\u9012\u73b0\u6709\u7684\u6a21\u578b\u5b9e\u4f8b\nform = MyModelForm(instance=row_object)  # \u5c06\u6a21\u578b\u5b9e\u4f8b\u4f20\u9012\u7ed9\u8868\u5355\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong><code>data<\/code> vs <code>instance<\/code> \u7684\u533a\u522b<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>data<\/code><\/strong>\uff1a\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u4f20\u9012\u7684\u662f\u5b57\u6bb5\u7684\u521d\u59cb\u503c\u3002\u9002\u7528\u4e8e\u4f60\u4e0d\u9700\u8981\u4fee\u6539\u6570\u636e\u5e93\u4e2d\u7684\u5bf9\u8c61\uff0c\u53ea\u662f\u60f3\u663e\u793a\u6216\u64cd\u4f5c\u4e00\u4e2a\u8868\u5355\u6570\u636e\uff08\u6bd4\u5982\u67e5\u8be2\u3001\u8fc7\u6ee4\u540e\u7684\u6570\u636e\uff09\u3002\n<ul class=\"wp-block-list\">\n<li>\u9002\u7528\u4e8e\u521b\u5efa\u65b0\u5bf9\u8c61\u6216\u4e0d\u4e0e\u6570\u636e\u5e93\u5bf9\u8c61\u76f4\u63a5\u7ed1\u5b9a\u7684\u573a\u666f\u3002<\/li>\n\n\n\n<li><code>data<\/code> \u4e0d\u4f1a\u81ea\u52a8\u4fdd\u5b58\u5230\u6570\u636e\u5e93\uff0c\u9700\u8981\u624b\u52a8\u8c03\u7528 <code>save()<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>instance<\/code><\/strong>\uff1a\u662f\u4e00\u4e2a\u6a21\u578b\u5b9e\u4f8b\uff0c\u8868\u793a\u4e00\u4e2a\u6570\u636e\u5e93\u4e2d\u7684\u5bf9\u8c61\u3002\u9002\u7528\u4e8e\u4f60\u60f3\u4fee\u6539\u6216\u7f16\u8f91\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u6570\u636e\u5e93\u5bf9\u8c61\uff0c\u5e76\u5c06\u8868\u5355\u4e2d\u7684\u6570\u636e\u4fdd\u5b58\u56de\u6570\u636e\u5e93\u3002\n<ul class=\"wp-block-list\">\n<li>\u9002\u7528\u4e8e\u7f16\u8f91\u5df2\u7ecf\u5b58\u5728\u7684\u6a21\u578b\u5bf9\u8c61\uff0c\u5e76\u4e14\u5f53\u8868\u5355\u63d0\u4ea4\u65f6\uff0c\u5b83\u4f1a\u66f4\u65b0 <code>instance<\/code> \u4e2d\u7684\u5185\u5bb9\u3002<\/li>\n\n\n\n<li>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>instance<\/code> \u6765\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c<code>instance<\/code> \u4f1a\u81ea\u52a8\u4e0e\u6570\u636e\u5e93\u5bf9\u8c61\u5173\u8054\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u7ed3\u5408\u4f7f\u7528 <code>data<\/code> \u548c <code>instance<\/code><\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u4f1a\u5728\u4e00\u4e2a\u8868\u5355\u4e2d\u540c\u65f6\u4f7f\u7528 <code>data<\/code> \u548c <code>instance<\/code>\uff0c\u4f8b\u5982\uff1a\u4f60\u60f3\u4ece\u6570\u636e\u5e93\u4e2d\u52a0\u8f7d\u67d0\u4e2a\u5bf9\u8c61\u7684\u73b0\u6709\u6570\u636e\u5e76\u4fee\u6539\u5b83\uff0c\u4f46\u4f60\u8fd8\u60f3\u8986\u76d6\u67d0\u4e9b\u5b57\u6bb5\u7684\u521d\u59cb\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>row_object = MyModel.objects.get(id=1)\n\n# \u4f7f\u7528 instance \u4f20\u9012\u6a21\u578b\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7 data \u8986\u76d6\u67d0\u4e9b\u5b57\u6bb5\ndata = {'field1': 'new_value1'}\nform = MyModelForm(data=data, instance=row_object)  # \u4fee\u6539 `field1` \u7684\u503c\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<code>instance<\/code> \u63d0\u4f9b\u73b0\u6709\u6570\u636e\uff0c<code>data<\/code> \u7528\u6765\u8986\u76d6\u67d0\u4e9b\u5b57\u6bb5\u7684\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong><code>save()<\/code> \u65b9\u6cd5\u7684\u884c\u4e3a<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>instance<\/code><\/strong>\uff1a<ul><li>\u5982\u679c\u8868\u5355\u63d0\u4ea4\u5e76\u4e14\u6210\u529f\u9a8c\u8bc1\uff0c<code>save()<\/code> \u65b9\u6cd5\u4f1a\u81ea\u52a8\u66f4\u65b0 <code>instance<\/code> \u5bf9\u8c61\u5e76\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u3002<\/li><\/ul><code>form.save() # \u66f4\u65b0\u6570\u636e\u5e93\u4e2d\u7684\u5bf9\u8c61<\/code><\/li>\n\n\n\n<li><strong>\u4f7f\u7528 <code>data<\/code><\/strong>\uff1a<ul><li>\u5f53\u4f60\u4f7f\u7528 <code>data<\/code> \u65f6\uff0c<code>save()<\/code> \u65b9\u6cd5\u4e0d\u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u6a21\u578b\u5b9e\u4f8b\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6a21\u578b\u5b9e\u4f8b\uff0c\u6216\u8005\u66f4\u65b0\u4e00\u4e2a\u73b0\u6709\u5b9e\u4f8b\u3002<\/li><\/ul><code>form.save() # \u4f1a\u8fd4\u56de\u65b0\u7684\u5bf9\u8c61\u6216\u66f4\u65b0\uff0c\u4f46\u8981\u786e\u4fdd\u624b\u52a8\u5904\u7406\u6570\u636e\u5e93\u4fdd\u5b58<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>data<\/code> \u7528\u4e8e\u4f20\u9012\u521d\u59cb\u6570\u636e\uff08\u901a\u5e38\u662f\u5b57\u5178\u5f62\u5f0f\uff09\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u65b0\u5bf9\u8c61\u6216\u66f4\u65b0\u8868\u5355\u6570\u636e\uff0c\u4e0d\u76f4\u63a5\u4e0e\u6570\u636e\u5e93\u5173\u8054\u3002<\/li>\n\n\n\n<li><code>instance<\/code> \u7528\u4e8e\u4f20\u9012\u73b0\u6709\u7684\u6a21\u578b\u5b9e\u4f8b\uff0c\u9002\u7528\u4e8e\u7f16\u8f91\u73b0\u6709\u5bf9\u8c61\uff0c\u5e76\u5c06\u66f4\u6539\u4fdd\u5b58\u56de\u6570\u636e\u5e93\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4f60\u7f16\u8f91\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u6570\u636e\u5e93\u5bf9\u8c61\u65f6\uff0c\u5e94\u8be5\u4f7f\u7528 <code>instance<\/code>\uff0c\u800c <code>data<\/code> \u7528\u4e8e\u521d\u59cb\u5316\u8868\u5355\u6570\u636e\u3002\u5982\u679c\u4f60\u9700\u8981\u5c06\u8fd9\u4e24\u4e2a\u529f\u80fd\u7ed3\u5408\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u540c\u65f6\u4f20\u9012\u8fd9\u4e24\u4e2a\u53c2\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>ModelForm<\/code> \u8868\u5355\u4e2d\uff0c\u9664\u4e86 <code>data<\/code> \u548c <code>instance<\/code>\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u53c2\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>initial<\/code>: \u7528\u4e8e\u4e3a\u8868\u5355\u5b57\u6bb5\u63d0\u4f9b\u521d\u59cb\u503c\uff08\u4e0d\u4f1a\u4e0e\u6570\u636e\u5e93\u6a21\u578b\u5b9e\u4f8b\u6570\u636e\u8986\u76d6\uff09\u3002<\/li>\n\n\n\n<li><code>prefix<\/code>: \u7528\u4e8e\u4e3a\u5b57\u6bb5\u540d\u79f0\u6dfb\u52a0\u524d\u7f00\uff0c\u907f\u514d\u591a\u4e2a\u8868\u5355\u5b57\u6bb5\u540d\u79f0\u51b2\u7a81\u3002<\/li>\n\n\n\n<li><code>files<\/code>: \u7528\u4e8e\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\uff0c\u901a\u5e38\u4e0e <code>request.FILES<\/code> \u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n\n\n\n<li><code>auto_id<\/code>: \u7528\u4e8e\u63a7\u5236\u5b57\u6bb5\u7684 HTML <code>id<\/code> \u5c5e\u6027\u751f\u6210\u65b9\u5f0f\u3002<\/li>\n\n\n\n<li><code>empty_permitted<\/code>: \u63a7\u5236\u8868\u5355\u5b57\u6bb5\u662f\u5426\u5141\u8bb8\u4e3a\u7a7a\u3002<\/li>\n\n\n\n<li><code>error_messages<\/code>: \u7528\u4e8e\u81ea\u5b9a\u4e49\u5b57\u6bb5\u7684\u9519\u8bef\u6d88\u606f\u3002<\/li>\n\n\n\n<li><code>validators<\/code>: \u7528\u4e8e\u4e3a\u5b57\u6bb5\u63d0\u4f9b\u989d\u5916\u7684\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u903b\u8f91\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u72ec\u4f7f\u7528instance\uff0c\u5e38\u7528\u4e8e\u5c55\u793a\u6570\u636e\u5e93\u5df2\u6709\u7684\u6570\u636e\u3002\u6216\u8005\u4fee\u6539\u6570\u636e\u7684\u524d\u7f6e\u5c55\u793a \u7efc\u5408\u4f7f\u7528instance\u548cdat [&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-967","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/967","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=967"}],"version-history":[{"count":1,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/967\/revisions"}],"predecessor-version":[{"id":968,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/967\/revisions\/968"}],"wp:attachment":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/media?parent=967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/categories?post=967"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/tags?post=967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}