From 7a42bded50175ad93d3c14ae662eb58b114a8148 Mon Sep 17 00:00:00 2001 From: Vitaliy Kucheryaviy Date: Sun, 11 Aug 2024 00:43:24 +0300 Subject: [PATCH] PatchDict docs --- docs/docs/guides/response/django-pydantic.md | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/docs/docs/guides/response/django-pydantic.md b/docs/docs/guides/response/django-pydantic.md index f3f23ea2..0cd4d803 100644 --- a/docs/docs/guides/response/django-pydantic.md +++ b/docs/docs/guides/response/django-pydantic.md @@ -125,3 +125,31 @@ def patch(request, pk: int, payload: PatchGroupSchema): ``` + +#### PatchDict + +Another way to work with patch request data is a `PatchDict` container which allows you to make +a schema with all optional fields and get a dict with **only** fields that was provide + +```Python hl_lines="1 11" +from ninja import PatchDict + +class GroupSchema(Schema): + # You do not have to make fields optional it will be converted by PatchDict + name: str + description: str + due_date: date + + +@api.patch("/patch/{pk}") +def modify_data(request, pk: int, payload: PatchDict[GroupSchema]): + obj = MyModel.objects.get(pk=pk) + + for attr, value in payload.items(): + setattr(obj, attr, value) + + obj.save() + +``` + +in this example the `payload` argument will be a type of `dict` only fields that were passed in request and validated using `GroupSchema`