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

311
Views
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 answers
Answer question

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 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!