initial Queryset from view to form

Hi,

I have a dropdown on a form I would like to give a queryset from from view.py If no queryset is passed I would like the list to be empty. I’ve tried numerous examples but I can’t seem to get it to work. When I pass my queryset, the form doesn’t show it (just shows the queryset that in the forms.Form.

forms.py

class SubmitResultForm(forms.Form):
    # the experiments listed need to be only those in the course the student is in
    expName = forms.ModelChoiceField( queryset=Experiment.objects.all(), label="Experiment: ", widget=forms.Select(), required=False)
    expResult = forms.IntegerField(label="Experiment Result:", required=False) 

view.py

context = {}
experimentList = Experiment.objects.filter(courseCode__pk=2).values('pk','name')
context['form']= SubmitResultForm(initial={'expName':experimentList})

thank you!

This has been discussed multiple times here. You can search to find some examples.

You might want to start with Passing an altered queryset to ModelChoiceField. The 4th message in that thread has links to some other threads on this same topic.