-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
15794 Make "related objects" dynamic #15876
base: develop
Are you sure you want to change the base?
15794 Make "related objects" dynamic #15876
Conversation
Instead of hardcoding relationships between models for the detail view, they are now dynamically generated.
Instead of providing a rarely used hook method, additional related models can now be passed directly to the lookup method.
'related_models': self.get_related_models( | ||
request, | ||
instance, | ||
[CircuitTermination], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be able to include circuit terminations now that #15496 has been completed.
request, | ||
instance, | ||
[CableTermination, CircuitTermination], | ||
( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we need to pull in ASNs too.
return { | ||
'related_models': related_models, | ||
'related_models': self.get_related_models(request, instance, [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Including the keyword improves clarity.
'related_models': self.get_related_models(request, instance, [ | |
'related_models': self.get_related_models(request, instance, omit=[ |
return { | ||
'related_models': related_models, | ||
'related_models': self.get_related_models(request, instance, [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'related_models': self.get_related_models(request, instance, [ | |
'related_models': self.get_related_models(request, instance, omit=[ |
@@ -52,7 +47,7 @@ def get_extra_context(self, request, instance): | |||
) | |||
|
|||
return { | |||
'related_models': related_models, | |||
'related_models': self.get_related_models(request, instance), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This returns two entries for "Interfaces," because we're querying both device and VM interfaces. The simplest solution would be to exclude both, which IMO is reasonable.
return { | ||
'related_models': related_models, | ||
'related_models': self.get_related_models(request, [instance]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Site and Provider need to be pulled explicitly as they are both M2M relationships.
Fixes: #15794
Instead of hardcoding relationships between models for the detail view, they are now dynamically generated.
Known limitations
If a related model doesn't have a
list
view, rendering of the related objects pane will fail. This limitation was already present before, but related models listed were explicitly chosen to do so. For all views shipped with NetBox I've added a filter if needed (omit
parameter). However, external plugins might trigger this unintentionally, when named view is not available, but the relationship has not been setup withrelated_name='+'
parameter. Switching toverified_view
in the panel's template should resolve this, but might decrease performance slightly.If a model introduces several relations to another model, they will all be listed with the same model name but different URL query parameters. This is not the case for models included in NetBox. As before, plugin developers can still use an extra panel for this special purpose and extra context about the different types of relationships.