Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions netbox_custom_objects/template_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,34 @@ class CustomObjectLink(PluginTemplateExtension):

def left_page(self):
# TODO: Improve performance of these nested queries
content_type = ContentType.objects.get_for_model(self.context['object']._meta.model)
custom_object_type_fields = CustomObjectTypeField.objects.filter(related_object_type=content_type)
content_type = ContentType.objects.get_for_model(
self.context["object"]._meta.model
)
custom_object_type_fields = CustomObjectTypeField.objects.filter(
related_object_type=content_type
)
linked_custom_objects = []
for field in custom_object_type_fields:
model = field.custom_object_type.get_model()
for model_object in model.objects.all():
model_field = getattr(model_object, field.name)
if field.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT:
if model_field.filter(id=self.context["object"].pk).exists():
linked_custom_objects.append(LinkedCustomObject(custom_object=model_object, field=field))
else:
if model_field.id == self.context["object"].pk:
linked_custom_objects.append(LinkedCustomObject(custom_object=model_object, field=field))
return render_jinja2("""
if model_field:
if field.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT:
if model_field.filter(id=self.context["object"].pk).exists():
linked_custom_objects.append(
LinkedCustomObject(
custom_object=model_object, field=field
)
)
else:
if model_field.id == self.context["object"].pk:
linked_custom_objects.append(
LinkedCustomObject(
custom_object=model_object, field=field
)
)
return render_jinja2(
"""
<div class="card">
<h2 class="card-header">Custom Objects linking to this object</h2>
<table class="table table-hover attr-table">
Expand All @@ -72,7 +86,9 @@ def left_page(self):
{% endfor %}
</table>
</div>
""", {'linked_custom_objects': linked_custom_objects})
""",
{"linked_custom_objects": linked_custom_objects},
)


template_extensions = (
Expand Down