Django-formwizard 和 ModelFormSet 保存 [英] Django-formwizard and ModelFormSet save
问题描述
我正在重写我们应用程序的一大块,它需要用户创建一个 Project
并附加 Rewards
.
I am rewriting a big piece of our application which requires a user to create a Project
with Rewards
attached to it.
表单分为不同的步骤,前两个是普通的Project
,下一个是Rewards
,最后是一个简单的预览,让用户来回轻弹以创建完美的项目
.
The form is broken into different steps, the first two are the normal Project
, the next one is the Rewards
, and then lastly a simple preview that lets the user flick back and forth to create a perfect Project
.
我的 forms.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 = ( ... )
我的 urls.py
instance_dict = {'2': Reward.objects.none()}
url(r'^new-project/$', login_required(ProjectWizard.as_view([ProjectForm1, ProjectForm2, RewardFormSet1, ProjectForm3], instance_dict=instance_dict))),
我的views.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
when rendering redirect, it shows that all the data has been cleaned which means that the form is valid, and all the project data is saved to Project
我可以看到 Rewards 数据没有被保存,因为它没有被调用,但到目前为止我尝试过的每个解决方案都失败了.
I can see that the Rewards data will not save since it has not been called, but every solution I have tried thus far has failed.
如何在保存时在此解决方案中实施奖励?
也许有人可以对此有所了解,非常感谢!
Maybe someone can shed some light on this, much appreciated!
推荐答案
这有效吗?假设 Reward
有 project
.
Is this working? Assuming Reward
has project
.
for rw in form_list[2].save(commit=False):
rw.project = project
rw.save()
这篇关于Django-formwizard 和 ModelFormSet 保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!