From c0e7a7efd744a61eca99ca8361fe513f0d763490 Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 10 Jan 2022 20:39:56 +0000 Subject: [PATCH] . --- generic/forms.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/generic/forms.py b/generic/forms.py index 0ff170ac..15ccbbef 100755 --- a/generic/forms.py +++ b/generic/forms.py @@ -93,6 +93,14 @@ class QuestionNoteForm(ModelForm): class CidUserForm(ModelForm): def __init__(self, *args, **kwargs): + 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["physics_exams"] = [t.pk for t in kwargs["instance"].physics_exams.all()] + ModelForm.__init__(self, *args, **kwargs) super(CidUserForm, self).__init__(*args, **kwargs)