refactor: add person update form fields to initial

This commit is contained in:
James Graham
2021-01-15 17:45:55 +00:00
parent f94627e4c8
commit 4cfee6362d
3 changed files with 64 additions and 12 deletions

View File

@@ -1,6 +1,4 @@
"""
Forms for creating / updating models belonging to the 'people' app.
"""
"""Forms for creating / updating models belonging to the 'people' app."""
import typing
@@ -28,11 +26,7 @@ class OrganisationForm(forms.ModelForm):
"""Form for creating / updating an instance of :class:`Organisation`."""
class Meta:
model = models.Organisation
fields = [
'name',
'latitude',
'longitude'
]
fields = ['name', 'latitude', 'longitude']
class PersonForm(forms.ModelForm):
@@ -59,6 +53,8 @@ class DynamicAnswerSetBase(forms.Form):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
initial = kwargs.get('initial', {})
for question in self.question_model.objects.all():
field_class = self.field_class
field_widget = self.field_widget
@@ -67,11 +63,14 @@ class DynamicAnswerSetBase(forms.Form):
field_class = forms.ModelMultipleChoiceField
field_widget = Select2MultipleWidget
field_name = f'question_{question.pk}'
field = field_class(label=question,
queryset=question.answers,
widget=field_widget,
required=self.field_required)
self.fields['question_{}'.format(question.pk)] = field
required=self.field_required,
initial=initial.get(field_name, None))
self.fields[field_name] = field
class PersonAnswerSetForm(forms.ModelForm, DynamicAnswerSetBase):