Skip to content

1.3.0

Latest
Compare
Choose a tag to compare
@vitalik vitalik released this 15 Aug 09:14
· 35 commits to master since this release
b1ecd36

This version brings few enahcments

  1. 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)
  1. 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

New Contributors

Full Changelog: v1.2.2...v1.3.0