Plugin Forms - MACAddressField Validation #8838

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

Originally created by @wz4 on GitHub (Nov 14, 2023).

NetBox version

v3.6.5

Python version

3.10

Steps to Reproduce

  1. This problem can be reproduced by following the Netbox Plugin Tutorial. The problems is encountered on forms if attempting to use the MACAddressField class.
  2. Generate a form as outlined in Netbox Plugin Tutorial
  3. Import MACAddressField class from utilities.forms.fields
  4. Create a field for mac_address in your model
  5. Assign MACAddressField class to mac_address field
  6. Run the webserver and attempt to Add and object for the plugin
  7. The following error is produced before page load (AttributeError: 'CharField' object has no attribute 'attrs')

Expected Behavior

The edit form will load and apply MAC Address validation on input.

Problem was fixed by editing the MACAddressField class in netbox/utilities/forms/fields/fields.py.
widget = forms.TextInput

Observed Behavior

File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 104, in view
return self.dispatch(request, *args, **kwargs)
File "/opt/netbox/netbox/netbox/views/generic/object_views.py", line 170, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/opt/netbox/netbox/netbox/views/generic/base.py", line 26, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/opt/netbox/netbox/utilities/views.py", line 99, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/opt/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 143, in dispatch
return handler(request, *args, **kwargs)
File "/opt/netbox/netbox/netbox/views/generic/object_views.py", line 221, in get
form = self.form(instance=obj, initial=initial_data)
File "/opt/netbox/netbox/utilities/forms/mixins.py", line 30, in init
css = field.widget.attrs.get('class', '')
AttributeError: 'CharField' object has no attribute 'attrs'

Originally created by @wz4 on GitHub (Nov 14, 2023). ### NetBox version v3.6.5 ### Python version 3.10 ### Steps to Reproduce 1. This problem can be reproduced by following the Netbox Plugin Tutorial. The problems is encountered on forms if attempting to use the MACAddressField class. 2. Generate a form as outlined in [Netbox Plugin Tutorial](https://github.com/netbox-community/netbox-plugin-tutorial/blob/main/tutorial/step04-forms.md) 3. Import MACAddressField class from utilities.forms.fields 4. Create a field for mac_address in your model 5. Assign MACAddressField class to mac_address field 6. Run the webserver and attempt to Add and object for the plugin 7. The following error is produced before page load (AttributeError: 'CharField' object has no attribute 'attrs') ### Expected Behavior The edit form will load and apply MAC Address validation on input. Problem was fixed by editing the MACAddressField class in netbox/utilities/forms/fields/fields.py. widget = forms.TextInput ### Observed Behavior File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/opt/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 104, in view return self.dispatch(request, *args, **kwargs) File "/opt/netbox/netbox/netbox/views/generic/object_views.py", line 170, in dispatch return super().dispatch(request, *args, **kwargs) File "/opt/netbox/netbox/netbox/views/generic/base.py", line 26, in dispatch return super().dispatch(request, *args, **kwargs) File "/opt/netbox/netbox/utilities/views.py", line 99, in dispatch return super().dispatch(request, *args, **kwargs) File "/opt/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 143, in dispatch return handler(request, *args, **kwargs) File "/opt/netbox/netbox/netbox/views/generic/object_views.py", line 221, in get form = self.form(instance=obj, initial=initial_data) File "/opt/netbox/netbox/utilities/forms/mixins.py", line 30, in __init__ css = field.widget.attrs.get('class', '') AttributeError: 'CharField' object has no attribute 'attrs'
adam closed this issue 2025-12-29 20:41:54 +01:00
Author
Owner

@jeremystretch commented on GitHub (Nov 28, 2023):

It's not clear from your description what your issue is, but as this relates to plugin development please start a discussion for assistance. Bug reports are only for reproducible issues in NetBox itself.

@jeremystretch commented on GitHub (Nov 28, 2023): It's not clear from your description what your issue is, but as this relates to plugin development please [start a discussion](https://github.com/netbox-community/netbox/discussions/new?category=help-wanted) for assistance. Bug reports are only for reproducible issues in NetBox itself.
Author
Owner

@wz4 commented on GitHub (Nov 29, 2023):

Thank you. I would clarify by saying that the Netbox provided input validation MACAddressField does not work. The widget needs to be changed from CharField to TextInput.

@wz4 commented on GitHub (Nov 29, 2023): Thank you. I would clarify by saying that the Netbox provided input validation [MACAddressField](https://docs.netbox.dev/en/stable/plugins/development/forms/#utilities.forms.fields.fields.MACAddressField) does not work. The widget needs to be changed from CharField to TextInput.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#8838