'Unexplained Validation Errors in my Fast API
I'm Writing a timesheet API That has the fields TimeSheet_Id, Employee_ID, Employee_Name, Date, hours. So far I can get it to Create new Time sheet objects and upload them onto a sqlite Database, So there doesn't seem to be an error when using a post request, but when I try to retrieve data from it, for example retrieving all entries with an Employee_Id equal to 1, it just gives me the validation error list bellow:
pydantic.error_wrappers.ValidationError: 5 validation errors for TimeSheet
response -> Sheet_Id
field required (type=value_error.missing)
response -> Employee_Id
field required (type=value_error.missing)
response -> Employee_Name
field required (type=value_error.missing)
response -> Date
field required (type=value_error.missing)
response -> hours
field required (type=value_error.missing)
All the forums I have checked that have this error show up usually happen when they preform a post request and not a get request like mine. Here is what my main.py looks like:
from email.policy import HTTP
from http.client import HTTPException
from fastapi import Depends, FastAPI, status
from requests import session
from sqlalchemy.orm import Session
from database import Base, SessionLocal, engine
from typing import List
import models
import schema
app = FastAPI()
#Create Database
Base.metadata.create_all(bind=engine)
#Get DB
def get_session():
session = SessionLocal()
try:
yield session
finally:
session.close()
@app.get("/")
def root():
return "TimeSheet app"
@app.post("/TimeSheet", status_code=status.HTTP_201_CREATED)
def create_TimeSheet(timesheet: schema.TimeSheetCreate, session: Session = Depends(get_session)):
timesheet_db = models.TimeSheetDB(Employee_Id= timesheet.Employee_Id, Employee_Name = timesheet.Employee_Name, Date = timesheet.Date, hours = timesheet.hours)
session.add(timesheet_db)
session.commit()
session.refresh(timesheet_db)
session.close
return session
@app.get("/TimeSheets/{id}", response_model=schema.TimeSheet)
def get_TimeSheet(e_id:int):
session = Session(bind=engine, expire_on_commit=False)
sheet_list = session.query(models.TimeSheetDB)\
.filter(models.TimeSheetDB.Employee_Id == e_id)\
.all()
session.close
return sheet_list
@app.get("/TimeSheet/{id}", response_model=schema.TimeSheet)
def get_TimeSheetDate(date:str, e_id:int, year:int, month:int, day:int):
session = Session(bind=engine, expire_on_commit=False)
sheet_list = session.query(models.TimeSheetDB)\
.filter(models.TimeSheetDB.Employee_Id==e_id, models.TimeSheetDB.Date == date(year,month,day))\
.all()
session.close
return sheet_list
Here's schema.py:
from pydantic import BaseModel
from datetime import date
#Create Timesheet Model
class TimeSheetCreate(BaseModel):
Employee_Id:int
Employee_Name:str
Date:date
hours:int
class TimeSheet(BaseModel):
Sheet_Id:int
Employee_Id:int
Employee_Name:str
Date:date
hours:int
class Config:
orm_mode=True
And Here's models.py:
from sqlalchemy import String, Integer, Column, Date
from database import Base
class TimeSheetDB(Base):
__tablename__ = "TimeSheets"
TimeSheet_Id = Column(Integer, primary_key=True)
Employee_Id = Column(Integer)
Employee_Name = Column(String(50))
Date = Column(Date)
hours = Column(Integer)
Solution 1:[1]
you return list of objects so you should edit response model like that:
@app.get("/TimeSheets/{e_id}", response_model=List[schema.TimeSheet])
def get_TimeSheet(e_id:int):
session = Session(bind=engine, expire_on_commit=False)
sheet_list = session.query(models.TimeSheetDB)\
.filter(models.TimeSheetDB.Employee_Id == e_id)\
.all()
session.close()
return sheet_list
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
