注意:最终解决了解决方案。
我正在尝试将请求值传递给表单,我必须过滤表单中的字段,但它会一直返回TypeError,我认为代码没问题。
views.py:
class AddProposalSet(FormView):
"""
Create a new prpoposal set, it can be related to a debate or be in free mode,
which is not linked to anything. If it's linked to a debate, people can
make their proposals related to the debate notes.
.. versionadded: 0.1.5
:rtype: Form object
:context: form, get_place
"""
#form_class = ProposalSetForm
template_name = 'proposals/proposalset_form.html'
def get_success_url(self):
space = self.kwargs['space_url']
return reverse(urln_space.SPACE_INDEX, kwargs={'space_url':space})
def get_form_class(self):
"""
The reason for overriding "form_class" is that we pass here the
request to the form, that way we can filter the debates based on the
current space.
"""
return ProposalSetForm(self.kwargs['space_url'])
def form_valid(self, form):
self.space = get_object_or_404(Space, url=self.kwargs['space_url'])
form_uncommited = form.save(commit=False)
form_uncommited.space = self.space
form_uncommited.author = self.request.user
form_uncommited.save()
return super(AddProposalSet, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(AddProposalSet, self).get_context_data(**kwargs)
self.space = get_object_or_404(Space, url=self.kwargs['space_url'])
context['get_place'] = self.space
return context
@method_decorator(permission_required('proposals.add_proposalset'))
def dispatch(self, *args, **kwargs):
return super(AddProposalSet, self).dispatch(*args, **kwargs)
forms.py
class ProposalSetForm(ModelForm):
"""
ProposalSetForm is a basic form autogenerated form from ProposalSet model.
Returns an empty form to create a new proposal set.
:rtype: HTML Form
.. versionadded:: 0.1.5b
"""
class Meta:
model = ProposalSet
def __init__(self, *args, **kwargs):
super(ProposalSetForm, self).__init__(*args, **kwargs)
get_place = get_object_or_404(Space, url=args[0])
if self.instance:
self.fields['debate'].queryset = Debate.objects.filter(end_date__lte=datetime.date.today(), space=get_place)
错误:
TypeError at /en-gb/spaces/test/proposal/add/set/
'ProposalSetForm' object is not callable
我无法找到可能发生的事情,我错过了什么?
SOLUTION:
views.py
class AddProposalSet(FormView):
"""
Create a new prpoposal set, it can be related to a debate or be in free mode,
which is not linked to anything. If it's linked to a debate, people can
make their proposals related to the debate notes.
.. versionadded: 0.1.5
:rtype: Form object
:context: form, get_place
"""
form_class = ProposalSetForm
template_name = 'proposals/proposalset_form.html'
def get_success_url(self):
space = self.kwargs['space_url']
return reverse(urln_space.SPACE_INDEX, kwargs={'space_url':space})
def get_form_kwargs(self, **kwargs):
kwargs = super(AddProposalSet, self).get_form_kwargs(**kwargs)
kwargs['initial']['space'] = self.kwargs['space_url']
return kwargs
def form_valid(self, form):
self.space = get_object_or_404(Space, url=self.kwargs['space_url'])
form_uncommited = form.save(commit=False)
form_uncommited.space = self.space
form_uncommited.author = self.request.user
form_uncommited.save()
return super(AddProposalSet, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(AddProposalSet, self).get_context_data(**kwargs)
self.space = get_object_or_404(Space, url=self.kwargs['space_url'])
context['get_place'] = self.space
return context
@method_decorator(permission_required('proposals.add_proposalset'))
def dispatch(self, *args, **kwargs):
return super(AddProposalSet, self).dispatch(*args, **kwargs)
forms.py
class ProposalSetForm(ModelForm):
"""
ProposalSetForm is a basic form autogenerated form from ProposalSet model.
Returns an empty form to create a new proposal set.
:rtype: HTML Form
.. versionadded:: 0.1.5b
"""
class Meta:
model = ProposalSet
def __init__(self, *args, **kwargs):
super(ProposalSetForm, self).__init__(*args, **kwargs)
get_place = get_object_or_404(Space, url=kwargs['initial']['space'])
if self.instance:
self.fields['debate'].queryset = Debate.objects.filter(end_date__lte=datetime.date.today(), space=get_place)
print self.fields['debate']
答案 0 :(得分:1)
get_form_class()
方法应该返回类而不是表单类的对象/实例。所以你的方法可以更新到
def get_form_class(self):
return ProposalSetForm
但是,我不确定这将如何传递您需要的__init__()
类的参数。
您可以定义get_form()
而不是get_form_class()
并返回相应的表单实例。
答案 1 :(得分:0)
看起来ProposalSetForm被作为一个模板中的函数调用