Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.2K
Views
How to partially update data with FastAPI and PyDantic BaseModel using PATCH

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}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!