Django 表单向导使用以前的表单验证(干净)表单
Django form wizard validate (clean) form using previous form
问题简述:
我正在使用 django Form Wizard 依次显示多个表单。是否有一种干净的方法可以使用先前形式(步骤 X < Y)的数据来验证后面的形式(在步骤 Y)?
详情:
标准的 django 表单验证是在表单的每一步的 clean() 方法中完成的,如果当前表单有效并且没有引发 ValidationError,向导只会继续下一步。我现在遇到一种情况,后面步骤 (X) 的表单验证取决于前面表单 (Y < X) 的数据。仅当这两个表单具有兼容数据时,我希望向导接受表单 Y 并继续执行步骤 Y+1。
django documentation 指定表单和字段验证在以下情况下的工作方式:
- 单个字段,
The clean_< fieldname >() method is called on a form subclass – where is replaced with the name of the form field attribute. This method does any cleaning that is specific to that particular attribute, unrelated to the type of field that it is.
- 同表单的多个字段
The form subclass’s clean() method can perform validation that requires access to multiple form fields.
但是,我找不到任何可以使用前面步骤的所有数据验证当前步骤的地方。我希望表单向导中有一个 clean() 方法。但是,我找不到它。
可以这样实现:
- 覆盖向导的get_form_initial(self, step) 方法。后面的步骤处理如下:
- 取回上一步清理后的数据;
- 检索此步骤的初始数据(超级调用);
- 合并两个词典和return结果;
- 在后一个表单的 clean 方法中,通过 self.initial 字段访问前一个表单的数据。
向导的代码片段 class:
def get_form_initial(self, step):
if step == '5':
step4 = self.get_cleaned_data_for_step('3')
res = super(DenovoPatternWizard, self).get_form_initial(step)
res['extendsequencedb'] = {}
res['extendsequencedb']['include_most_similar_pattern_sequences'] = step4['sequencepatternassignment'][
'number_sites_per_pattern']
return res
后一种形式的代码片段:
def clean(self):
cleaned_data = super(InputDenovoPatternPatternForm, self).clean()
# compatibility of earlier and current step
if self.initial['timeseries_data'].number_timepoints != cleaned_data['pattern_data'].number_timepoints:
raise ValidationError("The time-series and pattern input files need to have the same number of timepoints (%d and %d respectively)." %
(self.initial['timeseries_data'].number_timepoints, cleaned_data['pattern_data'].number_timepoints))
问题简述:
我正在使用 django Form Wizard 依次显示多个表单。是否有一种干净的方法可以使用先前形式(步骤 X < Y)的数据来验证后面的形式(在步骤 Y)?
详情:
标准的 django 表单验证是在表单的每一步的 clean() 方法中完成的,如果当前表单有效并且没有引发 ValidationError,向导只会继续下一步。我现在遇到一种情况,后面步骤 (X) 的表单验证取决于前面表单 (Y < X) 的数据。仅当这两个表单具有兼容数据时,我希望向导接受表单 Y 并继续执行步骤 Y+1。
django documentation 指定表单和字段验证在以下情况下的工作方式:
- 单个字段,
The clean_< fieldname >() method is called on a form subclass – where is replaced with the name of the form field attribute. This method does any cleaning that is specific to that particular attribute, unrelated to the type of field that it is.
- 同表单的多个字段
The form subclass’s clean() method can perform validation that requires access to multiple form fields.
但是,我找不到任何可以使用前面步骤的所有数据验证当前步骤的地方。我希望表单向导中有一个 clean() 方法。但是,我找不到它。
可以这样实现:
- 覆盖向导的get_form_initial(self, step) 方法。后面的步骤处理如下:
- 取回上一步清理后的数据;
- 检索此步骤的初始数据(超级调用);
- 合并两个词典和return结果;
- 在后一个表单的 clean 方法中,通过 self.initial 字段访问前一个表单的数据。
向导的代码片段 class:
def get_form_initial(self, step):
if step == '5':
step4 = self.get_cleaned_data_for_step('3')
res = super(DenovoPatternWizard, self).get_form_initial(step)
res['extendsequencedb'] = {}
res['extendsequencedb']['include_most_similar_pattern_sequences'] = step4['sequencepatternassignment'][
'number_sites_per_pattern']
return res
后一种形式的代码片段:
def clean(self):
cleaned_data = super(InputDenovoPatternPatternForm, self).clean()
# compatibility of earlier and current step
if self.initial['timeseries_data'].number_timepoints != cleaned_data['pattern_data'].number_timepoints:
raise ValidationError("The time-series and pattern input files need to have the same number of timepoints (%d and %d respectively)." %
(self.initial['timeseries_data'].number_timepoints, cleaned_data['pattern_data'].number_timepoints))