0

I'd like to partly update database via PATCH method in FastAPI. I use Postgres as my database, Postman to test.

I followed the example on FastAPI document, link: https://fastapi.tiangolo.com/tutorial/body-updates/#partial-updates-with-patch

I use GET to fetch the original data in DB, copy the content to body raw json, then change the part where I need to update and choose PATCH, click send in Postman, an error occurs: main.Product() argument after ** must be a mapping, not Product

What is the right approach to PATCH data? I omitted the code to connect to Postgres using psycopg2

from fastapi import FastAPI, Response, status, HTTPException, Path
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel

app = FastAPI()

class Product(BaseModel):
    name: str
    price: float
    inventory: int

@app.get("/posts/{id}")
def get_a_post(id: int = Path(None, title='Prod ID')):
    cursor.execute('''SELECT * FROM public.products WHERE ID = %s''',(str(id),))
    post = cursor.fetchone()
    if not post:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND,
                            detail=f"product with id {id} was not found!")
    return post

@app.patch("/posts/{id}", response_model=Product)
def patch_posts(id: int, post: Product):
    stored_data = post
    stored_model = Product(**stored_data)
    update_data = post.dict(exclude_unset=True)
    updated_data = stored_model.copy(update=update_data)
    post = jsonable_encoder(updated_data)
    return{"partially updated product": post}
Liu Yu
  • 391
  • 1
  • 6
  • 16

1 Answers1

0

Looks like your issue is caused by trying to get the key/value pairs via **stored_data, but that variable is of type Product.

In your patch_posts function, change stored_data = post to stored_data = post.dict().

Using the example you provided:

import uvicorn
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel

app = FastAPI()

class Product(BaseModel):
    name: str
    price: float
    inventory: int

@app.patch("/posts/{id}", response_model=Product)
def patch_posts(id: int, post: Product):
    stored_data = post.dict()
    stored_model = Product(**stored_data)
    update_data = post.dict(exclude_unset=True)
    updated_data = stored_model.copy(update=update_data)
    post = jsonable_encoder(updated_data)
    return post
Josh
  • 1,556
  • 1
  • 10
  • 21
  • Thank you. I tried your way. The error is gone, now a new error. I think I need a new model, put Optional in every field. Seems exclude_unset=True did not do the job... pydantic.error_wrappers.ValidationError: 2 validation errors for Product response -> name field required (type=value_error.missing) response -> price field required (type=value_error.missing) – Liu Yu Jan 23 '22 at 07:08
  • Note in my example above, I have also updated what the function returns. You defined the `response_model` as `Product`, so you must return a single `Product` model. In your first example, you were returning a dictionary instead. – Josh Jan 23 '22 at 07:45
  • Thank you. I noticed. I removed response_model in the decorator. I created a new BaseModel with all fields optional. It seems work. – Liu Yu Jan 23 '22 at 08:09