Skip to content

Field utilities

Field markers and the type-to-Field resolver used by the serializer metaclass. See the Type annotations guide for the resolution rules.

Field

Field(**kwargs)

Sentinel field that carries DRF kwargs to an annotated field while letting the annotation pick the final field class.

Example::

class Ser(Serializer):
    email: Email = Field(write_only=True)

Capture the kwargs so the metaclass can clone them onto the annotation-resolved field.

clone

clone(_type=None, field_name=None, **inner_kwargs)

Return a real field for the given type annotation, merging the captured kwargs with inner_kwargs.

DecimalField

DecimalField(
    *,
    max_digits: int = 20,
    decimal_places: int = 6,
    **kwargs,
)

Bases: DecimalField

DRF DecimalField with restflow defaults for max_digits and decimal_places.

SerializerFieldMap module-attribute

SerializerFieldMap: dict[type, type[Field]] = {
    int: IntegerField,
    float: FloatField,
    str: CharField,
    bool: BooleanField,
    bytes: CharField,
    datetime: DateTimeField,
    date: DateField,
    time: TimeField,
    timedelta: DurationField,
    Decimal: DecimalField,
    UUID: UUIDField,
    Email: EmailField,
    IPAddress: IPAddressField,
    dict: DictField,
    Any: JSONField,
    BlankableString: BlankableCharField,
}

get_field_from_type

get_field_from_type(
    data_type, field_name: str | None = None, **field_kwargs
)

Resolve a Python type annotation to a DRF serializer Field, handling nested Serializers, Optional, Literal, and list[T].

Email module-attribute

Email = NewType('Email', str)

IPAddress module-attribute

IPAddress = NewType('IPAddress', str)