ArnesSI / netbox-inventory

Manage your hardware inventory in NetBox
MIT License
215 stars 21 forks source link

Server error when uploading a picture on Netbox #177

Closed shynuu closed 4 months ago

shynuu commented 5 months ago

Hi all,

First thanks for the wonderful plugin, it helps a lot to manage all the assets we have in our labs!

Since the update to version 4, we have this error everytime we want to upload a picture to assets.

image

Do you know where the issue could come from? A package conflict maybe?

Best,

sorano commented 5 months ago

I'm seeing the same error when trying to use the Attachments plugin together with Inventory plugin.



SerializerNotFound at /plugins/netbox-attachments/netbox-attachments/add/
Could not determine serializer for netbox_inventory.Purchase with prefix 'Nested'```
matejv commented 4 months ago

It seems this is an issue when using netbox-attachments plugin. Adding images to fields that are part of netbox-inventory plugin works fine.

When using netbox-attachments with the following config

PLUGINS_CONFIG = {
    'netbox_attachments': {
        'apps': ['dcim', 'netbox_inventory',],
        'display_setting': {
            'dcim.device': "right_page",
            "netbox_inventory.purchase": "right_page",
        },
    },

And trying to add a file to a Purchase instance results in the following error:

Environment:

Request Method: POST
Request URL: http://localhost:8000/plugins/netbox-attachments/netbox-attachments/add/?content_type=146&object_id=6&return_url=/plugins/inventory/purchases/6

Django Version: 5.0.7
Python Version: 3.12.3
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'django.forms',
 'corsheaders',
 'debug_toolbar',
 'django_filters',
 'django_htmx',
 'django_tables2',
 'django_prometheus',
 'strawberry_django',
 'mptt',
 'rest_framework',
 'social_django',
 'taggit',
 'timezone_field',
 'core',
 'account',
 'circuits',
 'dcim',
 'ipam',
 'extras',
 'tenancy',
 'users',
 'utilities',
 'virtualization',
 'vpn',
 'wireless',
 'django_rq',
 'drf_spectacular',
 'drf_spectacular_sidecar',
 'netbox_lists.ListsPluginConfig',
 'netbox_initializers.NetBoxInitializersConfig',
 'netbox_attachments.NetBoxAttachmentsConfig',
 'netbox_inventory.NetBoxInventoryConfig',
 'netbox_dns.DNSConfig']
Installed Middleware:
['strawberry_django.middlewares.debug_toolbar.DebugToolbarMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'django_htmx.middleware.HtmxMiddleware',
 'netbox.middleware.RemoteUserMiddleware',
 'netbox.middleware.CoreMiddleware',
 'netbox.middleware.MaintenanceModeMiddleware']

Traceback (most recent call last):
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/django/utils/module_loading.py", line 30, in import_string
    return cached_import(module_path, class_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/django/utils/module_loading.py", line 16, in cached_import
    return getattr(module, class_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^

The above exception (module 'netbox_inventory.api.serializers' has no attribute 'NestedPurchaseSerializer') was the direct cause of the following exception:
  File "/home/matej/temp/netbox-devel/netbox/netbox/utilities/api.py", line 36, in get_serializer_for_model
    return import_string(serializer_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/django/utils/module_loading.py", line 32, in import_string
    raise ImportError(
    ^^^^^^^^^^

During handling of the above exception (Module "netbox_inventory.api.serializers" does not define a "NestedPurchaseSerializer" attribute/class), another exception occurred:
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/netbox/netbox/views/generic/object_views.py", line 180, in dispatch
    return super().dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/netbox/netbox/views/generic/base.py", line 26, in dispatch
    return super().dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/netbox/utilities/views.py", line 113, in dispatch
    return super().dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/django/views/generic/base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/netbox/netbox/views/generic/object_views.py", line 274, in post
    obj = form.save()
          ^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/django/forms/models.py", line 552, in save
    self.instance.save()
    ^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/netbox_attachments/models.py", line 108, in save
    super().save(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/django/db/models/base.py", line 822, in save
    self.save_base(
    ^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/django/db/models/base.py", line 924, in save_base
    post_save.send(
    ^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/django/dispatch/dispatcher.py", line 189, in send
    response = receiver(signal=self, sender=sender, **named)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/netbox/extras/signals.py", line 109, in handle_changed_object
    enqueue_object(queue, instance, request.user, request.id, action)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/netbox/extras/events.py", line 74, in enqueue_object
    'data': serialize_for_event(instance),
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/netbox/extras/events.py", line 32, in serialize_for_event
    return serializer.data
           ^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/rest_framework/serializers.py", line 571, in data
    ret = super().data
          ^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/rest_framework/serializers.py", line 249, in data
    self._data = self.to_representation(self.instance)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/rest_framework/serializers.py", line 538, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/rest_framework/fields.py", line 1870, in to_representation
    return method(value)
           ^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/venv/lib/python3.12/site-packages/netbox_attachments/api/serializers.py", line 48, in get_parent
    serializer = get_serializer_for_model(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/temp/netbox-devel/netbox/netbox/utilities/api.py", line 38, in get_serializer_for_model
    raise SerializerNotFound(
    ^

Exception Type: SerializerNotFound at /plugins/netbox-attachments/netbox-attachments/add/
Exception Value: Could not determine serializer for netbox_inventory.Purchase with prefix 'Nested'

The issue is that netbox-attachments is searching for a serializer named Nested*. However the plugin migration guide for netbox 4 instructs plugin developers to remove nested serializers and cell regular serializers with brief argument (MySerializer(brief=True,...)).

Attaching files to netbox device works, because netbox core did not yet remove nested serializers (see https://github.com/netbox-community/netbox/issues/16638). However my understanding is that these are expected to be removed in the future.

Netbox own code internally moved to using serializers using brief argument, see https://github.com/netbox-community/netbox/commit/78e284c14f05eeb18d67ba90ed80a6b75e9d8cc6

I believe this should be fixed in netbox-attachment plugin. I'll open an issue there.

matejv commented 4 months ago

netbox-attachments version 5.1.0 was released that fixes this issue.