【问题标题】:Updating a list within a class更新类中的列表
【发布时间】:2021-06-18 00:31:35
【问题描述】:

是否可以根据实例详细信息更新类中的列表?

我想做这样的事情:

from formtools.wizard.views import SessionWizardView
class ContactWizard(SessionWizardView):
        form_list = [ # a list of forms used per step
            (S1, Q1),
            (S2, Q2),
            (S3, Q3),
        ]
        def form_updater(self):
            if self.get_cleaned_data_for_step("0") != None:
                form_list.append((S3, Q3),)
            return form_list

#However, I get thrown an error when I try to make the following call:
ContactWizard.form_updater(ContactWizard)
## error:
get_cleaned_data_for_step() missing 1 required positional argument: 'step'

非常感谢您提供的任何帮助或指导。

【问题讨论】:

  • 你在哪里打电话ContactWizard
  • 课外。如果我尝试在类中调用它,它的名称将无法识别。
  • 请显示您用来调用它的代码
  • @LordElrond 我更新了我的帖子以澄清我的通话结构。这应该是可复制的。

标签: python-3.x django django-views django-formwizard


【解决方案1】:

看起来你想要一个classmethod

class ContactWizard(SessionWizardView):
        form_list = [ # a list of forms used per step
            (S1, Q1),
            (S2, Q2),
            (S3, Q3),
        ]
   
        @classmethod
        def form_updater(cls):
            if cls.get_cleaned_data_for_step("0") != None:
                cls.form_list.append((S3, Q3),)
            return form_list

【讨论】:

  • 感谢@LordElrond。当我运行您的代码时,我得到一个替代错误: form_updater() 采用 1 个位置参数,但给出了 2 个。知道这是为什么吗?
  • @unicoder 你还在把课程传进去吗?你应该不带任何参数地调用它,像这样:ContactWizard.form_updater()
  • 谢谢,@LordElrond。当我在没有任何参数的情况下进行调用时,我收到以下错误(与我开始时相同):get_cleaned_data_for_step() 缺少 1 个必需的位置参数:'step'。任何想法为什么会出现这种情况?
  • @unicoder get_cleaned_data_for_step() 不是类方法,因此 cls 实例不会被隐式传递给它。我不熟悉您使用的软件包,所以我无法为您提供进一步的帮助。相反,我建议阅读formtools 的此文档并按照他们的建议进行操作。
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 2012-10-15
  • 2015-09-28
  • 2016-01-19
  • 1970-01-01
  • 2019-01-11
  • 2018-07-06
  • 2018-01-28
相关资源
最近更新 更多