GenericIPAddressField
This class provides a field for storing and validating IPv4 and IPv6 addresses. It supports protocol-specific validation, optional IPv4-mapped IPv6 address unpacking, and automatic normalization of IPv6 addresses. The field ensures that input values are properly formatted and stripped of whitespace before being processed.
Attributes
| Attribute | Type | Description |
|---|---|---|
| unpack_ipv4 | boolean = False | Boolean flag that determines whether IPv4-mapped IPv6 addresses (e.g., ::ffff:192.0.2.1) are converted to standard IPv4 format. |
| default_validators | list | A list of validation functions generated based on the specified protocol and IPv4 unpacking preference to ensure the input is a valid IP address. |
Constructor
Signature
def GenericIPAddressField(
protocol: string = "both",
unpack_ipv4: boolean = false,
**kwargs: dict
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| protocol | string = "both" | The IP protocol to validate (e.g., 'both', 'IPv4', or 'IPv6'). |
| unpack_ipv4 | boolean = false | Whether to unpack IPv4 mapped addresses like ::ffff:192.0.2.1. |
| **kwargs | dict | Additional keyword arguments passed to the parent CharField class. |
Methods
to_python()
@classmethod
def to_python(
value: string
) - > string
Converts the input value into a normalized IP address string, handling IPv6 cleaning and IPv4 unpacking if configured.
Parameters
| Name | Type | Description |
|---|---|---|
| value | string | The raw IP address string or empty value to be processed and validated. |
Returns
| Type | Description |
|---|---|
string | A normalized IP address string, or an empty string if the input is null or empty. |