From b37fc5ea6fd763be2acbd0513019e8b753dd52d5 Mon Sep 17 00:00:00 2001 From: Ross Date: Tue, 1 Mar 2022 19:01:08 +0000 Subject: [PATCH] . --- generic/views.py | 3 ++- rapids/forms.py | 30 +++++++++++++++--------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/generic/views.py b/generic/views.py index 81e73bcf..37ade9f3 100644 --- a/generic/views.py +++ b/generic/views.py @@ -195,7 +195,8 @@ def generic_exam_json_edit(request): add_count += 1 exam.exam_questions.add(i) - exam.save() + if add_count > 0: + exam.save() data = {"status": "success", "questions added": add_count} return JsonResponse(data, status=200) diff --git a/rapids/forms.py b/rapids/forms.py index 35c00b99..c9b752c8 100755 --- a/rapids/forms.py +++ b/rapids/forms.py @@ -80,13 +80,13 @@ class RapidForm(ModelForm): self.user = kwargs.pop( "user" ) # To get request.user. Do not use kwargs.pop('user', None) due to potential security hole - if kwargs.get("instance"): - # We get the 'initial' keyword argument or initialize it - # as a dict if it didn't exist. - initial = kwargs.setdefault("initial", {}) - # The widget for a ModelMultipleChoiceField expects - # a list of primary key for the selected data. - initial["exams"] = [t.pk for t in kwargs["instance"].exams.all()] + #if kwargs.get("instance"): + # # We get the 'initial' keyword argument or initialize it + # # as a dict if it didn't exist. + # initial = kwargs.setdefault("initial", {}) + # # The widget for a ModelMultipleChoiceField expects + # # a list of primary key for the selected data. + # initial["exams"] = [t.pk for t in kwargs["instance"].exams.all()] ModelForm.__init__(self, *args, **kwargs) super(RapidForm, self).__init__(*args, **kwargs) @@ -132,16 +132,16 @@ class RapidForm(ModelForm): instance.save() self.save_m2m() - old_exams = instance.exams.all() + #old_exams = instance.exams.all() - new_exams = self.cleaned_data["exams"] + #new_exams = self.cleaned_data["exams"] - for exam in old_exams: - if exam in new_exams: - exam.exam_questions.add(instance) - else: - exam.exam_questions.remove(instance) - exam.save() + #for exam in old_exams: + # if exam in new_exams: + # exam.exam_questions.add(instance) + # else: + # exam.exam_questions.remove(instance) + # exam.save() # # Prepare a 'save_m2m' method for the form, # old_save_m2m = self.save_m2m