Opened 3 months ago

Closed 3 months ago

#35699 closed Bug (invalid)

DatabaseOperations raise ValueError for valid GenericIPAddressField (inet) value with psycopg3

Reported by: florianvazelle Owned by:
Component: Database layer (models, ORM) Version:
Severity: Normal Keywords: psycopg3, GenericIPAddressField
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by florianvazelle)

I move recently to psycopg3, and it's seems that the Inet class change to be an alias of ipaddress.ip_address in https://github.com/django/django/blob/main/django/db/backends/postgresql/psycopg_any.py#L12.
But some valid inet value raise an ValueError, such as:

>>> import ipaddress
>>> ipaddress.ip_address("192.168.1/24")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/florian/.pyenv/versions/3.11.9/lib/python3.11/ipaddress.py", line 54, in ip_address
    raise ValueError(f'{address!r} does not appear to be an IPv4 or IPv6 address')
ValueError: '192.168.1/24' does not appear to be an IPv4 or IPv6 address

Reference: https://www.postgresql.org/docs/current/datatype-net-types.html#DATATYPE-INET

Change History (4)

comment:1 by florianvazelle, 3 months ago

Description: modified (diff)

comment:2 by Simon Charette, 3 months ago

Thank you for your report.

Are you running into this issue by importing the django.db.backends.postgresql.psycopg_any.Inet alias directly or through of of Django's?

The only internal usage of the Inet alias is through GenericIPAddressField which only supports an IPv4 or IPv6 address. In other words, GenericIPAddressField was never meant to support IP ranges such as 192.168.1/24 in the first place from my understanding and it just happened to work by chance on Postgres previously because inet was used as the column type.

Last edited 3 months ago by Simon Charette (previous) (diff)

comment:3 by florianvazelle, 3 months ago

I wasn't using Inet directly but through django, so it's missing some prior validations and indications, because the behavior changes between versions of psycopg.

So maybe a InetField would be a good addition?

comment:4 by Simon Charette, 3 months ago

Resolution: invalid
Status: newclosed

I wasn't using Inet directly but through django, so it's missing some prior validations and indications, because the behavior changes between versions of psycopg.

I'm not sure what kind of validation its missing

>>> GenericIPAddressField().run_validators('192.168.1/24')
...
ValidationError: ['Enter a valid IPv4 or IPv6 address.']

Obviously if you assign the string value directly to your model instance field and don't trigger model validation values won't be validated on save just like assigning a non-email string to an EmailField without calling validation is allowed.

I'm going to close this as invalid and suggest you have a look at third-party librairies that provide a field meeting your expectations.

Note: See TracTickets for help on using tickets.
Back to Top