Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to dynamically update the 'initial' value in a Form ChoiceField

I have a Form with the following field: image_choices = [] images = forms.ChoiceField(label=_("Images"), choices=image_choices, initial="")

I need to be able to update the value of the 'initial' attribute, after I learn what that value should be. Currently, I have this assignment done within the __init__ :

def __init__(self, request, image_choices=image_choices, flavor_choices=flavor_choices, args, *kwargs): super(UpdateWorkload, self).__init__(request, args, *kwargs) selected_image = selected_workload['image'] self.fields['images'].initial = str(selected_image)

I do not get any errors, and, when printed, the value is there, but, in the actual form on the screen, I still get my default list, and no specific items are selected, as per self.fields['images'].initial = str(selected_image)

How can I fix that?

How to dynamically update the 'initial' value in a Form ChoiceField

I have a Form with the following field: field:

image_choices = []
    images = forms.ChoiceField(label=_("Images"), choices=image_choices, initial="")

initial="")

I need to be able to update the value of the 'initial' attribute, after I learn what that value should be. Currently, I have this assignment done within the __init__ :

def __init__(self, request, image_choices=image_choices, flavor_choices=flavor_choices,
                 args, *kwargs):
*args, **kwargs):
        super(UpdateWorkload, self).__init__(request, args, *kwargs)
*args, **kwargs)
       selected_image = selected_workload['image']
      self.fields['images'].initial = str(selected_image)

str(selected_image)

I do not get any errors, and, when printed, the value is there, but, in the actual form on the screen, I still get my default list, and no specific items are selected, as per self.fields['images'].initial = str(selected_image)

How can I fix that?