4

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 = 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?

Lev Levitsky
  • 59,844
  • 20
  • 139
  • 166
Eugene Goldberg
  • 12,610
  • 19
  • 85
  • 154

1 Answers1

4

After all, using this approach:

self.fields['images'].initial = selected_image
self.fields['flavors'].initial = selected_flavor

is working. The only thing I did differently was changing my backend from django restframework to tastypie

Lev Levitsky
  • 59,844
  • 20
  • 139
  • 166
Eugene Goldberg
  • 12,610
  • 19
  • 85
  • 154