Skip to content

Commit

Permalink
Investigate and fix N+1 query issue django#1806
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamatha1718 committed Dec 31, 2024
1 parent ba71219 commit 5b8516e
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ def index(request):
if data is None:
metrics = []
for MC in Metric.__subclasses__():
metrics.extend(MC.objects.filter(show_on_dashboard=True))
metrics.extend(MC.objects.filter(show_on_dashboard=True).prefetch_related("data"))
metrics = sorted(metrics, key=operator.attrgetter("display_position"))

data = []
for metric in metrics:
data.append({"metric": metric, "latest": metric.data.latest()})
latest_data = metric.data.latest()
data.append({"metric": metric, "latest": latest_data})
cache.set(key, data, 60 * 60, version=generation)

return render(request, "dashboard/index.html", {"data": data})
Expand Down

0 comments on commit 5b8516e

Please sign in to comment.