NoReverseMatch exception for plugins with models without *_list views #9946

Closed
opened 2025-12-29 21:24:46 +01:00 by adam · 9 comments
Owner

Originally created by @jsenecal on GitHub (Jul 4, 2024).

Originally assigned to: @jsenecal, @jeremystretch on GitHub.

Deployment Type

Self-hosted

NetBox Version

v4.0.6

Python Version

3.10

Steps to Reproduce

A bit complex, let me know if you really want me to go through a thorough plugin example

  1. Create a plugin with a model related to Core models.
  2. Create a set of views and urls for that model, without creating any _list views.
  3. Register that plugin in your netbox instance
  4. Create and install required migrations

Expected Behavior

Related core model detail view should work as expected regardless of the missing _list view in the plugin.

Observed Behavior

Bug Introduced by 5353f83710

I have models with no *_list views related to core models in my plugin and they get picked up by get_related_models in GetRelatedModelsMixin which raises a NoReverseMatch exception in netbox/templates/inc/panels/related_objects.html

Technically get_related_models has been designed with the idea that one can set the omit method parameter to an iterable to avoid including these but there does not seem to have API which is exposed to plugin authors for Core models.

Originally created by @jsenecal on GitHub (Jul 4, 2024). Originally assigned to: @jsenecal, @jeremystretch on GitHub. ### Deployment Type Self-hosted ### NetBox Version v4.0.6 ### Python Version 3.10 ### Steps to Reproduce A bit complex, let me know if you really want me to go through a thorough plugin example 1. Create a plugin with a model related to Core models. 2. Create a set of views and urls for that model, without creating any `_list` views. 3. Register that plugin in your netbox instance 4. Create and install required migrations ### Expected Behavior Related core model detail view should work as expected regardless of the missing `_list` view in the plugin. ### Observed Behavior Bug Introduced by https://github.com/netbox-community/netbox/commit/5353f837108ee5db11537bc95dde3ce9bc4a042c I have models with no `*_list` views related to core models in my plugin and they get picked up by `get_related_models` in `GetRelatedModelsMixin` which raises a `NoReverseMatch` exception in `netbox/templates/inc/panels/related_objects.html` Technically `get_related_models` has been designed with the idea that one can set the `omit` method parameter to an iterable to avoid including these but there does not seem to have API which is exposed to plugin authors for Core models.
adam added the type: bugstatus: acceptedtopic: pluginsseverity: low labels 2025-12-29 21:24:46 +01:00
adam closed this issue 2025-12-29 21:24:46 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jul 4, 2024):

@jsenecal would you like to tackle this?

@jeremystretch commented on GitHub (Jul 4, 2024): @jsenecal would you like to tackle this?
Author
Owner

@jsenecal commented on GitHub (Jul 4, 2024):

Sure, I can give it a go. You can assign this to me.

I will propose a solution here first before submitting a PR.

@jsenecal commented on GitHub (Jul 4, 2024): Sure, I can give it a go. You can assign this to me. I will propose a solution here first before submitting a PR.
Author
Owner

@jeremystretch commented on GitHub (Jul 4, 2024):

It may be sufficient to just catch exceptions on view name resolution when determining related models.

@jeremystretch commented on GitHub (Jul 4, 2024): It may be sufficient to just catch exceptions on view name resolution when determining related models.
Author
Owner

@alehaa commented on GitHub (Jul 4, 2024):

I think catching the exception should be enough. Maybe we can remove the omit parameter then, as the models are listed explicitly to avoid that exception. Sorry for the inconvenience.

@alehaa commented on GitHub (Jul 4, 2024): I think catching the exception should be enough. Maybe we can remove the `omit` parameter then, as the models are listed explicitly to avoid that exception. Sorry for the inconvenience.
Author
Owner

@jsenecal commented on GitHub (Jul 5, 2024):

I think catching the exception should be enough. Maybe we can remove the omit parameter then, as the models are listed explicitly to avoid that exception. Sorry for the inconvenience.

The exception is generated when Django renders the template. We may need to resolve the view names earlier to see if they exist prior to passing these into the context.

@jsenecal commented on GitHub (Jul 5, 2024): > I think catching the exception should be enough. Maybe we can remove the `omit` parameter then, as the models are listed explicitly to avoid that exception. Sorry for the inconvenience. The exception is generated when Django renders the template. We may need to resolve the view names earlier to see if they exist prior to passing these into the context.
Author
Owner

@jsenecal commented on GitHub (Jul 5, 2024):

So I've designed two fixes to this:

  1. Use the validated_viewname filter instead of the viewname one:
    https://github.com/jsenecal/netbox/compare/develop...related_objects-template-fix

    Pros: Simple, effective
    Cons: Not really reusable for other dynamic functionalities in the future

  2. Modify get_related_models to try to resolve urls at this point and omit the relationship if it cannot be resolved.
    https://github.com/jsenecal/netbox/compare/develop...related_objects-utilities-fix

    Pros: More targeted fix, allows for reuse of code
    Cons: Moves a bit more code around

In both cases omit could be removed everywhere but it does provide some performance improvements for netbox native models.

@jeremystretch let me know which approach you prefer and I'll prepare a proper PR.

@jsenecal commented on GitHub (Jul 5, 2024): So I've designed two fixes to this: 1. Use the `validated_viewname` filter instead of the `viewname` one: https://github.com/jsenecal/netbox/compare/develop...related_objects-template-fix Pros: Simple, effective Cons: Not really reusable for other dynamic functionalities in the future 2. Modify `get_related_models` to try to resolve urls at this point and omit the relationship if it cannot be resolved. https://github.com/jsenecal/netbox/compare/develop...related_objects-utilities-fix Pros: More targeted fix, allows for reuse of code Cons: Moves a bit more code around In both cases `omit` could be removed everywhere but it does provide some performance improvements for netbox native models. @jeremystretch let me know which approach you prefer and I'll prepare a proper PR.
Author
Owner

@jsenecal commented on GitHub (Jul 10, 2024):

@jeremystretch any chance you could look into my proposed fixes?

@jsenecal commented on GitHub (Jul 10, 2024): @jeremystretch any chance you could look into my proposed fixes?
Author
Owner

@jsenecal commented on GitHub (Jul 29, 2024):

@arthanson @DanSheps (Sorry for spam-tagging), opinions on the above?
This bug is really causing me headaches and preventing my plugins to work on the newer releases of netbox; I just need to know which fix to submit a PR for.

Thanks

@jsenecal commented on GitHub (Jul 29, 2024): @arthanson @DanSheps (Sorry for spam-tagging), opinions on the above? This bug is really causing me headaches and preventing my plugins to work on the newer releases of netbox; I just need to know which fix to submit a PR for. Thanks
Author
Owner

@jeremystretch commented on GitHub (Aug 27, 2024):

@jsenecal sorry for the delay; we've been very backed up. I took the liberty of opening a PR for your first solution above. LGTM.

@jeremystretch commented on GitHub (Aug 27, 2024): @jsenecal sorry for the delay; we've been very backed up. I took the liberty of opening a PR for your first solution above. LGTM.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9946