{"id":85172,"date":"2020-03-29T08:46:24","date_gmt":"2020-03-29T00:46:24","guid":{"rendered":"http:\/\/4563.org\/?p=85172"},"modified":"2020-03-29T08:46:24","modified_gmt":"2020-03-29T00:46:24","slug":"fastapi-%e6%80%8e%e4%b9%88%e6%9b%b4%e5%a5%bd%e7%9a%84%e8%bf%94%e5%9b%9e%e8%87%aa%e5%ae%9a%e4%b9%89%e7%9a%84%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=85172","title":{"rendered":"FastAPI \u600e\u4e48\u66f4\u597d\u7684\u8fd4\u56de\u81ea\u5b9a\u4e49\u7684\u683c\u5f0f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  FastAPI \u600e\u4e48\u66f4\u597d\u7684\u8fd4\u56de\u81ea\u5b9a\u4e49\u7684\u683c\u5f0f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : holinhot <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p><strong>\u5b98\u65b9\u6587\u6863\u662f\u7528 response_model\uff0c\u4f46\u611f\u89c9 \u8fd9\u4e2a\u597d\u50cf\u4e0d\u662f\u5f88\u7075\u6d3b\u3002<\/strong><\/p>\n<p><strong>\u6211\u60f3\u8981\u8fd4\u56de\u7684\u7edf\u4e00\u683c\u5f0f, \u628a pydantic \u7684\u9a8c\u8bc1\u9519\u8bef\u4e5f\u5305\u8fdb\u81ea\u5df1\u5b9a\u4e49\u7684\u683c\u5f0f\u91cc<\/strong><\/p>\n<p>pydantic \u9ed8\u8ba4\u6570\u636e\u8fd4\u56de\u683c\u5f0f\u683c\u5f0f\uff1a<\/p>\n<pre><code>{   \"email\": \"[email&#160;protected]\",   \"id\": 6,   \"is_active\": true,   \"item\": [] } <\/code><\/pre>\n<p>pydantic \u9ed8\u8ba4\u9a8c\u8bc1\u9519\u8bef\u683c\u5f0f\uff1a<\/p>\n<pre><code>{   \"detail\": [     {       \"loc\": [         \"body\",         \"user\",         \"email\"       ],       \"msg\": \"value is not a valid email address\",       \"type\": \"value_error.email\"     }   ] } <\/code><\/pre>\n<p>\u8fd9\u79cd\u8fd4\u56de\u683c\u5f0f\u7f3a\u4e4f\u89c4\u8303\u6027\uff0c\u540e\u9762\u8c03\u7528\u63a5\u53e3\u65f6\u5c31\u975e\u5e38\u4e71\u4e86\u3002<\/p>\n<p><strong>\u6211\u60f3\u8981\u8fd4\u56de\u7684\u7edf\u4e00\u683c\u5f0f\u7edf\u4e00\u6210\u4e0b\u9762\u8fd9\u6837\uff0c\u65b9\u4fbf\u8c03\u7528\u548c\u5224\u65ad\u9519\u8bef<\/strong><\/p>\n<pre><code>{ \"success\": true, \"time\": \"1588633541\", \"errors\": \"\", \"result\": {   \"username\": \"bigbig\",   \"created_at\": \"2020-05-04 21:57:22.815393\" } }  { \"success\": false, \"time\": \"1588633541\", \"errors\": {   \"msg\": \"Authorization failed\",   \"errors\": {\"detail\": \"wrong user name or password\"} }, \"result\": \"\" }  { \"success\": false, \"time\": \"1588633541\", \"errors\": {\"detail\": [     {       \"loc\": [         \"body\",         \"user\",         \"email\"       ],       \"msg\": \"value is not a valid email address\",       \"type\": \"value_error.email\"     }   ]}, \"result\": \"\" } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1403505\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : holinhot <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9519\u8bef\u8fd4\u56de\u683c\u5f0f\u770b\u6587\u6863\u53ef\u4ee5\u6539\u4e86<\/p>\n<p>&#8220;`<br \/>@app.exception_handler(RequestValidationError)<br \/>async def validation_exception_handler(request: Request, exc: RequestValidationError):<br \/> return JSONResponse(<br \/> status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,<br \/> content=jsonable_encoder(<br \/> {<br \/> &#8220;success&#8221;: False,<br \/> &#8220;errors&#8221;: exc.errors(),<br \/> &#8220;result&#8221;: &#8220;&#8221;,<br \/> &#8220;body&#8221;: exc.body<br \/> }<br \/> )<br \/> )<br \/>&#8220;`<\/p>\n<p>&#8220;`<br \/>{<br \/> &#8220;success&#8221;: false,<br \/> &#8220;errors&#8221;: [<br \/> {<br \/> &#8220;loc&#8221;: [<br \/> &#8220;body&#8221;,<br \/> &#8220;user&#8221;,<br \/> &#8220;email&#8221;<br \/> ],<br \/> &#8220;msg&#8221;: &#8220;value is not a valid email address&#8221;,<br \/> &#8220;type&#8221;: &#8220;value_error.email&#8221;<br \/> }<br \/> ],<br \/> &#8220;result&#8221;: &#8220;&#8221;,<br \/> &#8220;body&#8221;: {<br \/> &#8220;email&#8221;: &#8220;user522example.com&#8221;,<br \/> &#8220;password&#8221;: &#8220;string&#8221;<br \/> }<br \/>}<br \/>&#8220;`<\/p>\n<p>response_model \u8fd8\u5728\u7814\u7a76\uff0c\u4e0d\u7136\u5c31\u53ea\u80fd\u4e0d\u7528 response_model\uff0c\u5b8c\u5168\u81ea\u5df1\u8fd4\u56de json \u5185\u5bb9\u4e86                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1403506\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : holinhot <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ed4\u7ec6\u7814\u7a76\u5df2\u641e\u5b9a<br \/>&#8220;`<br \/>class User(UserBase):<br \/> id: int<br \/> is_active: bool<br \/> item: List[Item] = []<\/p>\n<p> class Config:<br \/> orm_mode = True<\/p>\n<p>class Standard_result_format(BaseModel):<br \/> success: bool = True<br \/> errors: list = []<br \/> result: User<br \/>&#8220;`<\/p>\n<p>\u662f\u53ef\u4ee5\u5728\u4e0b\u9762\u8c03\u7528\u53e6\u4e00\u4e0b BaseModel class                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1403507\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : holinhot <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6539\u4e86\u9519\u8bef\u683c\u5f0f\u540e\u81ea\u52a8\u751f\u6210\u7684\u6587\u6863\u662f\u4e00\u4e2a\u5e94\u8be5\u662f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>FastAPI \u600e\u4e48\u66f4\u597d\u7684\u8fd4\u56de\u81ea\u5b9a&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/85172"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85172"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/85172\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85172"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}