@router.get("/products", tags=["Product"], response_model=list[schemas.Product])
def get_all_product(db: Session = Depends(get_db)):
return get_products(db)
def get_products(db: Session):
products = db.query(models.Product).all()
return products
The above works fine, when I go to localhost:5000/products I get a JSON file.
I want to save that JSON file on my computer, but the following code returns an error:
def get_all_product(db: Session = Depends(get_db)):
with open('save_products.json', 'w', encoding = 'utf-8' ) as fp:
json.dump(get_products(db), fp)
return get_products(db)
TypeError: Object of type Product is not JSON serializable.
I understand I have to make my 'Product' class serializable, but it seems FastAPI can already do that by itself (otherwise I wouldn't get a JSON file already through my request).