Hello,
In my Form I have a couple of ChoiceField fields, for which I need to set a 
default / initial value.
I'm trying to do this in the __init__, and my debug shows, that these 
values do take, but, when the actual html forms shows up, my default values 
are not set as desired.
Am I missing something?

The complete code is below. The two fields are 'images' and 'flavors'

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

    def handle(self, request, data):
         try:
            message = _('Updated Workload')
            messages.info(request, message)
            # This is where we actually update our Workload
            workload_url = "http://localhost:8000/workloads/";
            workload_name = request.POST.get("name")
            workload_description = request.POST.get("description")
            flavor_id = request.POST.get("flavors")
            image_id = request.POST.get("images")
            post_data = {"name": workload_name, "description": 
workload_description,
                         "image": image_id, "flavor": flavor_id}
            post_data = json.dumps(post_data)
            # req = urllib2.Request(workload_url)
            # req.add_header('Content-Type', 'application/json')
            req = urllib2.Request(url=workload_url, data=post_data)
            req.get_method = lambda: 'PUT'
            resp = urllib2.urlopen(req, post_data)

            return True
         except Exception:
            redirect = reverse("horizon:mydashboard:workloads_panel:index")
            exceptions.handle(request,
                              _('Unable to create Workload.'),
                              redirect=redirect)


-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4c738a85-18ce-4a46-8643-acdbf6f40a15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to