Django表单错误:选择一个有效的选项。 ......不是可用的选择之一

时间:2017-11-16 15:07:01

标签: python django forms django-forms

我正在尝试创建一个动态选择字段。我有一个视图,创建一个元组列表。元组的第一个值是对象ServiceWriter的主键,而第二个值是ServiceWriter的名称。然后该列表将传递到表单类。当我进行选择并提交页面时,表单被确定为无效,并且在shell中打印以下表单错误:“选择一个有效的选项。(元组的第一个值。即1,2,3 ..)是不是可用的选择之一。“

forms.py

class CreateAdvancedRO(forms.Form):
service_writer = forms.ChoiceField()

def __init__(self, writer_choices, *args, **kwargs):
    super(CreateAdvancedRO, self).__init__(*args, **kwargs)
    self.fields['service_writer'].choices = writer_choices
    self.helper = FormHelper()
    self.helper.form_id = 'id-create-advanced-ro'
    self.helper.form_method = 'post'
    self.helper.add_input(Submit('submit', 'Open Repair Order'))

注意:我没有使用ModelForm。

views.py
class CreateAdvancedRO(View):                                                                    
form_class = CreateAdvancedRO                                                                
writer_form = CreateServiceWriter                                                            
add_line_form = AddJobLine                                                                   

def post(self, request):                                                                     
    writer_choices = []                                                                      
    form = self.form_class(writer_choices, request.POST)                                     
    print(form.errors)                                                                       
    if form.is_valid():                                                                      
        '''Do something'''                        
    else:                                                                                                                                                                       
        writer_choices = []                                                                  
        try:                                                                                 
            writers = ServiceWriter.objects.filter(user=request.user)                        
            for writer in writers:                                                           
                writer_choices.append((str(writer.id), writer.name))                         
        except ObjectDoesNotExist:                                                           
            pass                                                                             
        form = self.form_class(writer_choices, request.POST)                                 
        writer_form = self.writer_form()                                                     
        add_line_form = self.add_line_form()                                                 
        return render(request, 'free/advanced_create.html', {'form': form, 'writer_form': wri
                                                             'add_line_form': add_line_form})

我在视图中尝试了以下两种方法:

writer_choices.append((str(writer.id), writer.name)) and
writer_choices.append((writer.id, writer.name))

以下是ServiceWriter模型,以防万一。

class ServiceWriter(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=20, blank=False)

def __str__(self):
    return str(self.name)

有什么想法? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

您似乎正在尝试根据空的选项列表验证表单。您是否尝试在实例化或尝试验证表单之前填充writer_choices?