Here's a self-contained, minimal, reproducible, example with my use case:
import fastapi
import starlette.testclient
from pydantic import BaseModel, Field
app = fastapi.FastAPI()
class InModel(BaseModel):
start_time: str = Field(alias='start_hms')
class OutModel(BaseModel):
end_time: str = Field(alias='end_hms')
@app.post('/test', response_model=OutModel)
def test(query_body: InModel):
print(query_body) # start_time='14:00:00'
print(query_body.dict()) # {'start_time': '14:00:00'}
out_model = {'end_time': '12:00:00'}
return out_model
with starlette.testclient.TestClient(app) as test_client:
try:
response = test_client.post('/test', json={'start_hms': '14:00:00'})
print(response.json())
except Exception as exc:
print(exc)
A wonderful framework! Very happy to use it to coding. Here is a little problem that make me really confused. I can easily change the name for request body from public name to field name by add alias in pydantic model (just what i did to InModel in the code), but when I do the same thing to the return model (OutModel in the code), an error occurred.
1 validation error for OutModel
response -> end_hms
field required (type=value_error.missing).
{"end_hms": "14:00:00"}.Hi @zihaooo you could do it like this.
class OutModel(BaseModel):
end_time: str = Field(alias='end_hms')
class Config:
# so we can use either the 'end_time' attribute or the alias attribute in this case 'end_hms'
allow_population_by_field_name = True
@MacMacky Many thanks~
For someone has the same question: https://pydantic-docs.helpmanual.io/usage/model_config/
And I complete the example with @MacMacky 's code:
import fastapi
import starlette.testclient
from pydantic import BaseModel, Field
app = fastapi.FastAPI()
class InModel(BaseModel):
start_time: str = Field(alias='start_hms')
class OutModel(BaseModel):
end_time: str = Field(alias='end_hms')
class Config:
# so we can use either the 'end_time' attribute or the alias attribute in this case 'end_hms'
allow_population_by_field_name = True
@app.post('/test', response_model=OutModel)
def test(query_body: InModel):
print('Enter server side...')
print(f'Query body: {query_body.dict()}') # {'start_time': '14:00:00'}
out_model = {'end_time': '20:00:00'}
print(f'Return value: {out_model}')
print('Making response...\n')
return out_model
with starlette.testclient.TestClient(app) as test_client:
try:
print('Enter client side...')
query_body = {'start_hms': '14:00:00'}
print(f'Query body: {query_body}')
print('Making request...\n')
response = test_client.post('/test', json=query_body)
print('Back to client side...')
print(f'Return value: {response.json()}')
print('Exit client side.\n')
except Exception as exc:
print(exc)
Output:
Enter client side...
Query body: {'start_hms': '14:00:00'}
Making request...
Enter server side...
Query body: {'start_time': '14:00:00'}
Return value: {'end_time': '20:00:00'}
Making response...
Back to client side...
Return value: {'end_hms': '20:00:00'}
Exit client side.
@zihaooo You're welcome, man. Glad that I helped you.
Will you please close this issue. Thanks.
Thanks for the help here everyone! :clap: :bow:
Thanks for reporting back and closing the issue :+1:
Most helpful comment
Hi @zihaooo you could do it like this.