विषय पर बढ़ें

Body - Fields

🌐 एआई और मनुष्यों द्वारा किया गया अनुवाद

यह अनुवाद मनुष्यों के मार्गदर्शन में एआई द्वारा किया गया है। 🤝

इसमें मूल अर्थ को गलत समझने या अप्राकृतिक लगने आदि जैसी गलतियाँ हो सकती हैं। 🤖

आप हमें एआई LLM को बेहतर मार्गदर्शन करने में मदद करके इस अनुवाद को बेहतर बना सकते हैं।

अंग्रेज़ी संस्करण

जिस तरह आप path operation function के parameters में Query, Path और Body के साथ अतिरिक्त validation और metadata घोषित कर सकते हैं, उसी तरह आप Pydantic के Field का उपयोग करके Pydantic models के अंदर validation और metadata घोषित कर सकते हैं।

Field import करें

सबसे पहले, आपको इसे import करना होगा:

from typing import Annotated

from fastapi import Body, FastAPI
from pydantic import BaseModel, Field

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = Field(
        default=None, title="The description of the item", max_length=300
    )
    price: float = Field(gt=0, description="The price must be greater than zero")
    tax: float | None = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Annotated[Item, Body(embed=True)]):
    results = {"item_id": item_id, "item": item}
    return results
🤓 Other versions and variants

Tip

Prefer to use the Annotated version if possible.

from fastapi import Body, FastAPI
from pydantic import BaseModel, Field

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = Field(
        default=None, title="The description of the item", max_length=300
    )
    price: float = Field(gt=0, description="The price must be greater than zero")
    tax: float | None = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item = Body(embed=True)):
    results = {"item_id": item_id, "item": item}
    return results

चेतावनी

ध्यान दें कि Field को सीधे pydantic से import किया जाता है, fastapi से नहीं, जैसे बाकी सभी (Query, Path, Body, आदि) किए जाते हैं।

model attributes घोषित करें

फिर आप model attributes के साथ Field का उपयोग कर सकते हैं:

from typing import Annotated

from fastapi import Body, FastAPI
from pydantic import BaseModel, Field

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = Field(
        default=None, title="The description of the item", max_length=300
    )
    price: float = Field(gt=0, description="The price must be greater than zero")
    tax: float | None = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Annotated[Item, Body(embed=True)]):
    results = {"item_id": item_id, "item": item}
    return results
🤓 Other versions and variants

Tip

Prefer to use the Annotated version if possible.

from fastapi import Body, FastAPI
from pydantic import BaseModel, Field

app = FastAPI()


class Item(BaseModel):
    name: str
    description: str | None = Field(
        default=None, title="The description of the item", max_length=300
    )
    price: float = Field(gt=0, description="The price must be greater than zero")
    tax: float | None = None


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item = Body(embed=True)):
    results = {"item_id": item_id, "item": item}
    return results

Field उसी तरह काम करता है जैसे Query, Path और Body, इसमें वही सभी parameters आदि होते हैं।

तकनीकी विवरण

वास्तव में, Query, Path और अन्य जिन्हें आप आगे देखेंगे, एक सामान्य Param class के subclasses के objects बनाते हैं, जो स्वयं Pydantic की FieldInfo class का subclass है।

और Pydantic का Field भी FieldInfo का एक instance लौटाता है।

Body भी सीधे FieldInfo के subclass के objects लौटाता है। और कुछ अन्य भी हैं जिन्हें आप बाद में देखेंगे, जो Body class के subclasses हैं।

याद रखें कि जब आप fastapi से Query, Path और अन्य import करते हैं, तो वे वास्तव में functions होते हैं जो विशेष classes लौटाते हैं।

सुझाव

ध्यान दें कि type, default value और Field वाले हर model के attribute की संरचना path operation function के parameter जैसी ही होती है, बस Path, Query और Body की जगह Field होता है।

अतिरिक्त जानकारी जोड़ें

आप Field, Query, Body आदि में अतिरिक्त जानकारी घोषित कर सकते हैं। और यह जनरेट किए गए JSON Schema में शामिल होगी।

आप docs में आगे examples घोषित करना सीखते समय अतिरिक्त जानकारी जोड़ने के बारे में और जानेंगे।

चेतावनी

Field को दिए गए अतिरिक्त keys आपके application के परिणामी OpenAPI schema में भी मौजूद होंगे। क्योंकि ये keys जरूरी नहीं कि OpenAPI specification का हिस्सा हों, इसलिए कुछ OpenAPI tools, उदाहरण के लिए OpenAPI validator, आपके जनरेट किए गए schema के साथ काम नहीं कर सकते।

Recap

आप model attributes के लिए अतिरिक्त validations और metadata घोषित करने के लिए Pydantic के Field का उपयोग कर सकते हैं।

आप अतिरिक्त JSON Schema metadata पास करने के लिए extra keyword arguments का भी उपयोग कर सकते हैं।