Complete worked example
python
from drf_lab import setup
setup()
from rest_framework import serializers
from drf_lab import reset_fixture
from drf_lab.models import Role
class RoleWrite(serializers.ModelSerializer):
slots = serializers.IntegerField(min_value=1, max_value=5)
class Meta:
model = Role
fields = ["id", "title", "country", "slots"]
read_only_fields = ["id"]
def inspect_example(payload):
alice, bob, company = reset_fixture()
serializer = RoleWrite(data=payload)
if not serializer.is_valid():
return {"errors": {key: [e.code for e in values] for key, values in serializer.errors.items()},
"rows": Role.objects.count()}
row = serializer.save(owner=alice, company=company)
return {"value": {key: serializer.data[key] for key in ["title", "country", "slots"]},
"keys": sorted(serializer.data), "owner": row.owner.username, "rows": Role.objects.count()}- Reasoning
- The serializer checks field values, then its generated create path constructs a real model row. The helper reports the public key set, selected values, stored owner and row count. Those observations distinguish accepted input from a save that actually persisted the intended state. The fixture resets its own in-memory rows before every case. IDs in newly created rows are deliberately not assumed to restart after deletion. Public keys still include id, while the test focuses on stable values and ownership rather than a fragile auto-increment expectation.