Repository: netbox-community/netbox

Issue:
Dynamic choice fields don't support plugin API endpoints
### 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
```


Discussion:
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?

Resolving patch:
diff --git a/netbox/utilities/utils.py b/netbox/utilities/utils.py
--- a/netbox/utilities/utils.py
+++ b/netbox/utilities/utils.py
@@ -31,7 +31,7 @@ def get_viewname(model, action=None, rest_api=False):
 
     if rest_api:
         if is_plugin:
-            viewname = f'plugins-api:{app_label}:{model_name}'
+            viewname = f'plugins-api:{app_label}-api:{model_name}'
         else:
             viewname = f'{app_label}-api:{model_name}'
         # Append the action, if any


Test patch: