netbox icon indicating copy to clipboard operation
netbox copied to clipboard

Unable to set selector for user model in forms

Open abhi1693 opened this issue 1 year ago • 2 comments

Deployment Type

Self-hosted

NetBox Version

v3.7.2

Python Version

3.10

Steps to Reproduce

Inside a NetBoxModelForm, add a field for NetBoxUser with selector=True

class Form(NetBoxModelForm):
assigned_to = DynamicModelMultipleChoiceField(
        queryset=User.objects.all(),
        selector=True,
    )

Expected Behavior

Select search form should open up

Observed Behavior

"GET /htmx/object-selector/?_model=users.netboxuser&target=id_assigned_to HTTP/1.1" 200 5576
Internal Server Error: /htmx/object-selector/
Traceback (most recent call last):
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/utils/module_loading.py", line 30, in import_string
    return cached_import(module_path, class_name)
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/utils/module_loading.py", line 16, in cached_import
    return getattr(module, class_name)
AttributeError: module 'users.filtersets' has no attribute 'NetBoxUserFilterSet'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/asaharan/PycharmProjects/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 "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/asaharan/PycharmProjects/netbox/netbox/netbox/views/htmx.py", line 20, in get
    filterset = self._get_filterset_class(model)
  File "/home/asaharan/PycharmProjects/netbox/netbox/netbox/views/htmx.py", line 56, in _get_filterset_class
    return import_string(f'{app_label}.filtersets.{class_name}')
  File "/home/asaharan/PycharmProjects/netbox/venv/lib/python3.10/site-packages/django/utils/module_loading.py", line 32, in import_string
    raise ImportError(
ImportError: Module "users.filtersets" does not define a "NetBoxUserFilterSet" attribute/class
"GET /htmx/object-selector/?_model=users.netboxuser&_search=true&q=&is_active=&is_staff=&is_superuser= HTTP/1.1" 500 130748

abhi1693 avatar Feb 09 '24 17:02 abhi1693

@abhi1693 are you aware of the custom user model work being done for v4.0 under #12795? Can this issue wait until 4.0 so that we avoid a merge conflict?

jeffgdotorg avatar Feb 12 '24 16:02 jeffgdotorg

I'm aware but this is a simple fix and without this my plugins will not work on v3.7.x. Besides it doesn't change the functionality but makes the code consistent and I don't think it should create any conflict as only the class names have been fixed

abhi1693 avatar Feb 13 '24 04:02 abhi1693