Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

As it turns out, this was the actually correct syntax:

class UpdateWorkload(forms.SelfHandlingForm): name = forms.CharField(max_length="255", label=_("Workload Name")) description = forms.CharField(widget=forms.Textarea, label=_("Description"), required=False) image_choices = [] images = forms.ChoiceField(label=_("Images"), choices=image_choices) flavor_choices = [] flavors = forms.ChoiceField(label=_("Flavors"), choices=flavor_choices)

def __init__(self, request, image_choices=image_choices, flavor_choices=flavor_choices,
             *args, **kwargs):
    super(UpdateWorkload, self).__init__(request, *args, **kwargs)

    images, self._more, self._prev = api.glance.image_list_detailed(
        self.request)
    flavors = api.nova.flavor_list(request, True)

    for image in images:
            image_choices.append((image.id, image.name))
    if len(image_choices) > 1:
        image_choices.insert(0, ('', _("Select an Image")))
    self.fields['images'].choices = image_choices

    for flavor in flavors:
            flavor_choices.append((flavor.id, flavor.name))
    if len(flavor_choices) > 1:
        flavor_choices.insert(0, ('', _("Select an Flavor")))
    self.fields['flavors'].choices = flavor_choices
    selected_workload = kwargs['initial']
    selected_image = selected_workload['image']
    selected_flavor = selected_workload['flavor']

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