如何在 Horizon 工作流上正确调用__init__.操作



在我的自定义工作流程中,我有一个操作,除其他外,它应该向用户显示可用的OpenStack图像和风格的列表。我的假设是,填充这两个列表的最佳位置是在__init__方法中。目前,我的看起来像这样:

class SetWorkflowInfoAction(workflows.Action):
    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(SetWorkflowInfoAction, self).__init__(request, *args, **kwargs)
        image_choices = []
        flavor_choices = []
        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
    class Meta:
        name = _("Basic Workload Information")
        help_text = _("")
        slug = "set_workflow_info"

问题是 - 当我到达调用__init__时,我收到此错误:

File "/home/eugene/dev/horizon/openstack_dashboard/dashboards/mydashboard/workloads_panel/workflows.py", line 20, in __init__
    super(SetWorkflowInfoAction, self).__init__(request, *args, **kwargs)
TypeError: __init__() takes at least 3 arguments (2 given)

以这种方式调用__init__的正确方法是什么,以便我可以填充我的图像和风味列表?

要完成这项工作,我所要做的就是从顶级 ini 中删除两个未使用的参数。

因此,工作版本如下所示:

class SetWorkflowInfoAction(workflows.Action):
    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,
                 *args, **kwargs):
        super(SetWorkflowInfoAction, self).__init__(request, *args, **kwargs)
        image_choices = []
        flavor_choices = []
        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
    class Meta:
        name = _("Basic Workload Information")
        help_text = _("")
        slug = "set_workflow_info"

相关内容

  • 没有找到相关文章

最新更新