From 4c7d5855a7d48a19b24f5d93541a96d3d009c56b Mon Sep 17 00:00:00 2001 From: Benni Baermann Date: Mon, 5 Oct 2020 15:10:23 +0200 Subject: [PATCH] conditional form: detection of choice works, IFGForm not. --- input/forms.py | 2 +- input/views.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/input/forms.py b/input/forms.py index 47cd3e0..9ecc8c9 100644 --- a/input/forms.py +++ b/input/forms.py @@ -31,4 +31,4 @@ class LibraryForm(ModelForm): class IFGForm(ModelForm): class Meta: model = IFG - fields = '__all__' + exclude = ('realname', 'email', 'username') diff --git a/input/views.py b/input/views.py index e6a2ee1..a117258 100644 --- a/input/views.py +++ b/input/views.py @@ -3,7 +3,7 @@ from django.forms import modelformset_factory from django.http import HttpResponse from formtools.wizard.views import CookieWizardView -from .forms import ProjectForm, VolunteerForm, LibraryForm +from .forms import ProjectForm, VolunteerForm, LibraryForm, IFGForm from .models import Project def intern(request): @@ -34,6 +34,13 @@ class ExternView(CookieWizardView): template_name = "input/extern.html" form_list = [VolunteerForm, LibraryForm] + def process_step(self, form): + if form.cleaned_data.get('choice') == 'IFG': + print ('IFG detected!') + self.form_list = [VolunteerForm, IFGForm] + print('leaving process_step()') + return self.get_form_step_data(form) + def done(self, form_list, **kwargs): print('ExternView.done() reached') # gather data from all forms @@ -41,9 +48,11 @@ class ExternView(CookieWizardView): for form in form_list: data = {**data, **form.cleaned_data} print(data) + # write data to database form = form.save(commit=False) # this is ugly code. how can we copy this without explicit writing? + # i found no way to access the ModelForm.Meta.exclude-array form.realname = data['realname'] form.username = data['username'] form.email = data['email']