[Django] #35699: DatabaseOperations raise ValueError for valid GenericIPAddressField (inet) value with psycopg3

26 views
Skip to first unread message

Django

unread,
Aug 22, 2024, 5:06:03 AM8/22/24
to django-...@googlegroups.com
#35699: DatabaseOperations raise ValueError for valid GenericIPAddressField (inet)
value with psycopg3
-------------------------------------+-------------------------------------
Reporter: florianvazelle | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: | Severity: Normal
Keywords: | Triage Stage:
psycopg3,GenericIPAddressField | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
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
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35699>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 22, 2024, 5:11:20 AM8/22/24
to django-...@googlegroups.com
#35699: DatabaseOperations raise ValueError for valid GenericIPAddressField (inet)
value with psycopg3
-------------------------------------+-------------------------------------
Reporter: florianvazelle | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
psycopg3,GenericIPAddressField | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by florianvazelle:

Old description:

> 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
> }}}

New description:

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]

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35699#comment:1>

Django

unread,
Aug 22, 2024, 12:16:40 PM8/22/24
to django-...@googlegroups.com
#35699: DatabaseOperations raise ValueError for valid GenericIPAddressField (inet)
value with psycopg3
-------------------------------------+-------------------------------------
Reporter: florianvazelle | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
psycopg3,GenericIPAddressField | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

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?

[https://github.com/django/django/blob/7adb6dd98d50a238f3eca8c15b16b5aec12575fd/django/db/models/fields/__init__.py#L2277-L2280
The only internal usage] of the `Inet` alias is through
`GenericIPAddressField` which only supports
[https://docs.djangoproject.com/en/5.1/ref/models/fields/#django.db.models.GenericIPAddressField
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35699#comment:2>

Django

unread,
Aug 26, 2024, 3:14:46 AM8/26/24
to django-...@googlegroups.com
#35699: DatabaseOperations raise ValueError for valid GenericIPAddressField (inet)
value with psycopg3
-------------------------------------+-------------------------------------
Reporter: florianvazelle | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
psycopg3,GenericIPAddressField | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by florianvazelle):

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?
--
Ticket URL: <https://code.djangoproject.com/ticket/35699#comment:3>

Django

unread,
Aug 26, 2024, 9:28:30 AM8/26/24
to django-...@googlegroups.com
#35699: DatabaseOperations raise ValueError for valid GenericIPAddressField (inet)
value with psycopg3
-------------------------------------+-------------------------------------
Reporter: florianvazelle | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version:
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
psycopg3,GenericIPAddressField | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* resolution: => invalid
* status: new => closed

Comment:

> 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

{{{#!python
>>> 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
[https://docs.djangoproject.com/en/5.1/ref/models/instances/#validating-
objects 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
[https://github.com/jimfunk/django-postgresql-netfields at third-party
librairies] that provide a field meeting your expectations.
--
Ticket URL: <https://code.djangoproject.com/ticket/35699#comment:4>
Reply all
Reply to author
Forward
0 new messages