我怎样才能为新的 Django 表单向导提供单例视图?

发布于 2024-11-29 20:02:11 字数 1144 浏览 1 评论 0原文

我正在使用新的 Django 表单向导工具。它将在下一个 Django 1.4 中发布,但您可以在这里找到它: https://github.com/ stephrdev/django-formwizard

我想为所有向导过程提供一个单例视图类。

这是我的代码:

class submit(object):
    instance = None 

    def __new__(cls, request, *args, **kwargs):
        if not cls.instance:
            cls.instance = super(submit, cls).__new__(cls)
            cls.form     = SubmitStoryWizard.as_view([SubmitStoryForm1, SubmitStoryForm2, SubmitStoryForm3, SubmitStoryForm4])
        return cls.instance(request, *args, **kwargs)

    def __init__(self):
        pass

    def __call__(self, request, *args, **kwargs):
        return self.form(request)

问题是 WizardView 继承自 TemplateView,因此 as_view 方法返回一个函数。所以,在这行中:

cls.form     = SubmitStoryWizard.as_view([SubmitStoryForm1, SubmitStoryForm2, SubmitStoryForm3, SubmitStoryForm4])

就像我在类变量中存储了一个函数。因此,在 __call__ 方法中,当我调用 self.form 函数时,Python 会自动添加该类的引用作为第一个参数。 这是我不想要的。

我该如何解决它?有什么想法吗?

抱歉我的英语:S

提前致谢,

问候!

I am working with the new Django Form Wizard tool. It will be released in the next Django 1.4, but you can find it here: https://github.com/stephrdev/django-formwizard

I would like to have a singleton view class for all the wizard process.

This is my code:

class submit(object):
    instance = None 

    def __new__(cls, request, *args, **kwargs):
        if not cls.instance:
            cls.instance = super(submit, cls).__new__(cls)
            cls.form     = SubmitStoryWizard.as_view([SubmitStoryForm1, SubmitStoryForm2, SubmitStoryForm3, SubmitStoryForm4])
        return cls.instance(request, *args, **kwargs)

    def __init__(self):
        pass

    def __call__(self, request, *args, **kwargs):
        return self.form(request)

The problem is that WizardView inherits from TemplateView so the as_view method returns a function. So, in the line:

cls.form     = SubmitStoryWizard.as_view([SubmitStoryForm1, SubmitStoryForm2, SubmitStoryForm3, SubmitStoryForm4])

It's like I was storing in a class variable a function. So, in the __call__ method, when I call to self.form function, Python automatically add as the first parameter a reference of the class. This is what I do not want.

How could I resolve it? Any ideas?

Sorry for my english :S

Thanks in advance,

Greetings!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气称霸 2024-12-06 20:02:11

有趣的问题。我还没有测试过,但这个修改应该有效:

从:

    cls.form     = SubmitStoryWizard.as_view([SubmitStoryForm1, SubmitStoryForm2, SubmitStoryForm3, SubmitStoryForm4])

到:

    cls.form     = staticmethod(SubmitStoryWizard.as_view([SubmitStoryForm1, SubmitStoryForm2, SubmitStoryForm3, SubmitStoryForm4]))

Interesting issue. I haven't tested, but this modification should work:

from:

    cls.form     = SubmitStoryWizard.as_view([SubmitStoryForm1, SubmitStoryForm2, SubmitStoryForm3, SubmitStoryForm4])

to:

    cls.form     = staticmethod(SubmitStoryWizard.as_view([SubmitStoryForm1, SubmitStoryForm2, SubmitStoryForm3, SubmitStoryForm4]))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文