mirror of
https://github.com/Southampton-RSG/breccia-mapper.git
synced 2026-03-03 11:27:09 +00:00
feat: allow multiple choice questions in forms
This commit is contained in:
@@ -49,10 +49,17 @@ class DynamicAnswerSetBase(forms.Form):
|
|||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
for question in self.question_model.objects.all():
|
for question in self.question_model.objects.all():
|
||||||
field = self.field_class(label=question,
|
field_class = self.field_class
|
||||||
queryset=question.answers,
|
field_widget = self.field_widget
|
||||||
widget=self.field_widget,
|
|
||||||
required=self.field_required)
|
if question.is_multiple_choice:
|
||||||
|
field_class = forms.ModelMultipleChoiceField
|
||||||
|
field_widget = Select2MultipleWidget
|
||||||
|
|
||||||
|
field = field_class(label=question,
|
||||||
|
queryset=question.answers,
|
||||||
|
widget=field_widget,
|
||||||
|
required=self.field_required)
|
||||||
self.fields['question_{}'.format(question.pk)] = field
|
self.fields['question_{}'.format(question.pk)] = field
|
||||||
|
|
||||||
|
|
||||||
@@ -92,7 +99,12 @@ class PersonAnswerSetForm(forms.ModelForm, DynamicAnswerSetBase):
|
|||||||
# Save answers to relationship questions
|
# Save answers to relationship questions
|
||||||
for key, value in self.cleaned_data.items():
|
for key, value in self.cleaned_data.items():
|
||||||
if key.startswith('question_') and value:
|
if key.startswith('question_') and value:
|
||||||
self.instance.question_answers.add(value)
|
try:
|
||||||
|
self.instance.question_answers.add(value)
|
||||||
|
|
||||||
|
except TypeError:
|
||||||
|
# Value is a QuerySet - multiple choice question
|
||||||
|
self.instance.question_answers.add(*value.all())
|
||||||
|
|
||||||
return self.instance
|
return self.instance
|
||||||
|
|
||||||
@@ -119,7 +131,12 @@ class RelationshipAnswerSetForm(forms.ModelForm, DynamicAnswerSetBase):
|
|||||||
# Save answers to relationship questions
|
# Save answers to relationship questions
|
||||||
for key, value in self.cleaned_data.items():
|
for key, value in self.cleaned_data.items():
|
||||||
if key.startswith('question_') and value:
|
if key.startswith('question_') and value:
|
||||||
self.instance.question_answers.add(value)
|
try:
|
||||||
|
self.instance.question_answers.add(value)
|
||||||
|
|
||||||
|
except TypeError:
|
||||||
|
# Value is a QuerySet - multiple choice question
|
||||||
|
self.instance.question_answers.add(*value.all())
|
||||||
|
|
||||||
return self.instance
|
return self.instance
|
||||||
|
|
||||||
|
|||||||
23
people/migrations/0027_multiple_choice_questions.py
Normal file
23
people/migrations/0027_multiple_choice_questions.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# Generated by Django 2.2.10 on 2020-12-07 16:39
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('people', '0026_move_static_person_questions'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='personquestion',
|
||||||
|
name='is_multiple_choice',
|
||||||
|
field=models.BooleanField(default=False),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='relationshipquestion',
|
||||||
|
name='is_multiple_choice',
|
||||||
|
field=models.BooleanField(default=False),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -22,6 +22,11 @@ class Question(models.Model):
|
|||||||
#: Text of question
|
#: Text of question
|
||||||
text = models.CharField(max_length=255, blank=False, null=False)
|
text = models.CharField(max_length=255, blank=False, null=False)
|
||||||
|
|
||||||
|
#: Should people be able to select multiple responses to this question?
|
||||||
|
is_multiple_choice = models.BooleanField(default=False,
|
||||||
|
blank=False,
|
||||||
|
null=False)
|
||||||
|
|
||||||
#: Position of this question in the list
|
#: Position of this question in the list
|
||||||
order = models.SmallIntegerField(default=0, blank=False, null=False)
|
order = models.SmallIntegerField(default=0, blank=False, null=False)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user