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:
parent
245f50d0fd
commit
b54bf87d59
1 changed files with 6 additions and 2 deletions
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue