Open tasddc1226 opened 2 years ago
if request.method == "POST":
total_questions = int(request.POST.get("form-TOTAL_FORMS"))
for i in range(0, total_questions):
answers = request.POST.getlist(f"form-{i}-choice")
if len(answers) > 1:
with transaction.atomic():
for choice_id in answers:
Answer.objects.create(
choice_id=choice_id, submission_id=sub_pk
)
else:
with transaction.atomic():
Answer.objects.create(
choice_id=answers[0], submission_id=sub_pk
)
sub.is_complete = True
sub.save()
return redirect("survey-thanks", pk=survey_pk)
form을 활용한 is_vaild 함수를 사용하지 않고 직관적이진 않지만 POST로 넘어온 모든 항목들에 대하여 응답 값이 복수개 즉, 1개 이상이라면 Checkbox에 대한 복수 응답이라고 판단하여 list의 요소를 순회하면서 값을 저장하도록 하였다.
transaction.atomic()
을 사용한 이유?
True
로 변경 후 응답.
RadioSelect
와Select
일 때에는 한 개씩 매칭이 되므로 위와 같이 테이블에 적재 가능.CheckboxSelectMultiple
인 경우 아래와 같이 선택된 데이터가 list 형태이다.