Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jeremystretch opened this issue Feb 28, 2022 · 1 comment
Closed

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

jeremystretch opened this issue Feb 28, 2022 · 1 comment
Labels
beta Concerns a bug/feature in a beta release status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@jeremystretch
Copy link
Member

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
@jeremystretch jeremystretch added type: bug A confirmed report of unexpected behavior in the application status: accepted This issue has been accepted for implementation beta Concerns a bug/feature in a beta release labels Feb 28, 2022
@peteeckel
Copy link
Contributor

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 added a commit to peteeckel/netbox that referenced this issue Mar 1, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
beta Concerns a bug/feature in a beta release status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

No branches or pull requests

2 participants