{"id":205449,"date":"2025-05-29T10:31:18","date_gmt":"2025-05-29T02:31:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205449\/"},"modified":"2025-05-29T10:31:18","modified_gmt":"2025-05-29T02:31:18","slug":"flask-sqlalchemy-orm-%e5%ba%8f%e5%88%97%e5%8c%96%e9%97%ae%e9%a2%98%ef%bc%9a%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3unexpect-system-error-object-of-type-user-is-not-json-serializable","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205449\/","title":{"rendered":"Flask-SQLAlchemy ORM \u5e8f\u5217\u5316\u95ee\u9898\uff1a\u5982\u4f55\u89e3\u51b3\u201cUnexpect System Error &#8211; Object of type User is not JSON serializable\u201d\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Flask-SQLAlchemy ORM \u5e8f\u5217\u5316\u95ee\u9898\uff1a\u5982\u4f55\u89e3\u51b3\u201cUnexpect System Error &#8211; Object of type User is not JSON serializable\u201d\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aFlask-SQLAlchemy ORM \u5e8f\u5217\u5316\u95ee\u9898\uff1a\u5982\u4f55\u89e3\u51b3\u201cUnexpect System Error &#8211; Object of type User is not JSON serializable\u201d\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241202\/1733120876674d536cefeab.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>flask-sqlalchemy orm \u4f7f\u7528\u95ee\u9898<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 flask-sqlalchemy orm \u65f6\u9047\u5230\u95ee\u9898\uff1f\u8ba9\u6211\u4eec\u4e00\u8d77\u89e3\u51b3\u5b83\uff01<\/p>\n<p>\u95ee\u9898\uff1a\u4f7f\u7528 flask-sqlalchemy \u67e5\u8be2\u6570\u636e\u5e93\u65f6\u51fa\u73b0\u4ee5\u4e0b\u9519\u8bef\uff1a<\/p>\n<pre>unexpect system error - object of type user is not json serializable<\/pre>\n<p>\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\u4ee5\u53ca\u5982\u4f55\u89e3\u51b3\u5462\uff1f<\/p>\n<p>\u7b54\u6848\uff1a<\/p>\n<p>\u5f53\u901a\u8fc7 flask \u5e8f\u5217\u5316\u5bf9\u8c61\u65f6\u4f1a\u51fa\u73b0\u6b64\u9519\u8bef\uff0c\u56e0\u4e3a flask \u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e0d\u652f\u6301 sqlalchemy \u5bf9\u8c61\u5e8f\u5217\u5316\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u5728 user \u7c7b\u4e2d\u7f16\u5199\u4e00\u4e2a\u65b9\u6cd5\u6765\u5904\u7406\u5e8f\u5217\u5316\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>class user(base):\n    # ...\n    def to_dict(self):\n        return {\n            \"id\": self.id,\n            \"app_key\": self.app_key,\n            \"secret\": self.secret,\n        }<\/pre>\n<p>\u7136\u540e\u5728\u89c6\u56fe\u51fd\u6570\u4e2d\u4f7f\u7528 to_dict \u65b9\u6cd5\uff1a<\/p>\n<pre>from flask import jsonify\n\n@app.route(\"\/users\")\ndef get_users():\n    users = User.query.all()\n    return jsonify([user.to_dict() for user in users])<\/pre>\n<p>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u5b98\u65b9\u6587\u6863\u4ee5\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\uff1a<\/p>\n<ul>\n<li>flask \u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/flask.palletsprojects.com\/en\/2.1.x\/<\/li>\n<li>flask-sqlalchemy \u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/flask-sqlalchemy.palletsprojects.com\/en\/2.x\/<\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aFlask-SQLAlchemy ORM \u5e8f\u5217\u5316\u95ee\u9898\uff1a\u5982\u4f55\u89e3\u51b3\u201cUnexpect System Error &#8211; Object of type User is not JSON serializable\u201d\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flask-SQLAlchemy&#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-205449","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205449","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=205449"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205449\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}