{"id":2970,"date":"2023-11-14T07:47:17","date_gmt":"2023-11-13T23:47:17","guid":{"rendered":"https:\/\/104.es\/?p=2970"},"modified":"2023-11-14T07:47:18","modified_gmt":"2023-11-13T23:47:18","slug":"django-export-json-file","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2023\/11\/14\/django-export-json-file\/","title":{"rendered":"\u8b93\u4f7f\u7528\u8005\u53ef\u4ee5\u5728Django\u7db2\u7ad9\u4e2d\u4e0b\u8f09\u8cc7\u6599\u8868\u5167\u5bb9"},"content":{"rendered":"\n<p>\u6709\u6642\u5019\u6211\u5011\u5728\u88fd\u4f5c\u7db2\u7ad9\u7684\u6642\u5019\uff0c\u6703\u6709\u4e00\u4e9b\u60c5\u6cc1\u662f\u8b93\u4f7f\u7528\u8005\u5728\u700f\u89bd\u60f3\u8981\u7684\u8cc7\u6599\u4e4b\u5f8c\uff0c\u4e5f\u53ef\u4ee5\u4e00\u4f75\u4e0b\u8f09\u9019\u4e9b\u8cc7\u6599\u6210\u70ba\u672c\u5730\u7aef\u7684\u6587\u5b57\u6a94\u6848\uff0c\u9019\u6642\u5019\u5c31\u9700\u8981\u8b93\u7db2\u7ad9\u5177\u5099\u4e0b\u8f09\u8cc7\u6599\u4e26\u5132\u5b58\u6210\u6a94\u6848\u7684\u529f\u80fd\uff0c\u6211\u5011\u9019\u7bc7\u6587\u7ae0\u5c31\u4f86\u7c21\u55ae\u5730\u8aaa\u660eDjango\u7db2\u7ad9\u63d0\u4f9b\u6b64\u7a2e\u529f\u80fd\u7684\u4e00\u4e9b\u6280\u5de7\u3002<\/p>\n\n\n\n<p>\u5047\u8a2d\u6211\u5011\u60f3\u8981\u628a\u67d0\u4e00\u500b\u8cc7\u6599\u8868\u4e2d\u7684\u6240\u6709\u8a18\u9304\u5168\u90e8\u8f38\u51fa\uff0c\u4e26\u8f49\u6210JSON\u683c\u5f0f\uff0c\u8b93\u700f\u89bd\u5668\u53ef\u4ee5\u5448\u73fe\u51fa\u9019\u4e9b\u8cc7\u6599\uff0c\u90a3\u9ebc\u6211\u5011\u53ef\u4ee5\u5728 views.py \u4e2d\u5efa\u7acb\u4e00\u500b\u53eb\u505a export \u7684\u51fd\u5f0f\uff0c\u5148\u53d6\u51fa\u6240\u6709\u7684\u8a18\u9304\uff0c\u7136\u5f8c\u628a\u9019\u4e9b\u8a18\u9304\u8f49\u8b8a\u6210\u7531\u5b57\u5178\u7d44\u6210\u7684\u4e32\u5217\uff0c\u518d\u900f\u904e json.dumps \u8f49\u63db\u6210 JSON \u683c\u5f0f\u7684\u5b57\u4e32\u8f38\u51fa\uff0c\u50b3\u7d71\u7684\u65b9\u6cd5\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">import json\nfrom mysite import models\ndef export(request):\r\n    jsondata = list()\r\n    items = models.Item.objects.all().order_by('item_date')\r\n    for item in items:\r\n        d = dict()\r\n        d['item_date'] = item.item_date.isoformat()\r\n        d['name'] = item.name\r\n        d['category'] = item.category.name\r\n        d['payment_type'] = item.payment_type.name\r\n        d['amount'] = item.amount\r\n        d['description'] = item.description\r\n        jsondata.append(d)\r\n    return JsonResponse(json.dumps(jsondata), safe=False)\r<\/code><\/pre>\n\n\n\n<p>\u6b64\u7a2e\u65b9\u5f0f\u96d6\u7136\u53ef\u4ee5\u628a\u8cc7\u6599\u9806\u5229\u8f49\u63db\u6210 JSON \u683c\u5f0f\u7684\u5b57\u4e32\uff0c\u4f46\u662f\u4f7f\u7528\u8005\u6703\u5f97\u5230\u5982\u4e0b\u6240\u793a\u7684\uff0c\u4e00\u5927\u5806\u5bc6\u5bc6\u9ebb\u9ebb\u7684\u539f\u59cb\u683c\u5f0f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"521\" src=\"https:\/\/104.es\/wp-content\/uploads\/2023\/11\/image-4-1024x521.png\" alt=\"\" class=\"wp-image-2972\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2023\/11\/image-4-1024x521.png 1024w, https:\/\/104.es\/wp-content\/uploads\/2023\/11\/image-4-300x153.png 300w, https:\/\/104.es\/wp-content\/uploads\/2023\/11\/image-4-768x391.png 768w, https:\/\/104.es\/wp-content\/uploads\/2023\/11\/image-4-600x306.png 600w, https:\/\/104.es\/wp-content\/uploads\/2023\/11\/image-4.png 1188w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u96d6\u7136\u4f7f\u7528\u8005\u53ef\u4ee5\u9078\u64c7\u4f7f\u7528\u5168\u9078\u529f\u80fd\u518d\u8907\u88fd\u5230\u6587\u5b57\u7de8\u8f2f\u5668\u5b58\u6a94\uff0c\u4f46\u662f\u7562\u7adf\u9019\u4e0d\u662f\u4e00\u500b\u597d\u7684\u8fa6\u6cd5\u3002\u56e0\u6b64\uff0c\u5728\u8f38\u51fa\u7684\u6642\u5019\uff0c\u6211\u5011\u5c31\u9700\u8981\u505a\u4e00\u4e9b\u7c21\u55ae\u7684\u8abf\u6574\uff0c\u6539\u70ba\u4f7f\u7528 HttpResponse\uff0c\u4f46\u662f\u52a0\u4e0a\u4e00\u4e9b\u53c3\u6578\u7684\u8a2d\u5b9a\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">def export(request):\r\n    jsondata = list()\r\n    items = models.Item.objects.all().order_by('item_date')\r\n    for item in items:\r\n        d = dict()\r\n        d['item_date'] = item.item_date.isoformat()\r\n        d['name'] = item.name\r\n        d['category'] = item.category.name\r\n        d['payment_type'] = item.payment_type.name\r\n        d['amount'] = item.amount\r\n        d['description'] = item.description\r\n        jsondata.append(d)\r\n    response = HttpResponse(json.dumps(jsondata), \n                            content_type='application\/force-download')\r\n    response['Content-Disposition'] = 'attachment; filename=export.json'\r\n    return response<\/code><\/pre>\n\n\n\n<p>\u4e5f\u5c31\u662f\u5148\u5229\u7528 HttpResponse \u6e96\u5099\u597d\u8981\u8f38\u51fa\u7684\u5167\u5bb9\uff0c\u4f46\u662f\u4e26\u4e0d\u76f4\u63a5 return\uff0c\u800c\u662f\u653e\u5728 response \u8b8a\u6578\u4e2d\uff0c\u7136\u5f8c\u518d\u628a response \u8b8a\u6578\u52a0\u4e0a\u4e00\u500b Content-Disposition \u7684\u5c6c\u6027\uff0c\u4e26\u8a2d\u5b9a\u5b83\u662f\u9644\u4ef6\uff0c\u800c\u4e14\u6307\u5b9a\u6a94\u540d\u70baexport.json\uff0c\u4e4b\u5f8c\u518d\u628a response \u56de\u50b3\u7d66\u700f\u89bd\u5668\uff0c\u5982\u6b64\uff0c\u7576\u57f7\u884c\u5230\u9019\u6bb5\u7a0b\u5f0f\u78bc\u7684\u6642\u5019\uff0c\u700f\u89bd\u5668\u5c31\u6703\u628a\u5f97\u5230\u7684 JSON \u683c\u5f0f\u5b57\u4e32\u6e96\u5099\u6210\u4e00\u500b\u53ef\u4ee5\u4e0b\u8f09\u7684\u6a94\u6848\uff0c\u4e26\u8a62\u554f\u4f7f\u7528\u8005\u8981\u4e0b\u8f09\u7684\u4f4d\u7f6e\u4ee5\u53ca\u662f\u5426\u4fee\u6539\u6a94\u540d\uff08\u9810\u8a2d\u6a94\u540d\u5c31\u662fexport.json\uff09\uff0c\u7136\u5f8c\u5c31\u53ef\u4ee5\u9806\u5229\u5b58\u6a94\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u6642\u5019\u6211\u5011\u5728\u88fd\u4f5c\u7db2\u7ad9\u7684\u6642\u5019\uff0c\u6703\u6709\u4e00\u4e9b\u60c5\u6cc1\u662f\u8b93\u4f7f\u7528\u8005\u5728\u700f\u89bd\u60f3\u8981\u7684\u8cc7\u6599\u4e4b\u5f8c\uff0c\u4e5f\u53ef\u4ee5\u4e00\u4f75\u4e0b\u8f09\u9019\u4e9b\u8cc7\u6599\u6210\u70ba\u672c\u5730\u7aef\u7684\u6587\u5b57\u6a94\u6848\uff0c\u9019\u6642\u5019\u5c31\u9700\u8981\u8b93\u7db2\u7ad9\u5177\u5099\u4e0b\u8f09\u8cc7\u6599\u4e26\u5132\u5b58\u6210\u6a94\u6848\u7684\u529f\u80fd\uff0c\u6211\u5011\u9019\u7bc7\u6587\u7ae0\u5c31\u4f86\u7c21\u55ae\u5730\u8aaa\u660eDjango\u7db2\u7ad9\u63d0\u4f9b\u6b64\u7a2e\u529f\u80fd\u7684\u4e00\u4e9b\u6280\u5de7\u3002<\/p>\n","protected":false},"author":1,"featured_media":2971,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[28,234,235,131],"class_list":["post-2970","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django","tag-django","tag-export","tag-file-download","tag-json"],"jetpack_featured_media_url":"https:\/\/104.es\/wp-content\/uploads\/2023\/11\/01.png","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=2970"}],"version-history":[{"count":1,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2970\/revisions"}],"predecessor-version":[{"id":2973,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/2970\/revisions\/2973"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media\/2971"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=2970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=2970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=2970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}