作为表单向导的一部分我尝试将表单向导中的get_form_kwargs方法中的参数传递给我的表单,然后传递给modelformset。我已经查看了很多关于SO的示例(例如here,here和here)
代码看起来不错(初学者),但它仍然会产生此错误:
异常类型:TypeError
异常值:__ init __()得到了一个意外的关键字参数' parent'
相关views.py提取:
def get_form_kwargs(self, step=None):
kwargs = {}
if self.steps.current == 'assign_child':
kwargs = super(checkout, self).get_form_kwargs(step)
kwargs.update({
'parent': self.request.user,
})
return kwargs
forms.py extract:
class bindBookingItems(ModelForm):
class Meta:
model = BookingItem
fields = ('assignedKids',)
widgets = {
'assignedKids': Select(attrs={'class': 'form-control',}),
}
def __init__(self, *args, **kwargs):
parent = kwargs.pop('parent', None)
super(bindBookingItems, self).__init__(*args, **kwargs)
form.fields['assignedKids'].queryset = getChildren(parent.id)
checkout_bindBookingItemsFormSet = modelformset_factory(
BookingItem,
form = bindBookingItems,
fields=('assignedKids', ),
extra=0, max_num=5, can_delete=False)
我目前的想法是问题出在forms.py中。由于它的抱怨,这个论点显然正在被提起。但是,我想我会在超级声明之前正确地弹出kwarg。
我做错了什么?
更新了问题,其中包含混合中的模型形式。请忽略此信息。完全放下了我的想法。
使用追溯更新:
Traceback:
File "D:\Python27\Lib\site-packages\django\core\handlers\base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Python27\Lib\site-packages\django\views\generic\base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "D:\Python27\Lib\site-packages\django\contrib\formtools\wizard\views.py" in dispatch
237. response = super(WizardView, self).dispatch(request, *args, **kwargs)
File "D:\Python27\Lib\site-packages\django\views\generic\base.py" in dispatch
87. return handler(request, *args, **kwargs)
File "D:\Python27\Lib\site-packages\django\contrib\formtools\wizard\views.py" in get
255. return self.render(self.get_form())
File "D:\Python27\Lib\site-packages\django\contrib\formtools\wizard\views.py" in get_form
419. return form_class(**kwargs)
File "D:\Python27\Lib\site-packages\django\forms\models.py" in __init__
558. super(BaseModelFormSet, self).__init__(**defaults)
Exception Type: TypeError at /checkout/
Exception Value: __init__() got an unexpected keyword argument 'parent'
答案 0 :(得分:0)
以下是brilliant example,说明了如何正确覆盖get_form_kwargs
。
我很困惑,因为我在S.O.找不到一个血统的例子。到目前为止。
假设这种形式:
from django import forms
from .models import MyModel
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
def __init__(self, user_id, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
# set the user_id as an attribute of the form
self.user_id = user_id
现在已经定义了表单,视图需要使用用户id注入表单:
from django.views.generic import UpdateView
# this assumes that django-braces is installed
from braces.views import LoginRequiredMixin
from .forms import MyForm
from .models import MyModel
class MyUpdateView(LoginRequiredMixin, UpdateView):
model = MyModel
form_class = MyForm
success_url = "/someplace/"
def get_form_kwargs(self):
"""This method is what injects forms with their keyword
arguments."""
# grab the current set of form #kwargs
kwargs = super(MyUpdateView, self).get_form_kwargs()
# Update the kwargs with the user_id
kwargs['user_id'] = self.request.user.pk
return kwargs