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
| Attribute | Type | Description |
|---|---|---|
| default_error_messages | dict = {"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
| Name | Type | Description |
|---|---|---|
| value | any | The value to be prepared for display or serialization, typically a UUID object or a string. |
Returns
| Type | Description |
|---|---|
string | The 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
| Name | Type | Description |
|---|---|---|
| value | any | The raw input value to be converted, such as a hex string or a UUID instance. |
Returns
| Type | Description |
|---|---|
uuid.UUID | A native Python UUID object, or None if the input value is considered empty. |