Skip to content

Commit

Permalink
Avoid N+1 queries in RI list .to_json()
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Nov 8, 2024
1 parent 46bcf5b commit 05b4f94
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions arches/app/datatypes/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2367,17 +2367,25 @@ def to_json(self, tile, node):
nodevalue = self.get_nodevalues(data[str(node.nodeid)])
items = []

other_resource_ids: set[uuid.UUID] = set()
for resourceXresource in nodevalue:
try:
resourceid = resourceXresource["resourceId"]
related_resource = Resource.objects.get(pk=resourceid)
displayname = related_resource.displayname()
resourceXresource["display_value"] = displayname
items.append(resourceXresource)
except (TypeError, KeyError):
other_resource_ids.add(uuid.UUID(resourceXresource["resourceId"]))
except (TypeError, ValueError, KeyError):
pass
except:
other_resources = Resource.objects.filter(pk__in=other_resource_ids)
for resourceid in other_resource_ids:
for candidate in other_resources:
if candidate.pk == resourceid:
related_resource = candidate
break
else:
logger.info(f'Resource with id "{resourceid}" not in the system.')
continue
displayname = related_resource.displayname()
resourceXresource["display_value"] = displayname
items.append(resourceXresource)

return self.compile_json(tile, node, instance_details=items)

def collects_multiple_values(self):
Expand Down

0 comments on commit 05b4f94

Please sign in to comment.