Dynamic choice fields don't support plugin API endpoints #6156

Closed
opened 2025-12-29 19:37:25 +01:00 by adam · 1 comment
Owner

Originally created by @jeremystretch on GitHub (Feb 28, 2022).

NetBox version

v3.2-beta1

Python version

3.8

Steps to Reproduce

  1. Create a form with a DynamicModelChoiceField referencing a plugin model.
  2. Attempt to render the form within a view.

(This bug was originally reported by @peteeckel in WG8333.)

Expected Behavior

The field should render and populate choices dynamically using the REST API backend.

Observed Behavior

An exception is raised due the view namespace not being properly resolved:

django.urls.exceptions.NoReverseMatch: 'pluginname-api' is not a registered namespace
Originally created by @jeremystretch on GitHub (Feb 28, 2022). ### NetBox version v3.2-beta1 ### Python version 3.8 ### Steps to Reproduce 1. Create a form with a DynamicModelChoiceField referencing a plugin model. 2. Attempt to render the form within a view. (This bug was originally reported by @peteeckel in WG8333.) ### Expected Behavior The field should render and populate choices dynamically using the REST API backend. ### Observed Behavior An exception is raised due the view namespace not being properly resolved: ``` django.urls.exceptions.NoReverseMatch: 'pluginname-api' is not a registered namespace ```
adam added the type: bugstatus: acceptedbeta labels 2025-12-29 19:37:25 +01:00
adam closed this issue 2025-12-29 19:37:25 +01:00
Author
Owner

@peteeckel commented on GitHub (Feb 28, 2022):

I just tested it and still get an exception:

Traceback (most recent call last):
  File "/opt/netbox/lib/python3.8/site-packages/django/template/base.py", line 870, in _resolve_lookup
    current = getattr(current, bit)
  File "/opt/netbox/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/opt/netbox/lib/python3.8/site-packages/django/forms/forms.py", line 455, in changed_data
    return [name for name, bf in self._bound_items() if bf._has_changed()]
  File "/opt/netbox/lib/python3.8/site-packages/django/forms/forms.py", line 455, in <listcomp>
    return [name for name, bf in self._bound_items() if bf._has_changed()]
  File "/opt/netbox/lib/python3.8/site-packages/django/forms/forms.py", line 154, in _bound_items
    yield name, self[name]
  File "/opt/netbox/lib/python3.8/site-packages/django/forms/forms.py", line 177, in __getitem__
    bound_field = field.get_bound_field(self, name)
  File "/opt/netbox/netbox/utilities/forms/fields/dynamic.py", line 106, in get_bound_field
    widget.attrs['data-url'] = reverse(viewname)
  File "/opt/netbox/lib/python3.8/site-packages/django/urls/base.py", line 77, in reverse
    raise NoReverseMatch(
django.urls.exceptions.NoReverseMatch: 'netbox_dns' is not a registered namespace inside 'plugins-api'
Internal Server Error: /plugins/netbox-dns/records/
Traceback (most recent call last):
  File "/opt/netbox/lib/python3.8/site-packages/django/forms/forms.py", line 168, in __getitem__
    field = self.fields[name]
KeyError: 'changed_data'

The only place I tried to use DynamicModelMultipleChoiceFieldwas here:

class RecordFilterForm(BootstrapMixin, forms.Form):
    """Form for filtering Record instances."""

    model = Record

    q = CharField(
        required=False,
        widget=forms.TextInput(attrs={"placeholder": "Name, Zone or Value"}),
        label="Search",
    )
    type = forms.MultipleChoiceField(
        choices=add_blank_choice(Record.CHOICES),
        required=False,
        widget=StaticSelectMultiple(),
    )
    name = CharField(
        required=False,
        label="Name",
    )
    value = CharField(
        required=False,
        label="Value",
    )
    zone_id = DynamicModelMultipleChoiceField(
        queryset=Zone.objects.all(),
        required=False,
        label="Zone",
    )
    tag = TagFilterField(Record)

Is anything wrong with the code, or is there still some problem with the get_bound_field method?

@peteeckel commented on GitHub (Feb 28, 2022): I just tested it and still get an exception: ``` Traceback (most recent call last): File "/opt/netbox/lib/python3.8/site-packages/django/template/base.py", line 870, in _resolve_lookup current = getattr(current, bit) File "/opt/netbox/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/opt/netbox/lib/python3.8/site-packages/django/forms/forms.py", line 455, in changed_data return [name for name, bf in self._bound_items() if bf._has_changed()] File "/opt/netbox/lib/python3.8/site-packages/django/forms/forms.py", line 455, in <listcomp> return [name for name, bf in self._bound_items() if bf._has_changed()] File "/opt/netbox/lib/python3.8/site-packages/django/forms/forms.py", line 154, in _bound_items yield name, self[name] File "/opt/netbox/lib/python3.8/site-packages/django/forms/forms.py", line 177, in __getitem__ bound_field = field.get_bound_field(self, name) File "/opt/netbox/netbox/utilities/forms/fields/dynamic.py", line 106, in get_bound_field widget.attrs['data-url'] = reverse(viewname) File "/opt/netbox/lib/python3.8/site-packages/django/urls/base.py", line 77, in reverse raise NoReverseMatch( django.urls.exceptions.NoReverseMatch: 'netbox_dns' is not a registered namespace inside 'plugins-api' Internal Server Error: /plugins/netbox-dns/records/ Traceback (most recent call last): File "/opt/netbox/lib/python3.8/site-packages/django/forms/forms.py", line 168, in __getitem__ field = self.fields[name] KeyError: 'changed_data' ``` The only place I tried to use `DynamicModelMultipleChoiceField`was here: ``` class RecordFilterForm(BootstrapMixin, forms.Form): """Form for filtering Record instances.""" model = Record q = CharField( required=False, widget=forms.TextInput(attrs={"placeholder": "Name, Zone or Value"}), label="Search", ) type = forms.MultipleChoiceField( choices=add_blank_choice(Record.CHOICES), required=False, widget=StaticSelectMultiple(), ) name = CharField( required=False, label="Name", ) value = CharField( required=False, label="Value", ) zone_id = DynamicModelMultipleChoiceField( queryset=Zone.objects.all(), required=False, label="Zone", ) tag = TagFilterField(Record) ``` Is anything wrong with the code, or is there still some problem with the `get_bound_field` method?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6156