Define a get_absolute_url() method on NetBoxModel #9838

Closed
opened 2025-12-29 21:23:25 +01:00 by adam · 2 comments
Owner

Originally created by @jeremystretch on GitHub (Jun 13, 2024).

Originally assigned to: @arthanson on GitHub.

Proposed Changes

Add a get_absolute_url() method on NetBoxModel to dynamically return the appropriate URL based on the model's app_label and model_name Meta attributes:

    def get_absolute_url(self):
        return reverse(f'{self._meta.app_label}:{model_name}', args=[self.pk])

Justification

The vast majority of models in NetBox declare this method explicitly. Moving it to NetBoxModel allows us to remove boilerplate code while still permitting customization on individual models.

Originally created by @jeremystretch on GitHub (Jun 13, 2024). Originally assigned to: @arthanson on GitHub. ### Proposed Changes Add a [`get_absolute_url()`](https://docs.djangoproject.com/en/5.0/ref/models/instances/#get-absolute-url) method on NetBoxModel to dynamically return the appropriate URL based on the model's `app_label` and `model_name` Meta attributes: ```python def get_absolute_url(self): return reverse(f'{self._meta.app_label}:{model_name}', args=[self.pk]) ``` ### Justification The vast majority of models in NetBox declare this method explicitly. Moving it to NetBoxModel allows us to remove boilerplate code while still permitting customization on individual models.
adam added the status: acceptedtype: housekeeping labels 2025-12-29 21:23:25 +01:00
adam closed this issue 2025-12-29 21:23:25 +01:00
Author
Owner

@sleepinggenius2 commented on GitHub (Jun 14, 2024):

Would the proposed solution work for plugin models that inherit from NetBoxModel too? It would be nice to do away with the boilerplate there as well.

@sleepinggenius2 commented on GitHub (Jun 14, 2024): Would the proposed solution work for plugin models that inherit from NetBoxModel too? It would be nice to do away with the boilerplate there as well.
Author
Owner

@jeremystretch commented on GitHub (Nov 25, 2024):

A get_absolute_url() method has been added to the NetBoxFeatureSet mixin, which is used by NetBoxModel, OrganizationalModel, and NestedGroupModel.

@jeremystretch commented on GitHub (Nov 25, 2024): A `get_absolute_url()` method has been added to the NetBoxFeatureSet mixin, which is used by NetBoxModel, OrganizationalModel, and NestedGroupModel.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9838