Skip to content Skip to sidebar Skip to footer

Making Choices In Forms Dynamic

I have been stuck for a while trying to set a dynamic CHIOCES in form selection using python 2.7. I need the CHOICES to come from the query in my def update_file_choices. Here is t

Solution 1:

If your choices are coming directly from a queryset (of FileUpload), why don't you use the form field that is designed for that: ModelChoiceField?

titles = forms.ModelChoiceField(queryset=FileUpload.objects.all())

Solution 2:

You need to make it be some automatically changing iterable. Something like:

deffiles_uploaded():
        for file in FileUpload.objects.all():
                yield (file.title, file.title, )

classRoomForm(forms.Form):
        titles = form.ChoiceField(choices=files_uploaded())
        def__init__(self, *args, **kwargs):
                super(RoomForm, self).__init__(*args, **kwargs)
                self.fields['titles'].choices = files_uploaded()

Post a Comment for "Making Choices In Forms Dynamic"