Ugly exception when validating incorrect IP Address #9055

Closed
opened 2025-12-29 20:44:52 +01:00 by adam · 2 comments
Owner

Originally created by @llamafilm on GitHub (Jan 7, 2024).

Deployment Type

Self-hosted

NetBox Version

v3.7.0

Python Version

3.10

Steps to Reproduce

Try to create an IP address via script with incorrect address.

>>> ip = IPAddress(address='???')
>>> ip.full_clean()

Expected Behavior

I expect to see a meaningful exception message like this:

django.core.exceptions.ValidationError: ['Invalid IP address format: ???']

Observed Behavior

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/opt/netbox-3.7.0/venv/lib/python3.10/site-packages/django/db/models/base.py", line 1477, in full_clean
    self.clean()
  File "/opt/netbox-3.7.0/netbox/ipam/models/ip.py", line 842, in clean
    if self.address.prefixlen == 0:
AttributeError: 'str' object has no attribute 'prefixlen'
Originally created by @llamafilm on GitHub (Jan 7, 2024). ### Deployment Type Self-hosted ### NetBox Version v3.7.0 ### Python Version 3.10 ### Steps to Reproduce Try to create an IP address via script with incorrect address. ```py >>> ip = IPAddress(address='???') >>> ip.full_clean() ``` ### Expected Behavior I expect to see a meaningful exception message like this: ```py django.core.exceptions.ValidationError: ['Invalid IP address format: ???'] ``` ### Observed Behavior ```py Traceback (most recent call last): File "<console>", line 1, in <module> File "/opt/netbox-3.7.0/venv/lib/python3.10/site-packages/django/db/models/base.py", line 1477, in full_clean self.clean() File "/opt/netbox-3.7.0/netbox/ipam/models/ip.py", line 842, in clean if self.address.prefixlen == 0: AttributeError: 'str' object has no attribute 'prefixlen' ```
adam closed this issue 2025-12-29 20:44:52 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 8, 2024):

This is expected to happen when you intentionally pass invalid data directly to an instance without using a form field or serializer. This is the nature of working with the Python ORM directly.

@jeremystretch commented on GitHub (Jan 8, 2024): This is expected to happen when you intentionally pass invalid data directly to an instance without using a form field or serializer. This is the nature of working with the Python ORM directly.
Author
Owner

@llamafilm commented on GitHub (Jan 8, 2024):

But this issue seems to be specific to IP Addresses. For example trying to add a bad MAC address fails with a better error:

>>> iface = Device.objects.last().interfaces.first()
>>> iface.mac_address = '123'
>>> iface.full_clean()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/opt/netbox-3.7.0/venv/lib/python3.10/site-packages/django/db/models/base.py", line 1502, in full_clean
    raise ValidationError(errors)
django.core.exceptions.ValidationError: {'mac_address': ['Invalid MAC address format: 123']}
@llamafilm commented on GitHub (Jan 8, 2024): But this issue seems to be specific to IP Addresses. For example trying to add a bad MAC address fails with a better error: ``` >>> iface = Device.objects.last().interfaces.first() >>> iface.mac_address = '123' >>> iface.full_clean() Traceback (most recent call last): File "<console>", line 1, in <module> File "/opt/netbox-3.7.0/venv/lib/python3.10/site-packages/django/db/models/base.py", line 1502, in full_clean raise ValidationError(errors) django.core.exceptions.ValidationError: {'mac_address': ['Invalid MAC address format: 123']} ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9055