conditional form: detection of choice works, IFGForm not.

This commit is contained in:
Benni Bärmann 2020-10-05 15:10:23 +02:00
parent c25d409a11
commit 4c7d5855a7
2 changed files with 11 additions and 2 deletions

View File

@ -31,4 +31,4 @@ class LibraryForm(ModelForm):
class IFGForm(ModelForm): class IFGForm(ModelForm):
class Meta: class Meta:
model = IFG model = IFG
fields = '__all__' exclude = ('realname', 'email', 'username')

View File

@ -3,7 +3,7 @@ from django.forms import modelformset_factory
from django.http import HttpResponse from django.http import HttpResponse
from formtools.wizard.views import CookieWizardView from formtools.wizard.views import CookieWizardView
from .forms import ProjectForm, VolunteerForm, LibraryForm from .forms import ProjectForm, VolunteerForm, LibraryForm, IFGForm
from .models import Project from .models import Project
def intern(request): def intern(request):
@ -34,6 +34,13 @@ class ExternView(CookieWizardView):
template_name = "input/extern.html" template_name = "input/extern.html"
form_list = [VolunteerForm, LibraryForm] 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): def done(self, form_list, **kwargs):
print('ExternView.done() reached') print('ExternView.done() reached')
# gather data from all forms # gather data from all forms
@ -41,9 +48,11 @@ class ExternView(CookieWizardView):
for form in form_list: for form in form_list:
data = {**data, **form.cleaned_data} data = {**data, **form.cleaned_data}
print(data) print(data)
# write data to database # write data to database
form = form.save(commit=False) form = form.save(commit=False)
# this is ugly code. how can we copy this without explicit writing? # 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.realname = data['realname']
form.username = data['username'] form.username = data['username']
form.email = data['email'] form.email = data['email']