Skip to main content

UUIDField

This class provides a specialized field for handling Universally Unique Identifiers (UUIDs) within forms or data schemas. It extends character field functionality to automatically validate input strings and convert them into Python uuid.UUID objects. The field ensures that data is properly formatted during both value preparation and python-level object conversion.

Attributes

AttributeTypeDescription
default_error_messagesdict = {"invalid": _("Enter a valid UUID.")}A dictionary containing the default error message for the 'invalid' key, used when a value cannot be converted into a valid UUID.

Methods


prepare_value()

@classmethod
def prepare_value(
value: any
) - > string

Converts the given value into its string representation if it is a UUID object, ensuring it is in a format suitable for form rendering or serialization.

Parameters

NameTypeDescription
valueanyThe value to be prepared for display or serialization, typically a UUID object or a string.

Returns

TypeDescription
stringThe string representation of the UUID if the input was a UUID object; otherwise, the original value.

to_python()

@classmethod
def to_python(
value: any
) - > uuid.UUID

Validates and converts the input value into a native Python UUID object, raising a ValidationError if the string is not a properly formatted UUID.

Parameters

NameTypeDescription
valueanyThe raw input value to be converted, such as a hex string or a UUID instance.

Returns

TypeDescription
uuid.UUIDA native Python UUID object, or None if the input value is considered empty.