mirror of
https://github.com/fastapi/fastapi.git
synced 2026-11-03 14:45:43 -05:00
✨ Add support for combining form models (#16577)
This commit is contained in:
1 parent
386a28af2f
commit
48e43e5cbf
10 files changed
+726
-24
No files matched your search
@@ -0,0 +1,23 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import FastAPI, Form
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Profile(BaseModel):
|
||||
name: str
|
||||
|
||||
|
||||
class Preferences(BaseModel):
|
||||
tags: list[str] = []
|
||||
|
||||
|
||||
@app.post("/profiles/")
|
||||
async def create_profile(
|
||||
profile: Annotated[Profile, Form()],
|
||||
preferences: Annotated[Preferences, Form()],
|
||||
token: Annotated[str, Form()],
|
||||
):
|
||||
return {"profile": profile, "preferences": preferences, "token": token}
|
||||
@@ -0,0 +1,21 @@
|
||||
from fastapi import FastAPI, Form
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Profile(BaseModel):
|
||||
name: str
|
||||
|
||||
|
||||
class Preferences(BaseModel):
|
||||
tags: list[str] = []
|
||||
|
||||
|
||||
@app.post("/profiles/")
|
||||
async def create_profile(
|
||||
profile: Profile = Form(),
|
||||
preferences: Preferences = Form(),
|
||||
token: str = Form(),
|
||||
):
|
||||
return {"profile": profile, "preferences": preferences, "token": token}
|
||||
@@ -0,0 +1,15 @@
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import FastAPI, Form, UploadFile
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Profile(BaseModel):
|
||||
name: str
|
||||
|
||||
|
||||
@app.post("/profiles/")
|
||||
async def create_profile(profile: Annotated[Profile, Form()], avatar: UploadFile):
|
||||
return {"profile": profile, "filename": avatar.filename}
|
||||
@@ -0,0 +1,13 @@
|
||||
from fastapi import FastAPI, File, Form, UploadFile
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Profile(BaseModel):
|
||||
name: str
|
||||
|
||||
|
||||
@app.post("/profiles/")
|
||||
async def create_profile(profile: Profile = Form(), avatar: UploadFile = File()):
|
||||
return {"profile": profile, "filename": avatar.filename}
|
||||
Reference in new issue
Block a user