0

I'm getting this error when I'm rendering the below from 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")

This breaking onyl occurs when I ass the choices to my TableName widget, am I supplying the choices wrongly?

I'm using a dictionary of two columns (both of the same type to populate the html select box) but previously supplied only the list of table names and recieved the error Exception Type: ValueError Exception Value: need more than 1 value to unpack

Mark Corrigan
  • 544
  • 2
  • 11
  • 29

1 Answers1

0

The choices attribute of a django.forms.ChoiceField must be an iterable (tuple or list) of tuples, here is the relevant part of the documentation

tableChoices = (('id', 'value'), )  # or [('id', 'value')]

EDIT:

Since sqlQuery contains pyodbc.row objects you should create your choices like so

tableChoices = ((row.id, row.value) for row in sqlQuery)

replacing row.id and row.value for the values you want out of the query

Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50