Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Join theFastAPI Cloud waiting list 🚀
Follow@fastapi onX (Twitter) to stay updated
FollowFastAPI onLinkedIn to stay updated
Subscribe to theFastAPI and friends newsletter 🎉
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor

Extra Data Types

Up to now, you have been using common data types, like:

  • int
  • float
  • str
  • bool

But you can also use more complex data types.

And you will still have the same features as seen up to now:

  • Great editor support.
  • Data conversion from incoming requests.
  • Data conversion for response data.
  • Data validation.
  • Automatic annotation and documentation.

Other data types

Here are some of the additional data types you can use:

  • UUID:
    • A standard "Universally Unique Identifier", common as an ID in many databases and systems.
    • In requests and responses will be represented as astr.
  • datetime.datetime:
    • A Pythondatetime.datetime.
    • In requests and responses will be represented as astr in ISO 8601 format, like:2008-09-15T15:53:00+05:00.
  • datetime.date:
    • Pythondatetime.date.
    • In requests and responses will be represented as astr in ISO 8601 format, like:2008-09-15.
  • datetime.time:
    • A Pythondatetime.time.
    • In requests and responses will be represented as astr in ISO 8601 format, like:14:23:55.003.
  • datetime.timedelta:
    • A Pythondatetime.timedelta.
    • In requests and responses will be represented as afloat of total seconds.
    • Pydantic also allows representing it as a "ISO 8601 time diff encoding",see the docs for more info.
  • frozenset:
    • In requests and responses, treated the same as aset:
      • In requests, a list will be read, eliminating duplicates and converting it to aset.
      • In responses, theset will be converted to alist.
      • The generated schema will specify that theset values are unique (using JSON Schema'suniqueItems).
  • bytes:
    • Standard Pythonbytes.
    • In requests and responses will be treated asstr.
    • The generated schema will specify that it's astr withbinary "format".
  • Decimal:
    • Standard PythonDecimal.
    • In requests and responses, handled the same as afloat.
  • You can check all the valid Pydantic data types here:Pydantic data types.

Example

Here's an examplepath operation with parameters using some of the above types.

fromdatetimeimportdatetime,time,timedeltafromtypingimportAnnotatedfromuuidimportUUIDfromfastapiimportBody,FastAPIapp=FastAPI()@app.put("/items/{item_id}")asyncdefread_items(item_id:UUID,start_datetime:Annotated[datetime,Body()],end_datetime:Annotated[datetime,Body()],process_after:Annotated[timedelta,Body()],repeat_at:Annotated[time|None,Body()]=None,):start_process=start_datetime+process_afterduration=end_datetime-start_processreturn{"item_id":item_id,"start_datetime":start_datetime,"end_datetime":end_datetime,"process_after":process_after,"repeat_at":repeat_at,"start_process":start_process,"duration":duration,}
🤓 Other versions and variants

Tip

Prefer to use theAnnotated version if possible.

fromdatetimeimportdatetime,time,timedeltafromuuidimportUUIDfromfastapiimportBody,FastAPIapp=FastAPI()@app.put("/items/{item_id}")asyncdefread_items(item_id:UUID,start_datetime:datetime=Body(),end_datetime:datetime=Body(),process_after:timedelta=Body(),repeat_at:time|None=Body(default=None),):start_process=start_datetime+process_afterduration=end_datetime-start_processreturn{"item_id":item_id,"start_datetime":start_datetime,"end_datetime":end_datetime,"process_after":process_after,"repeat_at":repeat_at,"start_process":start_process,"duration":duration,}

Note that the parameters inside the function have their natural data type, and you can, for example, perform normal date manipulations, like:

fromdatetimeimportdatetime,time,timedeltafromtypingimportAnnotatedfromuuidimportUUIDfromfastapiimportBody,FastAPIapp=FastAPI()@app.put("/items/{item_id}")asyncdefread_items(item_id:UUID,start_datetime:Annotated[datetime,Body()],end_datetime:Annotated[datetime,Body()],process_after:Annotated[timedelta,Body()],repeat_at:Annotated[time|None,Body()]=None,):start_process=start_datetime+process_afterduration=end_datetime-start_processreturn{"item_id":item_id,"start_datetime":start_datetime,"end_datetime":end_datetime,"process_after":process_after,"repeat_at":repeat_at,"start_process":start_process,"duration":duration,}
🤓 Other versions and variants

Tip

Prefer to use theAnnotated version if possible.

fromdatetimeimportdatetime,time,timedeltafromuuidimportUUIDfromfastapiimportBody,FastAPIapp=FastAPI()@app.put("/items/{item_id}")asyncdefread_items(item_id:UUID,start_datetime:datetime=Body(),end_datetime:datetime=Body(),process_after:timedelta=Body(),repeat_at:time|None=Body(default=None),):start_process=start_datetime+process_afterduration=end_datetime-start_processreturn{"item_id":item_id,"start_datetime":start_datetime,"end_datetime":end_datetime,"process_after":process_after,"repeat_at":repeat_at,"start_process":start_process,"duration":duration,}

[8]ページ先頭

©2009-2026 Movatter.jp