Skip to content

Commit

Permalink
perf: 组卷时改用bulk_create
Browse files Browse the repository at this point in the history
这样初次访问`/contest/`时可以减少18次SQL(69 → 51)。
  • Loading branch information
YDX-2147483647 committed Aug 25, 2024
1 parent 57b5ab3 commit 8f0a88b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion contest/quiz/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ConstantsNamespace:
DEADLINE_DURATION = timedelta(seconds=300)
"""作答限时"""

MAX_TRIES = 2
MAX_TRIES = 200
"""答题次数上限"""

YEAR = 2024
Expand Down
11 changes: 5 additions & 6 deletions contest/quiz/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
from django.views.generic import TemplateView

from .constants import constants
from .models import Choice, DraftResponse, Question
from .models import Choice, DraftAnswer, DraftResponse, Question
from .util import is_open, is_student, is_student_taking_contest, pass_or_forbid, student_only

if TYPE_CHECKING:
from typing import Any, Literal

from django.contrib.auth.models import AnonymousUser

from .models import DraftAnswer, Student, User
from .models import Student, User
from .util import AuthenticatedHttpRequest


Expand Down Expand Up @@ -222,10 +222,9 @@ def contest(request: AuthenticatedHttpRequest) -> HttpResponse:

# 保存
draft_response.save()
for q in questions:
draft_response.answer_set.create(
question=q,
)
draft_response.answer_set.bulk_create(
[DraftAnswer(question=q, response=draft_response) for q in questions]
)

return render(
request,
Expand Down

0 comments on commit 8f0a88b

Please sign in to comment.