我有一个表单,利用从我创建的formset的选择参数。当包含formset的页面被渲染时,使用选择参数的字段显示下拉选择小部件。用户填写的表单没有错误。但是,用户未填写的表单对于除了使用选择小部件的字段以外的所有其他字段具有“此字段是必需的”错误。Django formset错误:选择字段导致部分填充的表单
看起来,选择字段的初始值导致表单被视为半填充形式,因此表单验证过程会为未填充的必填字段抛出错误。
# Form:
class OwnerForm(forms.Form):
name = forms.CharField(label = 'Name', max_length = 20)
owner_entity = forms.ChoiceField(label = 'Owner Entity', choices = OWNER_ENTITIES)
num_of_shares = forms.DecimalField(label = 'Number of Shares' , min_value = 0.0, max_digits = 5, decimal_places = 2)
share_class = forms.ChoiceField(label = 'Share Class', choices = SHARE_CLASSES)
joined_date = forms.DateField(label = 'Joined Date', help_text = 'mm/dd/yyyy')
# View:
# In Get method:
OwnersFormSet = formset_factory(OwnerForm, extra = 5)
...
# In Post method:
the_owners_forms = OwnerFormSet(request.POST)
if not the_owners_forms.is_valid():
the_owners_forms_errors = the_owners_forms.errors
所以,问题是我该如何处理这种行为以使非填充形式不采取是因为选择方法的初值为HAL-填写的表格?
我曾尝试发布问题之前,但它没有奏效。我向OWNER_ENTITIES添加了一个(None,'------------'),但是它跳过了这个并选择元组中的下一个元素。我在想的是手动删除这些初始值,当我收到表单并在手动修改后运行is_valid。 – EarlyCoder
如果您想使用javascript,我为您的问题提供了更加优雅的解决方案 – trantu
您的解决方案非常值得欢迎!我也想知道如何使用Django解决这个问题。 – EarlyCoder