Skip to content

ValueError: Cannot query "<object_name>": Must be "Table4Model" instance when deleting Custom Object via UI #477

@FrantisekAdastra

Description

@FrantisekAdastra

Plugin Version

0.4.9

NetBox Version

4.5.7

Python Version

3.12.3

Steps to Reproduce

  1. Create a Custom Object Type with a multiobject field (e.g. VM Dependencies → Virtualization > Virtual Machine)
  2. Create any Custom Object of that type
  3. Navigate to the object's detail page
  4. Click the "Delete" button
Image

Expected Behavior

The object should be deleted successfully, or a confirmation page should be displayed before deletion.

Observed Behavior

A server error is raised immediately when clicking Delete:

<class 'ValueError'>
Cannot query "<object_name>": Must be "Table4Model" instance.

The error occurs in CustomObjectDeleteView when NetBox's _get_dependent_objects() uses Django's Collector to find related objects before showing the confirmation page. The Collector traverses M2M relations (e.g. multiobject fields) and passes the object's string representation instead of a proper model instance into the ORM query.

The object name in the error message always matches the name of the object being deleted, confirming the issue is in the delete view's dependent object resolution.

Workaround: Deletion works correctly via the Django shell:

from netbox_custom_objects.models import *
cot = CustomObjectType.objects.get(slug="<your-slug>")
model = cot.get_model()
obj = model.objects.get(id=<id>)
obj.delete()

Suggested fix — add _get_dependent_objects override to CustomObjectDeleteView in views.py:

def _get_dependent_objects(self, obj):
    try:
        return super()._get_dependent_objects(obj)
    except ValueError:
        return {}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions