{"id":204317,"date":"2025-05-29T13:05:18","date_gmt":"2025-05-29T05:05:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204317\/"},"modified":"2025-05-29T13:05:18","modified_gmt":"2025-05-29T05:05:18","slug":"%e4%ba%86%e8%a7%a3-flask-%e4%b8%ad%e7%9a%84-jsonify%e3%80%81to_dict%e3%80%81make_response-%e5%92%8c-serializermixin","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204317\/","title":{"rendered":"\u4e86\u89e3 Flask \u4e2d\u7684 JSONify()\u3001to_dict()\u3001make_response() \u548c SerializerMixin"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4e86\u89e3 Flask \u4e2d\u7684 JSONify()\u3001to_dict()\u3001make_response() \u548c SerializerMixin<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u4e86\u89e3 Flask \u4e2d\u7684 JSONify()\u3001to_dict()\u3001make_response() \u548c SerializerMixin\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241104\/173069697767285711d88b4.jpg\" class=\"aligncenter\"><\/p>\n<p>flask \u786e\u5b9e\u63d0\u4f9b\u4e86\u591a\u79cd\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u54cd\u5e94\u7684\u5de5\u5177\uff0c\u4ece\u5c06 python \u5bf9\u8c61\u8f6c\u6362\u4e3a json \u5230\u521b\u5efa\u7ed3\u6784\u5316 http \u54cd\u5e94\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8 jsonify()\u3001to_dict()\u3001make_response() \u548c serializermixin\uff0c\u5b83\u4eec\u662f\u5728 flask \u4e2d\u5904\u7406\u6570\u636e\u54cd\u5e94\u7684\u56db\u4e2a\u6709\u7528\u7684\u51fd\u6570\u548c\u5de5\u5177\u3002\u4e86\u89e3\u8fd9\u4e9b\u5de5\u5177\u5c06\u6709\u52a9\u4e8e\u521b\u5efa\u66f4\u597d\u7684 api \u548c\u6709\u6548\u7684\u6570\u636e\u7ba1\u7406\u3002<\/p>\n<p><strong>jsonify()<\/strong><br \/> \u5b83\u662f\u4e00\u4e2a\u5185\u7f6e\u7684 flask \u51fd\u6570\uff0c\u53ef\u5c06 python \u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3a json \u683c\u5f0f\uff0c\u8fd9\u662f\u4e00\u79cd\u5e7f\u6cdb\u7528\u4e8e api web \u5f00\u53d1\u7684\u8f7b\u91cf\u7ea7\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u3002\u8be5\u51fd\u6570\u81ea\u52a8\u5c06\u54cd\u5e94 content-type \u8bbe\u7f6e\u4e3a application\/json \u5e76\u8fd4\u56de flask \u54cd\u5e94\u5bf9\u8c61\uff0c\u975e\u5e38\u9002\u5408\u5728 rest api \u4e2d\u8fd4\u56de\u6570\u636e\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>from flask import jsonify\n\n@app.route('\/data')\ndef get_data():\n    data = {\"message\": \"hello, world!\", \"status\": \"success\"}\n    return jsonify(data)\n<\/pre>\n<p>\u8fd9\u91cc\uff0cjsonify(data) \u5c06\u5b57\u5178\u6570\u636e\u8f6c\u6362\u4e3a json \u683c\u5f0f\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u54cd\u5e94\u6b63\u6587\u3002\u5f53\u60a8\u9700\u8981\u8fd4\u56de\u5c0f\u578b\u4e14\u5b9a\u4e49\u826f\u597d\u7684\u6570\u636e\u65f6\uff0c\u6b64\u51fd\u6570\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4f1a\u4e3a\u60a8\u5904\u7406 json \u8f6c\u6362\u548c\u54cd\u5e94\u683c\u5f0f\u8bbe\u7f6e\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cjsonify() \u53ef\u4ee5\u5f88\u597d\u5730\u5904\u7406\u7b80\u5355\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u4e0d\u76f4\u63a5\u652f\u6301\u590d\u6742\u7684\u5bf9\u8c61\uff0c\u4f8b\u5982 sqlalchemy \u6a21\u578b\uff0c\u65e0\u9700\u8fdb\u884c\u4e00\u4e9b\u8f6c\u6362\uff08\u5982\u4f7f\u7528 to_dict()\uff09\u3002<\/p>\n<p><strong>to_dict()<\/strong><br \/> \u5b83\u4e0d\u662f\u539f\u751f flask \u51fd\u6570\uff0c\u4f46\u901a\u5e38\u5728\u6a21\u578b\u7c7b\u4e2d\u7528\u4e8e\u5c06 sqlalchemy \u6216\u5176\u4ed6\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04 (orm) \u6a21\u578b\u5b9e\u4f8b\u8868\u793a\u4e3a\u5b57\u5178\u3002\u5c06\u6a21\u578b\u5c5e\u6027\u8f6c\u6362\u4e3a\u5b57\u5178\u4f7f\u6570\u636e\u66f4\u5bb9\u6613\u8f6c\u6362\u4e3a api \u54cd\u5e94\u7684 json \u683c\u5f0f\u3002<br \/> \u793a\u4f8b\uff1a<\/p>\n<pre>class student(db.model):\n    id = db.column(db.integer, primary_key=true)\n    username = db.column(db.string(80), nullable=false)\n\n    def to_dict(self):\n        return {\n            \"id\": self.id,\n            \"username\": self.username\n        }\n\n@app.route('\/user\/&lt;int:id&gt;')\ndef get_student(id):\n    student = student.query.get(id)\n    return jsonify(student.to_dict()) if student else jsonify({\"error\": \"student not found\"}), 404\n<\/pre>\n<p>to_dict() \u65b9\u6cd5\u5141\u8bb8\u60a8\u6307\u5b9a\u8981\u5305\u542b\u5728\u54cd\u5e94\u4e2d\u7684\u786e\u5207\u6570\u636e\uff0c\u4ece\u800c\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\u3002\u5b83\u5bf9\u4e8e\u9690\u85cf\u654f\u611f\u6570\u636e\uff08\u5982\u5bc6\u7801\uff09\u548c\u6709\u9009\u62e9\u5730\u4ec5\u663e\u793a\u5fc5\u8981\u7684\u5c5e\u6027\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>make_response()<\/strong><br \/> \u5b83\u662f\u4e00\u4e2a flask \u5b9e\u7528\u51fd\u6570\uff0c\u5141\u8bb8\u60a8\u521b\u5efa\u81ea\u5b9a\u4e49 http \u54cd\u5e94\u3002\u867d\u7136 jsonify() \u7b80\u5316\u4e86 json \u6570\u636e\u54cd\u5e94\uff0c\u4f46 make_response() \u5141\u8bb8\u60a8\u63a7\u5236\u54cd\u5e94\u7684\u6bcf\u4e2a\u90e8\u5206\uff0c\u5305\u62ec\u72b6\u6001\u4ee3\u7801\u3001\u6807\u5934\u548c\u6570\u636e\u683c\u5f0f\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre>from flask import make_response, jsonify\nfrom models import db\n\nclass student(db.model):\n    id = db.column(db.integer, primary_key=true)\n    username = db.column(db.string(80), nullable=false)\n\n    def to_dict(self):\n        return {\n            \"id\": self.id,\n            \"username\": self.username\n        }\n\n@app.route('\/student\/&lt;int:id&gt;', methods=['get'])\ndef get_student(id):\n    # query the database for the student\n    student = student.query.get(id)\n\n    # if student is found, return data with a 200 status\n    if student:\n        response_data = {\n            \"message\": \"student found\",\n            \"data\": student.to_dict()\n        }\n        return make_response(jsonify(response_data), 200)\n\n    # if student is not found, return a structured error response with a 404 status\n    error_data = {\n        \"error\": \"student not found\",\n        \"student_id\": id,\n        \"status_code\": 404\n    }\n    return make_response(jsonify(error_data), 404)\n\n<\/pre>\n<p>\u8fd9\u91cc\uff0cmake_response() \u5141\u8bb8\u63a7\u5236\u72b6\u6001\u4ee3\u7801\u548c\u54cd\u5e94\u6b63\u6587\u683c\u5f0f\u3002\u5f53\u54cd\u5e94\u5bf9\u8c61\u7684\u63a7\u5236\u81f3\u5173\u91cd\u8981\u65f6\uff0c\u8fd9\u79cd\u7075\u6d3b\u6027\u662f\u7406\u60f3\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u5e8f\u5217\u5316\u5668mixin<\/strong><br \/> \u5b83\u6765\u81ea sqlalchemy-serializer \u5e93\uff0c\u662f\u7528\u4e8e\u81ea\u52a8\u5316 sqlalchemy \u6a21\u578b\u5e8f\u5217\u5316\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a to_dict() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5904\u7406\u5305\u62ec\u6a21\u578b\u4e4b\u95f4\u5173\u7cfb\u7684\u590d\u6742\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u5305\u542b\u4e00\u4e2a serialize_rules \u5c5e\u6027\u6765\u63a7\u5236\u5b57\u6bb5\u5e8f\u5217\u5316\u3002<\/p>\n<p>\u7528\u6cd5\uff1a<\/p>\n<pre>from sqlalchemy_serializer import SerializerMixin\n\nclass Product(db.Model, SerializerMixin):\n    __tablename__ = 'products'\n    serialize_rules = ('-category',)  # Exclude category from serialization\n\n    id = db.Column(db.Integer, primary_key=True)\n    name = db.Column(db.String(80), nullable=False)\n    category = db.Column(db.String(80))\n\n# Usage in a route\n@app.route('\/product\/&lt;int:id&gt;')\ndef get_product(id):\n    product = Product.query.get(id)\n    return jsonify(product.to_dict()) if product else jsonify({\"error\": \"Product not found\"}), 404\n\n<\/pre>\n<p>serializermixin \u81ea\u52a8\u5c06 sqlalchemy \u6a21\u578b\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u8fd9\u5728\u5904\u7406\u590d\u6742\u6a21\u578b\u548c\u5173\u7cfb\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f7f\u7528serialize_rules\uff0c\u60a8\u53ef\u4ee5\u52a8\u6001\u5305\u542b\u6216\u6392\u9664\u5b57\u6bb5\u6216\u5173\u7cfb\uff0c\u8fd9\u53ef\u4ee5\u8282\u7701\u60a8\u4e3a\u6bcf\u4e2a\u6a21\u578b\u7f16\u5199\u81ea\u5b9a\u4e49 to_dict \u65b9\u6cd5\u7684\u65f6\u95f4\u3002<\/p>\n<p><strong>\u6bd4\u8f83\u53ca\u5176\u5173\u8054<\/strong><br \/> \u8fd9\u4e9b\u5de5\u5177\u5728\u6784\u5efa flask api \u4e2d\u90fd\u6709\u5176\u81ea\u5df1\u7684\u4f4d\u7f6e\u3002 jsonify() \u548c make_response() \u662f\u521b\u5efa json \u548c\u81ea\u5b9a\u4e49\u54cd\u5e94\u7684\u57fa\u672c flask \u51fd\u6570\uff0c\u800c to_dict() \u548c serializermixin \u5219\u4e13\u6ce8\u4e8e\u5c06\u6a21\u578b\u5b9e\u4f8b\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u4ee5\u4fbf\u66f4\u8f7b\u677e\u5730\u8fdb\u884c json \u5e8f\u5217\u5316\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4f55\u65f6\u4f7f\u7528\u6bcf\u79cd\u65b9\u6cd5\u7684\u6458\u8981\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 jsonify() \u8f7b\u677e\u5c06\u7b80\u5355\u7684 python \u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3a json \u683c\u5f0f\u3002<\/li>\n<li>\u5728\u6a21\u578b\u4e0a\u4f7f\u7528 to_dict() \u521b\u5efa\u5177\u6709\u7279\u5b9a\u5b57\u6bb5\u7684\u81ea\u5b9a\u4e49\u5b57\u5178\u4ee5\u8fdb\u884c json \u8f6c\u6362\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u654f\u611f\u6216\u590d\u6742\u6570\u636e\u65f6\u3002<\/li>\n<li>\u4f7f\u7528 make_response() \u5b9a\u4e49\u5bf9 http \u54cd\u5e94\u7684\u5b8c\u5168\u63a7\u5236\uff0c\u5141\u8bb8\u60a8\u8bbe\u7f6e\u72b6\u6001\u4ee3\u7801\u3001\u6807\u5934\u6216\u81ea\u5b9a\u4e49\u9519\u8bef\u6d88\u606f\u3002<\/li>\n<li>\u5982\u679c\u60a8\u6b63\u5728\u4f7f\u7528 sqlalchemy \u6a21\u578b\u5e76\u5e0c\u671b\u4ee5\u6700\u5c11\u7684\u914d\u7f6e\u81ea\u52a8\u5c06\u6a21\u578b\uff08\u5305\u62ec\u5173\u7cfb\uff09\u8f6c\u6362\u4e3a json\uff0c\u8bf7\u4f7f\u7528 serializermixin\u3002<\/li>\n<\/ul>\n<p>\u603b\u4e4b\uff0cjsonify()\u3001to_dict()\u3001make_response() \u548c serializermixin \u90fd\u662f\u5728 flask api \u4e2d\u8f6c\u6362\u548c\u7ba1\u7406\u6570\u636e\u7684\u91cd\u8981\u5de5\u5177\u3002\u6709\u6548\u5730\u4f7f\u7528\u5b83\u4eec\u5c06\u4f7f\u60a8\u7684 api \u66f4\u52a0\u7075\u6d3b\u3001\u5b89\u5168\u4e14\u6613\u4e8e\u7ba1\u7406\u3002<\/p>\n<p><strong>\u53c2\u8003\u6587\u732e<\/strong><\/p>\n<ul>\n<li>\n<p>flask \u6587\u6863\uff1amake_response()<\/p>\n<\/li>\n<li>\n<p>sqlalchemy serializermixin<\/p>\n<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4e86\u89e3 Flask \u4e2d\u7684 JSONify()\u3001to_dict()\u3001make_response() \u548c SerializerMixin\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 Flask \u4e2d\u7684 JSON&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204317","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204317"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204317\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}