Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

384
Views
Cómo pasar typedDict como argumentos en python para fastapi

Estoy trabajando en este ejemplo simple de fastapi, me gustaría reemplazar los argumentos read_books con un TypedDict equivalente, como la línea comentada. ¿Es posible?

 from fastapi import FastAPI, Query from typing import Optional, TypedDict # would like to define model for query parameter in separeted object class GetModel(TypedDict): test: Optional[str] query_param = GetModel(test=Query('default value', max_length=10, title='titulo', description="description 123", regex="^[az]+")) app = FastAPI() @app.get("/books") def read_books(test: Optional[str] = Query('default value', max_length=10, title='titulo', description="description 123", regex="^[az]+")): #def read_books(query_param): """ comentario do endpoint """ results = {"books": [{"book_name": "The Great Hunt"}, {"book_name": "The Dragon Reborn"}]} if test: results.update({"test": {"name":test}}) return results
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

La forma de hacer esto es implementar el análisis de parámetros de consulta en una dependencia de clase separada, como se describe en la documentación (también, eche un vistazo a esta discusión ). Ejemplo a continuación:

 from fastapi import FastAPI, Query, Depends from typing import Optional app = FastAPI() class MyModel: def __init__( self, test: Optional[str] = Query('default value', max_length=10, title='titulo', description="description 123", regex="^[az]+") ): self.test = test @app.get("/books") def read_books(m: MyModel = Depends(MyModel)): results = {"books": [{"book_name": "The Great Hunt"}, {"book_name": "The Dragon Reborn"}]} if m.test: results.update({"test": {"name": m.test}}) return results
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!