When inheriting NetBoxModelSerializer without defining an override for the url attribute, an exception is thrown #10244

Closed
opened 2025-12-29 21:28:48 +01:00 by adam · 1 comment
Owner

Originally created by @DanSheps on GitHub (Sep 13, 2024).

Originally assigned to: @DanSheps on GitHub.

Deployment Type

Self-hosted

NetBox Version

4.1.1

Python Version

3.12

Steps to Reproduce

  1. Create a plugin (per the plugin docs)
  2. Add plugin to NetBox
  3. View plugin's API where url is defined as a field but not overridden

Expected Behavior

url will have a proper link

Observed Behavior

Exception: ImproperlyConfigured at

Originally created by @DanSheps on GitHub (Sep 13, 2024). Originally assigned to: @DanSheps on GitHub. ### Deployment Type Self-hosted ### NetBox Version 4.1.1 ### Python Version 3.12 ### Steps to Reproduce 1. Create a plugin (per the plugin docs) 2. Add plugin to NetBox 3. View plugin's API where `url` is defined as a field but not overridden ### Expected Behavior `url` will have a proper link ### Observed Behavior Exception: ImproperlyConfigured at <x>
adam added the type: bugstatus: acceptedseverity: low labels 2025-12-29 21:28:48 +01:00
adam closed this issue 2025-12-29 21:28:48 +01:00
Author
Owner

@DanSheps commented on GitHub (Sep 13, 2024):

The root cause is by default NetBoxAPIHyperlinkedIdentityField and NetBoxURLHyperlinkedIdentityField use a static definition for the defining the path.

Proposed fix would be to make the following changes:

class NetBoxAPIHyperlinkedIdentityField(BaseNetBoxHyperlinkedIdentityField):

-    def get_view_name(self, app_name, model_name):
-        return f'{app_name}-api:{model_name}-detail'
+    def get_view_name(self, model):
+       return get_viewname(model=model, action='detail', rest_api=True)
class NetBoxURLHyperlinkedIdentityField(BaseNetBoxHyperlinkedIdentityField):

-    def get_view_name(self, app_name, model_name):
-        return f'{app_name}:{model_name}'
+    def get_view_name(self, app_name, model_name):
+       return get_viewname(model=model)
class BaseNetBoxHyperlinkedIdentityField(serializers.HyperlinkedIdentityField):

-        model_name = self.parent.Meta.model._meta.model_name
-        app_name = self.parent.Meta.model._meta.app_label
-        view_name = self.get_view_name(app_name, model_name)
+        view_name = self.get_view_name(obj)
@DanSheps commented on GitHub (Sep 13, 2024): The root cause is by default `NetBoxAPIHyperlinkedIdentityField` and `NetBoxURLHyperlinkedIdentityField` use a static definition for the defining the path. Proposed fix would be to make the following changes: ```diff class NetBoxAPIHyperlinkedIdentityField(BaseNetBoxHyperlinkedIdentityField): - def get_view_name(self, app_name, model_name): - return f'{app_name}-api:{model_name}-detail' + def get_view_name(self, model): + return get_viewname(model=model, action='detail', rest_api=True) ``` ```diff class NetBoxURLHyperlinkedIdentityField(BaseNetBoxHyperlinkedIdentityField): - def get_view_name(self, app_name, model_name): - return f'{app_name}:{model_name}' + def get_view_name(self, app_name, model_name): + return get_viewname(model=model) ``` ```diff class BaseNetBoxHyperlinkedIdentityField(serializers.HyperlinkedIdentityField): - model_name = self.parent.Meta.model._meta.model_name - app_name = self.parent.Meta.model._meta.app_label - view_name = self.get_view_name(app_name, model_name) + view_name = self.get_view_name(obj) ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#10244