Skip to content

Commit

Permalink
perf: /contest/预先查询answer_set
Browse files Browse the repository at this point in the history
这样初次访问`/contest/`时可以减少20次SQL(51 → 31)。
  • Loading branch information
YDX-2147483647 committed Aug 25, 2024
1 parent ddeb964 commit ac6247a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contest/quiz/templates/contest.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<article class="mx-auto max-w-2xl pb-6 prose">
<form action="{% url 'quiz:contest_submit' %}" method="post">
{% csrf_token %}
{% for a in draft_response.answer_set.all %}
{% for a in answer_set.all %}
<fieldset class="my-4 p-4 sm:px-6 lg:px-8 bg-white shadow disabled:text-gray-400"
{% if draft_response.outdated %}disabled{% endif %}>
<p class="text-lg my-0">
Expand Down
2 changes: 2 additions & 0 deletions contest/quiz/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ def contest(request: AuthenticatedHttpRequest) -> HttpResponse:
"contest.html",
{
"draft_response": draft_response,
# 为渲染模板预先从数据库查询相关内容
"answer_set": draft_response.answer_set.select_related('question', 'choice'),
"constants": constants,
},
)
Expand Down

0 comments on commit ac6247a

Please sign in to comment.