pypi django-ninja 1.3.0

2 months ago

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

Don't miss a new django-ninja release

NewReleases is sending notifications on new releases.