1

我正在重写我们应用程序的一大块,它需要用户创建一个附加到它Project的应用程序。Rewards

表单分为不同的步骤,前两个是正常Project的,下一个是Rewards,最后是一个简单的预览,让用户来回滑动以创建一个完美的Project

我的表格.py

class BaseRewardFormSet(BaseModelFormSet):
    def __init__(self, *args, **kwargs):
        super(BaseRewardFormSet, self).__init__(*args, **kwargs)
        self.queryset = Reward.objects.none()

RewardFormSet1 = modelformset_factory(Reward, extra=2, exclude=('project'), formset=BaseRewardFormSet)

class ProjectForm1(forms.ModelForm):
    """docstring for ProjectForm1"""
    class Meta:
        model = Project
        exclude=( ... )

class ProjectForm2(forms.ModelForm):
    """docstring for ProjectForm2"""
    class Meta:
        model = Project
        exclude = ( ... )

我的网址.py

instance_dict = {'2': Reward.objects.none()}
url(r'^new-project/$', login_required(ProjectWizard.as_view([ProjectForm1, ProjectForm2, RewardFormSet1, ProjectForm3], instance_dict=instance_dict))),

我的意见.py

class ProjectWizard(SessionWizardView):
    file_storage = cloudfiles_storage

    def get_context_data(self, form, **kwargs):
        context = super(ProjectWizard, self).get_context_data(form, **kwargs)
        initial_dict = self.get_form_initial('0')
        if self.steps.current == '1':
            step1_data = self.get_cleaned_data_for_step('0')
            context.update({'step1_data':step1_data,'currency_sign':step1_data['base_currency']})
        else:
            step1_data = self.get_cleaned_data_for_step('0')
            step2_data = self.get_cleaned_data_for_step('1')
            step3_data = self.get_cleaned_data_for_step('2')
            context.update({'step1_data':step1_data,'step2_data':step2_data,'step3_data':step3_data,})
        return context

    def get_template_names(self):
        step = int(self.steps.current)
        if step == 3:
            return 'formwizard/preview.html'
        else:
            return 'formwizard/wizard_form.html'

    def done(self, form_list, *args, **kwargs):
        form_data = form_list[0].cleaned_data
        form_data_details = form_list[1].cleaned_data
        form_data.update(form_data_details)
        project = Project()
        for field in project.__dict__.iterkeys():
            if field in form_data:
                project.__dict__[field] = form_data[field]
        project.owner = self.request.user
        project.date_published = datetime.now()
        project.save()        
        return render_to_response('formwizard/done.html', {
            'form_data': [form.cleaned_data for form in form_list],
        })

渲染redirect时,显示所有数据已被清理,表示表单有效,项目数据全部保存到Project

我可以看到奖励数据不会保存,因为它没有被调用,但到目前为止我尝试过的每个解决方案都失败了。

如何在保存时在此解决方案中实施奖励?

也许有人可以对此有所了解,非常感谢!

4

1 回答 1

2

这管用吗?假设Rewardproject.

for rw in form_list[2].save(commit=False):
    rw.project = project
    rw.save()
于 2012-07-23T13:01:50.990 回答