使用 formset_factory 的意外关键字参数
Unexpected Keyword argument using formset_factory
我正在做一个老师可以给学生加分的项目,我想用 formset_factory 这样老师可以同时加很多分,但我想让老师只看到他的学生,而不是不在他 class.
中的学生
我在使用单一表单时得到了它,但是当我尝试使用 formset_factory 时,我得到了这个错误:
init() 得到了意外的关键字参数 'request'
这是我的工作代码
浏览量:
class AddNotaBlock(FormView):
template_name='notas/insertar_nota.html'
form_class= NotaCreateFormTeacher
success_url= reverse_lazy('home')
def get_form_kwargs(self):
""" Passes the request object to the form class.
This is necessary to only display members that belong to a given user"""
kwargs = super(AddNotaBlock, self).get_form_kwargs()
kwargs['request'] = self.request
return kwargs
形式:
class NotaCreateFormTeacher(ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request')
super(NotaCreateFormTeacher, self).__init__(*args, **kwargs)
usuario=self.request.user
profe=Teacher_profile.objects.get(profesor=usuario)
colegio=profe.colegio
self.fields['Username'].queryset = Student_profile.objects.filter(
colegio=colegio)
class Meta:
model = Nota
fields = ('Username', 'nota', 'colegio')
widgets= {'colegio':HiddenInput()}
formset=formset_factory(NotaCreateFormTeacher, extra=2)
当我使用:
form_class= NotaCreateFormTeacher
一切正常(但只显示一个表格)
如果我使用:
form_class=formset
我收到意外的关键字参数错误:
init() 得到了意外的关键字参数 'request'
我做错了什么?
感谢您的帮助。
您将其作为 form_kwargs=…
parameter [Django-doc] 传递,因此:
class AddNotaBlock(FormView):
template_name = 'notas/insertar_nota.html'
form_class = formset
success_url = reverse_lazy('home')
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs.setdefault(<strong>'form_kwargs'</strong>, {})['request'] = self.request
return kwargs
我正在做一个老师可以给学生加分的项目,我想用 formset_factory 这样老师可以同时加很多分,但我想让老师只看到他的学生,而不是不在他 class.
中的学生我在使用单一表单时得到了它,但是当我尝试使用 formset_factory 时,我得到了这个错误:
init() 得到了意外的关键字参数 'request'
这是我的工作代码 浏览量:
class AddNotaBlock(FormView):
template_name='notas/insertar_nota.html'
form_class= NotaCreateFormTeacher
success_url= reverse_lazy('home')
def get_form_kwargs(self):
""" Passes the request object to the form class.
This is necessary to only display members that belong to a given user"""
kwargs = super(AddNotaBlock, self).get_form_kwargs()
kwargs['request'] = self.request
return kwargs
形式:
class NotaCreateFormTeacher(ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request')
super(NotaCreateFormTeacher, self).__init__(*args, **kwargs)
usuario=self.request.user
profe=Teacher_profile.objects.get(profesor=usuario)
colegio=profe.colegio
self.fields['Username'].queryset = Student_profile.objects.filter(
colegio=colegio)
class Meta:
model = Nota
fields = ('Username', 'nota', 'colegio')
widgets= {'colegio':HiddenInput()}
formset=formset_factory(NotaCreateFormTeacher, extra=2)
当我使用:
form_class= NotaCreateFormTeacher
一切正常(但只显示一个表格) 如果我使用:
form_class=formset
我收到意外的关键字参数错误:
init() 得到了意外的关键字参数 'request'
我做错了什么?
感谢您的帮助。
您将其作为 form_kwargs=…
parameter [Django-doc] 传递,因此:
class AddNotaBlock(FormView):
template_name = 'notas/insertar_nota.html'
form_class = formset
success_url = reverse_lazy('home')
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs.setdefault(<strong>'form_kwargs'</strong>, {})['request'] = self.request
return kwargs