Created
January 23, 2015 16:18
-
-
Save eugene-goldberg/b28c5bd260c97191bd26 to your computer and use it in GitHub Desktop.
ChoiceField - dynamic attribute value assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I need to assign the value to the 'initial' attributes for the images and flavors fields. | |
Here is how I do it currently. | |
No errors, the print shows that assignment took, but in the actual html form, I still have my default choice list, with no item selected, as desired | |
forms.py: | |
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, initial="") | |
flavor_choices = [] | |
flavors = forms.ChoiceField(label=_("Flavors"), choices=flavor_choices, initial="") | |
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'] | |
print "initial: " + self.fields['images'].initial | |
self.fields['images'].initial = str(selected_image) | |
self.fields['images'].initial = str(selected_flavor) | |
print "initial after assignment: " + self.fields['images'].initial |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment