2

So my issue is when creating a patch request to post which will update title and content but when sending, the patch request updates all optional fields to the defaults:

post- it's class:

class Post(BaseModel):
  title: str
  content: str
  published: bool = True
  rating: Optional[bool] = None

post stored in memory:

[{"title": "title",
  "content": "stuff", 
  "published": False,
  "rating": True,
  "id": 1}]

json sent:

{
"title": "updated title",
"content": "stuffyy"
}

post received:

{
"data": {
    "title": "updated title",
    "content": "stuffyy",
    "published": true,
    "rating": null,
    "id": 1
   }
}

and the python code:

@app.patch("/posts/{id}")
def update_post(id: int, post: Post):
    index = find_index_post(id)
    if index == None:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"post with id: '{id}' does not exist, hence cannot be updated")
    post_dict = post.dict()
    post_dict['id'] = id
    my_posts[index] = post_dict
    return {"data": post_dict}

probably out of my depth here

Chris
  • 18,724
  • 6
  • 46
  • 80
RGCSERG
  • 39
  • 4
  • You would need to use Pydantic's `exclude_unset=True`, e.g., `post_dict = post.dict(exclude_unset=True)`. Please have a look at [**this answer**](https://stackoverflow.com/a/73261442/17865804) for more details. – Chris Aug 21 '22 at 05:17

1 Answers1

1

You need to exclude those fields that are not required to partially update(patching), currently on your Post schema published, rating has default values like True(true) and None(null) because you are referring post: Post here def update_post(id: int, post: Post):

class Post(BaseModel):
  title: str
  content: str
  published: bool = True        #  <--------default to true
  rating: Optional[bool] = None #  <--------default to null

So you can do this way with exclude while making post to the dictionary.

post_dict = post.dict(exclude_unset=True)

OR

post_dict = post.dict(exclude={'published', 'rating'}) 
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • this does work in not updating the fields excluded, but it does this by deleting the fields not given - any work around? – RGCSERG Aug 21 '22 at 10:43