How could I have a singleton view for the new Django Form Wizard?
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])
re开发者_StackOverflowturn 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!
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]))
精彩评论