FastApi python how I can upload multiple image and save their url?

Viewed 23

How I can upload multiple image and save their url? here is my code

@app.post('/blog')
def create(request: schemas.Blog, db: Session = Depends(get_db)):
    new_blog = models.Blog(title=request.title,body=request.body)
    db.add(new_blog)
    db.commit()
    db.refresh(new_blog)
    return new_blog

here is my BaseModel

class Blog(BaseModel):
    title: str = Field(
         max_length=250
    )
    body: str
    image_url: str

here is my database table and column

class Blog(Base):
      __tablename__ = 'blogs'
      id = Column(Integer, primary_key=True, index=True)
      title = Column(String(250))
      body = Column(Text)
      image_url = Column(Text)

I tried this code after read their docs but getting error 422 Unprocessable Entity

@app.post('/blog')
def create(request: schemas.Blog, db: Session = Depends(get_db),files: list[bytes] = File()):
..... my code

here is full error log of my response body:

{
  "detail": [
    {
      "loc": [
        "body",
        "request"
      ],
      "msg": "value is not a valid dict",
      "type": "type_error.dict"
    },
    {
      "loc": [
        "body",
        "files",
        0
      ],
      "msg": "byte type expected",
      "type": "type_error.bytes"
    }
  ]
}
0 Answers
Related