Skip to content

DRF serializers

The Django REST Framework target writes a frozen dataclass for each schema of your contract, and a serializer for each input. DRF checks the request before your handler runs.

Used by: Django REST Framework.

Nothing to add: the pypi:seampact-drf target writes the classes. Its one option for validation is rules.

# seampact.config.yaml, under targets
- id: notes
out: notes
use: pypi:seampact-drf
api: notes

A schema is a frozen dataclass in schemas.py. For the CreateTask schema of the overview:

@_dataclasses.dataclass(frozen=True, slots=True, kw_only=True)
class CreateTask:
title: str
effort: int | None = None

Each input is a serializer in drf.py, built from the contract:

class CreateTaskSerializer(_drf_runtime.ContractSerializer):
"""The fields of schema 'CreateTask', built in get_fields()."""
def get_fields(self) -> _drf_runtime.SerializerFields:
return {
"title": _drf_runtime.ContractTextField(min_length=1, max_length=200),
"effort": _serializers.IntegerField(required=False, min_value=0),
}

Each DRF error becomes an issue of the 400 body, named by the contract rule.

With the default rules: native, the serializers use DRF’s own field arguments (min_length, max_length, min_value, max_value). With rules: seampact-consistent, they give the contract’s exact checks, equal on every target (Switch the validator). A rule with no DRF check stops generate with rule.<key>.unsupported.

The Python generators refuse by name a oneOf, a map schema, an open or narrowed enum and a property default (Generate fails).