This version brings few enahcments
- Serialisation context now has request and response code
class SchemaWithCustomSerializer(Schema):
test1: str
test2: str
@model_serializer(mode="wrap")
def ser_model(self, handler, info):
request = info.context["request"] # !!!
response_status = info.context["response_status"] # !!!
return handler(self)
- With
PatchDict
you can quickly define schemas with all optional fields and use it as dict with ONLY fields that were passed in request payload (aka patch requests)
from ninja import PatchDict
class SomeSchema(Schema):
name: str
description: str
due_date: date
# Note all fields a required
@api.patch("/patch")
def modify_data(request, payload: PatchDict[SomeSchema]):
# payload ! <--- payload is a type of dict and contains only keys that were passed in request body (validated with SomeSchema)
for attr, value in payload.items():
setattr(obj, attr, value)
obj.save()
All changes
- Support import string in router.add_router by @martinsvoboda in #1256
- Add default headers for NinjaClientBase instance by @c4ffein in #1259
- Fix issue when consulting request attributes while using mocked request by @acuriel in #1244
- Allow generic filter types by @eugenenelou in #1212
- DRF-style data property for responses in tests by @c4ffein in #1260
- Pass serialisation context to model_dump (fixes #1233) by @scorpp in #1261
- PatchDict util by @vitalik in #1262
New Contributors
- @martinsvoboda made their first contribution in #1256
- @acuriel made their first contribution in #1244
- @eugenenelou made their first contribution in #1212
- @scorpp made their first contribution in #1261
Full Changelog: v1.2.2...v1.3.0