Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

312
Visualizações
FastApi get request shows validation error

I'm getting this error when I try to get some data from my postgre db and using fastapi.

enter image description here

I don't know why it happens...but here is my code, thank you for your help.

Route

@router.get("/fuentes", response_model=FuenteSerializer.MFuente) # <--- WHEN I REMOVE RESPONSE_MODEL WORKS AND RETURNS A JSON DATA DIRECTLY FROM MODEL I GUESS
    async def read_fuentes(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    fuentes = FuenteSerializer.get_fuente(db, skip=skip, limit=limit)
    return fuentes

sqlalchemy model

class MFuente(Base):
    __tablename__ = 'M_fuentes'

    idfuentes = Column(Integer, primary_key=True)
    idproductos = Column(ForeignKey('M_productos.idproductos', ondelete='RESTRICT', onupdate='RESTRICT'), index=True)
    autoapp = Column(CHAR(2))
    rutFabricante = Column(String(12))
    elemento = Column(String(100))
    estado = Column(Integer)
    stype = Column(Integer)
    aql = Column(String(5))
    equiv = Column(String(5))
    division = Column(String(100))
    nu = Column(Integer)
    filexcel = Column(String(100))

    M_producto = relationship('MProducto')

Serializer / schema

class MFuente(BaseModel):

    idfuentes: int
    autoapp: str
    fecregistro: datetime.date
    rutFabricante: str
    elemento: str
    estado: int
    stype: int
    aql: str
    equiv: str
    division: str
    fileexel: str
    productos: List[MProducto]

    class Config:
        orm_mode = True


def get_fuente(db: Session, skip: int = 0, limit: int = 100):
   return db.query(Fuente).offset(skip).limit(limit).all()
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

For a little debugging i created the same little prototype, and i found some possible answers.

First of all here is the app that i created:

class MFuente(BaseModel):
    name: str
    value: int

@app.get("/items/{name}", response_model=MFuente)
async def get_item(name: str):
    query = fuente_db.select().where(fuente_db.c.name == name)
    return await database.fetch_all(query)

So with this schema i get the same error

response -> name
  field required (type=value_error.missing)
response -> value
  field required (type=value_error.missing)

So i debugged a little bit more i found it's all about response_model, so i came up with this:

from typing import List
...
@app.get("/items/{name}", response_model=List[MFuente])

Everything started working:

INFO:     127.0.0.1:52872 - "GET /items/masteryoda HTTP/1.1" 200 OK

So in your case fix will be:

@router.get("/fuentes", response_model=List[FuenteSerializer.MFuente]) 
                                       ^^^^
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda