netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
16k stars 2.57k forks source link

StagedChange: SerializerNotFound #13640

Closed minitriga closed 10 months ago

minitriga commented 1 year ago

NetBox version

v3.5.8

Python version

3.8

Steps to Reproduce

Create a staged change and delete via the UI in a plugin. There is no UI for the StagedChange model but when using it in a plugin and deleting a staged change the following is raised: netbox.api.exceptions.SerializerNotFound: Could not determine serializer for extras.StagedChange with prefix ''

Expected Behavior

I expect that the deletion of a branch or staged change would not fail. I think this is due to the model has the following mixin ChangeLoggedModel.

Observed Behavior

extras.api.serializers.StagedChangeSerializer
Internal Server Error: /plugins/ipfabric/sync/12/delete/
Traceback (most recent call last):
  File "/opt/netbox/netbox/utilities/api.py", line 33, in get_serializer_for_model
    return dynamic_import(serializer_name)
  File "/opt/netbox/netbox/utilities/utils.py", line 126, in dynamic_import
    mod = getattr(mod, comp)
AttributeError: module 'extras.api.serializers' has no attribute 'StagedChangeSerializer'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 56, in inner
    response = get_response(request)
  File "/opt/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 "/opt/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/netbox/netbox/netbox/views/generic/base.py", line 26, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/opt/netbox/netbox/utilities/views.py", line 99, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 142, in dispatch
    return handler(request, *args, **kwargs)
  File "/opt/netbox/netbox/netbox/views/generic/object_views.py", line 375, in post
    obj.delete()
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/base.py", line 1118, in delete
    return collector.delete()
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/deletion.py", line 458, in delete
    signals.pre_delete.send(
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 176, in send
    return [
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/opt/netbox/netbox/extras/signals.py", line 118, in handle_deleted_object
    enqueue_object(queue, instance, request.user, request.id, ObjectChangeActionChoices.ACTION_DELETE)
  File "/opt/netbox/netbox/extras/webhooks.py", line 73, in enqueue_object
    'data': serialize_for_webhook(instance),
  File "/opt/netbox/netbox/extras/webhooks.py", line 22, in serialize_for_webhook
    serializer_class = get_serializer_for_model(instance.__class__)
  File "/opt/netbox/netbox/utilities/api.py", line 35, in get_serializer_for_model
    raise SerializerNotFound(
netbox.api.exceptions.SerializerNotFound: Could not determine serializer for extras.StagedChange with prefix ''
minitriga commented 1 year ago

I can implement some serializers for Branch and StagedChange models.

minitriga commented 1 year ago

Having looked how many staged changes there can be I dont think its worth having change ChangeLoggedModel on it. For example when ingesting some customer information using the staging method there can be up to 100k+ objects being altered. Deleting these takes very long due to the ChangeLoggedModel process.

arthanson commented 1 year ago

@minitriga the SerializerNotFound can remain in this issue, for removing ChangeLoggedModel on StagedChange - that should be opened as a feature-request as it will cause database changes and need to go into a point release.

arthanson commented 10 months ago

@minitriga creating new deprecation #14424 as this removes functionality it would need to be discussed as a deprecation. Marking this as blocked on that issue. Blocked by #14424

arthanson commented 10 months ago

This is fixed via #14424 so closing.

minitriga commented 9 months ago

@arthanson This looks to be still broken in 3.7 I have upgraded and I get the same issue:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
January 10, 2024 - 15:39:10
Django version 4.2.8, using settings 'netbox.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

[10/Jan/2024 15:39:17] "GET /plugins/ipfabric/sync/1/ HTTP/1.1" 200 112087
[10/Jan/2024 15:39:19] "GET /plugins/ipfabric/sync/1/delete/ HTTP/1.1" 200 7065
[10/Jan/2024 15:39:19] "GET /__debug__/history_sidebar/?store_id=58e3fd2b7f8444b29927866e81aba754 HTTP/1.1" 200 9502
Internal Server Error: /plugins/ipfabric/sync/1/delete/
Traceback (most recent call last):
  File "/opt/netbox/netbox/utilities/api.py", line 32, in get_serializer_for_model
    return dynamic_import(serializer_name)
  File "/opt/netbox/netbox/utilities/utils.py", line 126, in dynamic_import
    mod = getattr(mod, comp)
AttributeError: module 'extras.api.serializers' has no attribute 'StagedChangeSerializer'. Did you mean: 'ObjectChangeSerializer'?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/opt/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 "/opt/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/netbox/netbox/netbox/views/generic/base.py", line 26, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/opt/netbox/netbox/utilities/views.py", line 99, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/views/generic/base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
  File "/opt/netbox/netbox/netbox/views/generic/object_views.py", line 420, in post
    obj.delete()
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/base.py", line 1132, in delete
    return collector.delete()
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/db/models/deletion.py", line 463, in delete
    signals.pre_delete.send(
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 176, in send
    return [
  File "/opt/netbox/venv/lib/python3.10/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/opt/netbox/netbox/extras/signals.py", line 125, in handle_deleted_object
    enqueue_object(queue, instance, request.user, request.id, ObjectChangeActionChoices.ACTION_DELETE)
  File "/opt/netbox/netbox/extras/events.py", line 67, in enqueue_object
    'data': serialize_for_event(instance),
  File "/opt/netbox/netbox/extras/events.py", line 28, in serialize_for_event
    serializer_class = get_serializer_for_model(instance.__class__)
  File "/opt/netbox/netbox/utilities/api.py", line 34, in get_serializer_for_model
    raise SerializerNotFound(
netbox.api.exceptions.SerializerNotFound: Could not determine serializer for extras.StagedChange with prefix ''
[10/Jan/2024 15:39:20] "POST /plugins/ipfabric/sync/1/delete/ HTTP/1.1" 500 172606
(venv) root@asd:/opt/netbox/netbox# python3 manage.py nbshell
### NetBox interactive shell (sa-alex-gittings-netboxdev)
### Python 3.10.12 | Django 4.2.8 | NetBox 3.7.0
### lsmodels() will show available models. Use help(<model>) for more info.
>>> from django.conf import settings
>>> settings.VERSION
'3.7.0'