Complete worked example
python
from drf_lab import setup
setup()
from rest_framework import serializers
class Patch(serializers.Serializer):
title = serializers.CharField()
slots = serializers.IntegerField(min_value=1)
owner = serializers.CharField(read_only=True)
def inspect_example(payload):
original = {"title": "QA", "slots": 1, "owner": "alice"}
serializer = Patch(data=payload, partial=True)
if not serializer.is_valid():
return {"errors": {key: [error.code for error in values]
for key, values in serializer.errors.items()}}
updated = dict(original)
updated.update(serializer.validated_data)
return {"value": updated, "original": original}- Reasoning
- A submitted title is trimmed and validated, while an omitted slots value is not invented as a new update. The read-only owner input is ignored under this DRF policy and never enters validated_data. The effective owner therefore remains the one already established by the server fixture. The helper copies the original mapping before applying validated changes. Tests check both the next value and the original. This distinguishes accepting a valid patch from accidentally mutating caller data or overwriting omitted fields with defaults.