feat: enhance SubnetField to support non-strict IP parsing

Updated SubnetField methods to use `ip_network` with `strict=False`, allowing for more lenient IP address parsing. Added `value_to_string` method to improve serialization of SubnetField values. This enhances flexibility and ensures robust handling of various IP formats.
This commit is contained in:
Kumi 2024-07-12 14:17:51 +02:00
parent 245f50d0fd
commit b54bf87d59
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -12,16 +12,20 @@ class SubnetField(models.CharField):
def from_db_value(self, value, expression, connection):
if value is None:
return value
return ipaddress.ip_network(value)
return ipaddress.ip_network(value, strict=False)
def to_python(self, value):
if value is None:
return value
if isinstance(value, ipaddress._BaseNetwork):
return value
return ipaddress.ip_network(value)
return ipaddress.ip_network(value, strict=False)
def get_prep_value(self, value):
if value is None:
return value
return str(value)
def value_to_string(self, obj):
value = self.value_from_object(obj)
return self.get_prep_value(value)