Complete worked example
python
from drf_lab import setup
setup()
from rest_framework import serializers
class RoleInput(serializers.Serializer):
title = serializers.CharField(max_length=20)
slots = serializers.IntegerField(min_value=1, max_value=3)
def inspect_example(payload):
serializer = RoleInput(data=payload)
if serializer.is_valid():
return {"value": dict(serializer.validated_data)}
return {"errors": {key: [error.code for error in errors]
for key, errors in serializer.errors.items()}}- Reasoning
- CharField trims surrounding whitespace by default, checks blank input and applies the declared length. required describes omission, allow_null describes an explicit null and allow_blank describes empty text. They solve different problems and should not be interchanged casually. IntegerField validates a positive small count here. The field contract is deliberately independent of any database model. The lab compares exact normalized values and verifies that caller input was not mutated. A valid serializer does not save anything unless an appropriate save path is implemented and called.