异常类型:ValueError异常值:解包的值太多 - Django

时间:2014-07-24 10:48:43

标签: django select django-forms django-templates django-widget

当我从as(form.as_ul)呈现以下内容时,我收到此错误

class InputParametersForm(ModelForm):

sqlConnection = SQLSeverConnection('MSSQLServerDataSource')
tableChoices = {'id': 'value'}
sqlQuery = sqlConnection.getTableNames()

for id in sqlQuery:
    tableChoices['id'] = id

for value in sqlQuery:
    tableChoices['value'] = value

TableName = forms.ChoiceField(widget=forms.Select(tableChoices),
                              choices=tableChoices)

ColumnName = forms.ChoiceField(widget=forms.Select())

StartDateTime = forms.DateField(widget=SelectDateWidget())

EndDateTime = forms.DateField(widget=SelectDateWidget())

class Meta:
    model = SelectionHistory
    fields = ("TableName", "ColumnName", "StartDateTime", "EndDateTime")

当我选择我的TableName小部件时,会发生这种破坏性的onyl,我是否错误地提供了选择?

我使用两列的字典(两者都是相同的类型来填充html选择框)但之前只提供了表名列表并收到了错误异常类型:ValueError 异常值:解包需要多于1个值

1 个答案:

答案 0 :(得分:0)

choices的{​​{1}}属性必须是元组的可迭代(元组或列表),here is the relevant part of the documentation

django.forms.ChoiceField

编辑:

由于tableChoices = (('id', 'value'), ) # or [('id', 'value')] 包含sqlQuery个对象,您应该像这样创建选择

pyodbc.row

tableChoices = ((row.id, row.value) for row in sqlQuery) row.id替换为您想要查询的值