mirror of
https://github.com/fastapi/fastapi.git
synced 2026-03-12 11:58:05 -04:00
* Make compatible with pydantic v1 * Remove unused import * Remove unused ignores * Update pydantic version * Fix minor formatting issue * ⏪ Revert removing iterate_in_threadpool * ✨ Add backwards compatibility with Pydantic 0.32.2 with deprecation warnings * ✅ Update tests to not break when using Pydantic < 1.0.0 * 📝 Update docs for Pydantic version 1.0.0 * 📌 Update Pydantic range version to support from 0.32.2 * 🎨 Format test imports * ✨ Add support for Pydantic < 1.2 for populate_validators * ✨ Add backwards compatibility for Pydantic < 1.2.0 with required fields * 📌 Relax requirement for Pydantic to < 2.0.0 🎉 🚀 * 💚 Update pragma coverage for older Pydantic versions
27 lines
613 B
Python
27 lines
613 B
Python
from typing import List
|
|
|
|
from fastapi import FastAPI
|
|
from pydantic import BaseModel
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class Item(BaseModel):
|
|
name: str
|
|
description: str = None
|
|
price: float
|
|
tax: float = 10.5
|
|
tags: List[str] = []
|
|
|
|
|
|
items = {
|
|
"foo": {"name": "Foo", "price": 50.2},
|
|
"bar": {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
|
|
"baz": {"name": "Baz", "description": None, "price": 50.2, "tax": 10.5, "tags": []},
|
|
}
|
|
|
|
|
|
@app.get("/items/{item_id}", response_model=Item, response_model_exclude_unset=True)
|
|
async def read_item(item_id: str):
|
|
return items[item_id]
|