我正在使用 aSessionWizardView并且我不明白为什么done()从不调用该方法。相反,在发布我的表单后,在最后一步,我可以POST HTTP 200在我的服务器上看到 a ,但这没有任何作用。该get_form()方法按预期工作。
我怀疑是分散注意力的错误,因为我对另一个视图有完全相同的逻辑,这很有效。
下面是整个代码。
风景
class DiscountsCreateView(PermissionRequiredCanHandleProducts,
ModelInContextMixin,
RestaurantMixin, SubSectionDiscounts,
SessionWizardView):
""" Wizard view to create a discount in 2 steps """
model = Discount # used for model context
form_list = [DiscountForm0, DiscountForm1]
template_name = "discounts/discount_add.html"
def get_form(self, step=None, data=None, files=None):
form = super().get_form(step, data, files)
if step is None:
step = self.steps.current
# step0 - name, kind, tax_rate
# => nothing special to do, always the same form
# step1 - specific fields related to the chosen kind
if step == '1':
step0_data = self.storage.get_step_data('0')
kind = step0_data['0-kind']
# combo => combo, combo_unit_price
if kind == Discount.COMBO:
form.fields['combo'].queryset = Combo.objects.restaurant(self.restaurant)
# NOTE : this is not a scalable way to show/hide fields (exponential)
form.fields['rebate_amount'].widget = forms.HiddenInput()
elif kind == Discount.REBATE:
form.fields['combo'].widget = forms.HiddenInput()
form.fields['combo_unit_price'].widget = forms.HiddenInput()
return form
def done(self, form_list, **kwargs):
data = [form.cleaned_data for form in form_list]
try:
Discount.objects.create(
name=data[0]['name'],
kind=data[0]['kind'],
tax_rate=data[0]['tax_rate'],
rebate_amount=data[1]['rebate_amount'],
combo=data[1]['combo'],
combo_unit_price=data[1]['combo_unit_price']
)
冉冉说
相关分类