tags:

views:

47

answers:

2

I thought I had it figured out but now I'm missing something.

First I have a QuerySet, records

records = Record.objects.all()

Now I want to make this into a list of one of the columns of the table, columnA

alist = records.values_list('columnA')

And then I want to pass this list in as a parameter to a custom form.

FilterForm(alist)

Here's my form

class FilterForm(forms.Form,list):
    numbers = forms.ChoiceField(list)

but keep getting an error that 'type' object is not iterable. I'm not sure the problem has to do with the passing of the list because when I try and run this code in the shell, I get the error message when just importing the FilterForm

EDIT: I changed my FilterForm so now it looks like this.

class FilterForm(forms.Form):
    def __init__(self,numbers):
        number = forms.ChoiceField(numbers)

so now I think it's more evident what I'm trying to do, pass in a list to the FilterForm. However when I render my template and pass the form, no form field shows up. No error message though

EDIT EDIT: Also tried this, saw it online

class FilterForm(forms.Form):
    number = forms.ChoiceField()

    def __init__(self,numbers):
        super(FilterForm,self).__init__()
        self.fields['number'].choices=numbers

but error:

Exception Type:     TemplateSyntaxError
Exception Value:    

Caught ValueError while rendering: need more than 1 value to unpack
+1  A: 

The problem is the word list in this line:

numbers = forms.ChoiceField(list)

You need to provide a specific list to ChoiceField.

Ned Batchelder
Ah, python is not java...What I'm trying to do is have the class take a list as a parameter. I suppose it's not called list though. In java I would a constructor FilterForm(List l) or something but, how is this different in python
JPC
edited my original post
JPC
A: 

Here's an error:

class FilterForm(forms.Form,list):
    numbers = forms.ChoiceField(list)

You make FilterForm a subclass of forms.Form and list; then you expect list to be available as argument to the ChoiceField.

I think you are looking for dynamic ChoiceFields.

Further reading:

The MYYN
edited my original post
JPC