Skip to main content

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

AttributeTypeDescription
unpack_ipv4boolean = FalseBoolean flag that determines whether IPv4-mapped IPv6 addresses (e.g., ::ffff:192.0.2.1) are converted to standard IPv4 format.
default_validatorslistA 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

NameTypeDescription
protocolstring = "both"The IP protocol to validate (e.g., 'both', 'IPv4', or 'IPv6').
unpack_ipv4boolean = falseWhether to unpack IPv4 mapped addresses like ::ffff:192.0.2.1.
**kwargsdictAdditional 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

NameTypeDescription
valuestringThe raw IP address string or empty value to be processed and validated.

Returns

TypeDescription
stringA normalized IP address string, or an empty string if the input is null or empty.