{"id":145,"date":"2026-06-16T15:20:13","date_gmt":"2026-06-16T07:20:13","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=145"},"modified":"2026-06-16T15:20:13","modified_gmt":"2026-06-16T07:20:13","slug":"fastapi-%e6%9f%a5%e8%af%a2%e5%8f%82%e6%95%b0%e6%a0%a1%e9%aa%8c","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/06\/16\/fastapi-%e6%9f%a5%e8%af%a2%e5%8f%82%e6%95%b0%e6%a0%a1%e9%aa%8c\/","title":{"rendered":"FastAPI \u67e5\u8be2\u53c2\u6570\u6821\u9a8c"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 FastAPI \u4e2d\uff0c\u67e5\u8be2\u53c2\u6570\uff08Query Parameters\uff09\u7684\u6821\u9a8c\u4e3b\u8981\u4f9d\u8d56\u4e8e <strong>Pydantic<\/strong> \u548c FastAPI \u63d0\u4f9b\u7684 <strong><code>Query<\/code><\/strong> \u51fd\u6570\u3002FastAPI \u4f1a\u81ea\u52a8\u89e3\u6790 URL \u4e2d\u7684\u67e5\u8be2\u53c2\u6570\uff0c\u5e76\u6839\u636e\u4f60\u63d0\u4f9b\u7684\u7c7b\u578b\u63d0\u793a\u548c\u6821\u9a8c\u89c4\u5219\u8fdb\u884c\u9a8c\u8bc1\u3002\u5982\u679c\u9a8c\u8bc1\u5931\u8d25\uff0c\u4f1a\u81ea\u52a8\u8fd4\u56de\u6e05\u6670\u7684 HTTP 422 (Unprocessable Entity) \u9519\u8bef\u54cd\u5e94\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u662f FastAPI \u67e5\u8be2\u53c2\u6570\u6821\u9a8c\u7684\u5168\u9762\u6307\u5357\uff0c\u5305\u542b\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u7684\u7528\u6cd5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u73b0\u4ee3\u63a8\u8350\u5199\u6cd5\uff1a\u4f7f\u7528 <code>Annotated<\/code> (FastAPI 0.95.0+ \/ Pydantic V2)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b98\u65b9\u76ee\u524d\u6700\u63a8\u8350\u4f7f\u7528 Python \u7684 <code>Annotated<\/code>\uff0c\u56e0\u4e3a\u5b83\u80fd\u5c06<strong>\u7c7b\u578b\u63d0\u793a<\/strong>\u548c<strong>\u5143\u6570\u636e\uff08\u6821\u9a8c\u89c4\u5219\uff09<\/strong> \u6e05\u6670\u5730\u5206\u79bb\u5f00\u6765\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from fastapi import FastAPI, Query\nfrom typing import Annotated\n\napp = FastAPI()\n\n@app.get(&quot;\/items\/&quot;)\nasync def read_items(\n    # \u7c7b\u578b\u662f str | None\uff0cQuery \u63d0\u4f9b\u6821\u9a8c\u89c4\u5219\uff0c\u9ed8\u8ba4\u503c\u4e3a None\n    q: Annotated[str | None, Query(min_length=3, max_length=50, description=&quot;\u641c\u7d22\u5173\u952e\u8bcd&quot;)] = None,\n    skip: Annotated[int, Query(ge=0, le=100, description=&quot;\u8df3\u8fc7\u7684\u8bb0\u5f55\u6570&quot;)] = 0,\n):\n    return {&quot;q&quot;: q, &quot;skip&quot;: skip}<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5e38\u7528\u6821\u9a8c\u89c4\u5219\u8be6\u89e3<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 \u5fc5\u586b\u4e0e\u53ef\u9009<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53ef\u9009<\/strong>\uff1a\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff08\u5982 <code>= None<\/code> \u6216 <code>= \"default\"<\/code>\uff09\u3002<br>PV<\/li>\n\n\n\n<li><strong>\u5fc5\u586b<\/strong>\uff1a\u4e0d\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u6216\u8005\u4f7f\u7528 <code>...<\/code> (Ellipsis)\u3002<\/li>\n<\/ul>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from fastapi import FastAPI, Query\n\napp = FastAPI()\n\n@app.get(&quot;\/items\/&quot;)\nasync def read_items(\n    # \u5fc5\u586b\u53c2\u6570\uff0c\u4e14\u957f\u5ea6\u81f3\u5c11\u4e3a 3\n    required_param: str = Query(..., min_length=3),\n\n    # \u53ef\u9009\u53c2\u6570\uff0c\u9ed8\u8ba4\u503c\u4e3a &quot;hello&quot;\n    optional_param: str = Query(&quot;hello&quot;, max_length=10)\n):\n    return {&quot;required&quot;: required_param, &quot;optional&quot;: optional_param}<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 \u6570\u503c\u8303\u56f4\u6821\u9a8c<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>ge<\/code> (\u5927\u4e8e\u7b49\u4e8e), <code>gt<\/code> (\u5927\u4e8e), <code>le<\/code> (\u5c0f\u4e8e\u7b49\u4e8e), <code>lt<\/code> (\u5c0f\u4e8e)\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from fastapi import FastAPI, Query\n\napp = FastAPI()\n\n@app.get(&quot;\/users\/&quot;)\nasync def read_users(\n    # age \u5fc5\u987b\u5927\u4e8e\u7b49\u4e8e 18 \u4e14\u5c0f\u4e8e\u7b49\u4e8e 120\n    age: int = Query(ge=18, le=120, description=&quot;\u7528\u6237\u5e74\u9f84&quot;),\n\n    # score \u5fc5\u987b\u5927\u4e8e 0 (\u4e0d\u80fd\u7b49\u4e8e 0)\n    score: float = Query(gt=0.0, description=&quot;\u7528\u6237\u5206\u6570&quot;)\n):\n    return {&quot;age&quot;: age, &quot;score&quot;: score}<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 \u5b57\u7b26\u4e32\u957f\u5ea6\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>min_length<\/code> \/ <code>max_length<\/code>\uff1a\u9650\u5236\u5b57\u7b26\u4e32\u957f\u5ea6\u3002<\/li>\n\n\n\n<li><code>pattern<\/code>\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6821\u9a8c\u5b57\u7b26\u4e32\u683c\u5f0f\uff08<strong>\u6ce8\u610f\uff1aPydantic V2 \u4e2d\u5df2\u5f03\u7528 <code>regex<\/code>\uff0c\u8bf7\u4f7f\u7528 <code>pattern<\/code><\/strong>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">import re\nfrom fastapi import FastAPI, Query\n\napp = FastAPI()\n\n@app.get(&quot;\/validate-string\/&quot;)\nasync def validate_string(\n    # \u5fc5\u987b\u662f 3-10 \u4f4d\u5b57\u6bcd\u6570\u5b57\n    username: str = Query(..., min_length=3, max_length=10, pattern=r&quot;^[a-zA-Z0-9]+$&quot;),\n\n    # \u5fc5\u987b\u662f\u6709\u6548\u7684\u90ae\u7bb1\u683c\u5f0f (\u7b80\u5316\u7248\u6b63\u5219)\n    email: str = Query(..., pattern=r&quot;^[\\w\\.-]+@[\\w\\.-]+\\.\\w+$&quot;)\n):\n    return {&quot;username&quot;: username, &quot;email&quot;: email}<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">2.4 \u679a\u4e3e\u4e0e\u56fa\u5b9a\u503c\u6821\u9a8c<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b\u53c2\u6570\u53ea\u80fd\u662f\u51e0\u4e2a\u7279\u5b9a\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Enum<\/code> \u6216 <code>Literal<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528 <code>Literal<\/code> (\u63a8\u8350\uff0c\u66f4\u7b80\u6d01):<\/strong><\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from fastapi import FastAPI, Query\nfrom typing import Literal\n\napp = FastAPI()\n\n@app.get(&quot;\/models\/&quot;)\nasync def get_model(\n    # \u53ea\u80fd\u662f &quot;resnet&quot;, &quot;vgg&quot; \u6216 &quot;transformer&quot;\n    model_name: Literal[&quot;resnet&quot;, &quot;vgg&quot;, &quot;transformer&quot;] = Query(..., description=&quot;\u6a21\u578b\u540d\u79f0&quot;)\n):\n    return {&quot;model&quot;: model_name}<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4f7f\u7528 <code>Enum<\/code>:<\/strong><\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from enum import Enum\nfrom fastapi import FastAPI, Query\n\nclass ModelName(str, Enum):\n    resnet = &quot;resnet&quot;\n    vgg = &quot;vgg&quot;\n    transformer = &quot;transformer&quot;\n\napp = FastAPI()\n\n@app.get(&quot;\/models-enum\/&quot;)\nasync def get_model_enum(\n    model_name: ModelName = Query(..., description=&quot;\u6a21\u578b\u540d\u79f0&quot;)\n):\n    return {&quot;model&quot;: model_name.value}<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">2.5 \u5217\u8868\u67e5\u8be2\u53c2\u6570 (\u591a\u4e2a\u540c\u540d\u53c2\u6570)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c URL \u662f <code>?items=foo&amp;items=bar<\/code>\uff0cFastAPI \u53ef\u4ee5\u81ea\u52a8\u5c06\u5176\u89e3\u6790\u4e3a\u5217\u8868\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from fastapi import FastAPI, Query\n\napp = FastAPI()\n\n@app.get(&quot;\/items\/list\/&quot;)\nasync def read_items_list(\n    # \u63a5\u6536\u591a\u4e2a items \u53c2\u6570\uff0c\u81f3\u5c11\u9700\u8981 1 \u4e2a\uff0c\u6700\u591a 5 \u4e2a\n    items: list[str] = Query(..., min_length=1, max_length=5, description=&quot;\u7269\u54c1\u5217\u8868&quot;)\n):\n    return {&quot;items&quot;: items}\n\n# \u8bf7\u6c42\u793a\u4f8b: GET \/items\/list\/?items=apple&amp;items=banana\n# \u8fd4\u56de: {&quot;items&quot;: [&quot;apple&quot;, &quot;banana&quot;]}<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>(\u6ce8\uff1a\u5982\u679c\u5e0c\u671b\u7528\u9017\u53f7\u5206\u9694\u7684\u5355\u4e2a\u53c2\u6570\u5982 <code>?items=apple,banana<\/code>\uff0c\u53ef\u4ee5\u58f0\u660e\u7c7b\u578b\u4e3a <code>str<\/code>\uff0c\u7136\u540e\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528 <code>.split(\",\")<\/code> \u5904\u7406\uff0c\u6216\u8005\u4f7f\u7528\u81ea\u5b9a\u4e49\u6821\u9a8c\u5668)<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u8fdb\u9636\uff1a\u81ea\u5b9a\u4e49\u6821\u9a8c\u903b\u8f91 (<code>Annotated<\/code> + \u6821\u9a8c\u51fd\u6570)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u5185\u7f6e\u7684 <code>Query<\/code> \u53c2\u6570\u65e0\u6cd5\u6ee1\u8db3\u9700\u6c42\uff08\u4f8b\u5982\uff1a\u6821\u9a8c\u4e24\u4e2a\u53c2\u6570\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb\uff0c\u6216\u590d\u6742\u7684\u4e1a\u52a1\u89c4\u5219\uff09\uff0c\u53ef\u4ee5\u7ed3\u5408 Pydantic \u7684 <code>AfterValidator<\/code> \u6216\u81ea\u5b9a\u4e49\u51fd\u6570\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><span>21<\/span><span>22<\/span><span>23<\/span><span>24<\/span><span>25<\/span><span>26<\/span><span>27<\/span><span>28<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">from fastapi import FastAPI, Query\nfrom typing import Annotated\nfrom pydantic import AfterValidator\n\napp = FastAPI()\n\n# 1. \u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6821\u9a8c\u51fd\u6570\ndef validate_password(value: str) -&gt; str:\n    if len(value) &lt; 8:\n        raise ValueError(&quot;\u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u81f3\u5c11\u4e3a 8 \u4f4d&quot;)\n    if not any(char.isdigit() for char in value):\n        raise ValueError(&quot;\u5bc6\u7801\u5fc5\u987b\u5305\u542b\u81f3\u5c11\u4e00\u4e2a\u6570\u5b57&quot;)\n    return value\n\n# 2. \u521b\u5efa\u5e26\u6709\u81ea\u5b9a\u4e49\u6821\u9a8c\u7684\u7c7b\u578b\u522b\u540d\nValidPassword = Annotated[str, AfterValidator(validate_password)]\n\n@app.get(&quot;\/register\/&quot;)\nasync def register(\n    # \u4f7f\u7528\u81ea\u5b9a\u4e49\u6821\u9a8c\u7c7b\u578b\n    password: ValidPassword = Query(..., description=&quot;\u7528\u6237\u5bc6\u7801&quot;),\n    age: int = Query(..., ge=0)\n):\n    # \u81ea\u5b9a\u4e49\u903b\u8f91\uff1a\u5e74\u9f84\u548c\u5bc6\u7801\u957f\u5ea6\u7684\u5173\u8054\u6821\u9a8c (\u793a\u4f8b)\n    if age &lt; 18 and len(password) &lt; 12:\n        raise ValueError(&quot;\u672a\u6210\u5e74\u4eba\u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u81f3\u5c11\u4e3a 12 \u4f4d&quot;)\n\n    return {&quot;message&quot;: &quot;\u6ce8\u518c\u6210\u529f&quot;}<\/code><\/pre><\/div><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u6821\u9a8c\u5931\u8d25\u65f6\u7684\u9ed8\u8ba4\u54cd\u5e94<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u6821\u9a8c\u5931\u8d25\u65f6\uff0cFastAPI \u4f1a\u81ea\u52a8\u62e6\u622a\u5e76\u8fd4\u56de <code>422 Unprocessable Entity<\/code>\uff0c\u54cd\u5e94\u4f53\u5305\u542b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u975e\u5e38\u9002\u5408\u524d\u7aef\u8c03\u8bd5\uff1a<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">{\n  &quot;detail&quot;: [\n    {\n      &quot;type&quot;: &quot;greater_than_equal&quot;,\n      &quot;loc&quot;: [&quot;query&quot;, &quot;age&quot;],\n      &quot;msg&quot;: &quot;Input should be greater than or equal to 18&quot;,\n      &quot;input&quot;: &quot;15&quot;,\n      &quot;ctx&quot;: {\n        &quot;ge&quot;: 18\n      }\n    }\n  ]\n}<\/code><\/pre><\/div><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>loc<\/code>: \u9519\u8bef\u53d1\u751f\u7684\u4f4d\u7f6e\uff08\u5982 <code>[\"query\", \"age\"]<\/code> \u8868\u793a\u67e5\u8be2\u53c2\u6570 age\uff09\u3002<\/li>\n\n\n\n<li><code>msg<\/code>: \u4eba\u7c7b\u53ef\u8bfb\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><code>type<\/code>: \u9519\u8bef\u7c7b\u578b\uff08\u5982 <code>string_too_short<\/code>, <code>greater_than_equal<\/code> \u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\u5efa\u8bae<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f18\u5148\u4f7f\u7528 <code>Annotated<\/code><\/strong>\uff1a\u4ee3\u7801\u66f4\u6574\u6d01\uff0c\u7b26\u5408\u73b0\u4ee3 Python \u7c7b\u578b\u63d0\u793a\u89c4\u8303\u3002<\/li>\n\n\n\n<li><strong>\u5584\u7528 <code>description<\/code><\/strong>\uff1a\u5728 <code>Query<\/code> \u4e2d\u6dfb\u52a0\u63cf\u8ff0\uff0cFastAPI \u4f1a\u81ea\u52a8\u5c06\u5176\u751f\u6210\u5230 Swagger UI (<code>\/docs<\/code>) \u4e2d\uff0c\u6781\u5927\u63d0\u5347 API \u6587\u6863\u7684\u53ef\u8bfb\u6027\u3002<\/li>\n\n\n\n<li><strong>Pydantic V2 \u6ce8\u610f<\/strong>\uff1a\u6b63\u5219\u6821\u9a8c\u8bf7\u4f7f\u7528 <code>pattern<\/code> \u800c\u4e0d\u662f\u65e7\u7248\u7684 <code>regex<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u590d\u6742\u903b\u8f91\u7528 Validator<\/strong>\uff1a\u7b80\u5355\u7684\u8303\u56f4\/\u957f\u5ea6\u7528 <code>Query<\/code>\uff0c\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\u6821\u9a8c\u4f7f\u7528 <code>AfterValidator<\/code> \u6216\u5728\u8def\u7531\u51fd\u6570\u5185\u90e8\u624b\u52a8 <code>raise HTTPException<\/code>\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 FastAPI \u4e2d\uff0c\u67e5\u8be2\u53c2\u6570\uff08Query Paramet&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-145","post","type-post","status-publish","format-standard","hentry","category-fastapi-"],"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=145"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/145\/revisions\/146"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}