Python - FastAPI declare form data based on a pydantic model schema

Viewed 42

I have the below route defined in FastAPI:

from schema import DocumentRequest

responses = {
    404: {"description": "The shipment was not found"},
}
@router.get("/{id}", responses=responses)
def get_documents(documents: DocumentRequest = Form(...)):   
    #Removed code in the example... 
    return

And my DocumentRequest.py schema looks like:

from pydantic import BaseModel

class DocumentRequest(BaseModel):
    shipment_id: str
    company: str
    enterprise_id: str
    server_id: str
    data_target_type: str

The problem is that this gives me the following in the docs:

enter image description here

As you can see in the above screenshot, the form data is having a parent declared called "documents", and the request itself is defined under this "documents".

How can I declare that it is only the form fields declared in the schema DocumentRequest.py that needs to be passed?

I can fix it by manually entereing the form fields like so:

def get_documents(shipment_id: str = Form(default=None), company: str = Form(default=None), enterprise_id = Form(default=None), server_id: str = Form(default=None), data_target_type: str = Form(default=None)):

But I would like to use the schema defined.

0 Answers
Related