I am getting this error for get request value is not a valid list (type=type_error.list)'
but when remove my response model not getting any error. Here is my code and why getting this error for using response model and how to overcome this error
@router.get('/ads', response_model=List[schemas.ShowBlog])
def all_publish_blog(db: Session = Depends(get_db)):
blogs = db.query(models.Blog).filter(models.Blog.is_published==True).all()
if blogs:
return {
"blogs":blogs
}
else:
raise HTTPException(400, detail=f'din\'t find any published ads')
here is myschemas
class ShowBlog(BaseModel):
title: str
body: str
slug: str
image_url: str
ads_published_date: datetime
ads_last_update_date: Optional[datetime] = ...
ads_comment: Optional[str] = ...
users_bp: ShowUser
class Config():
orm_mode = True
CodePudding user response:
the piece of your code that reads
return {
"blogs":blogs
}
returns a dictionary with the single key "blogs", presumably with the value being a list of blogs. The only way this schema would be validated by pydantic/fastapi is if your schema was written as such
class Blogs(BaseModel):
blogs: List[ShowBlog]
The response schema is currently expecting a list of blogs to be returned.
A potential fix for your issue as it stands is to just change your return statement to say
return blogs