Unexpected Keyword argument using formset_factory


I’m working on a project in which a teacher can add marks to his students, I want to use formset_factory so the teacher can add many marks at the same time, but I want a teacher to see only his students, and not the students who are not in his class.

I got it when using a single form, but when I try to use the formset_factory, I get this error:

init() got an unexpected keyword argument ‘request’

This is my working code
in views:

  class AddNotaBlock(FormView):

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

in forms:

  class NotaCreateFormTeacher(ModelForm):

def __init__(self, *args, **kwargs):

    self.request = kwargs.pop('request')
    super(NotaCreateFormTeacher, self).__init__(*args, **kwargs)
    self.fields['Username'].queryset = Student_profile.objects.filter(

class Meta:
    model = Nota
    fields = ('Username', 'nota', 'colegio')
    widgets= {'colegio':HiddenInput()}

  formset=formset_factory(NotaCreateFormTeacher, extra=2)

when I use:

  form_class= NotaCreateFormTeacher

Everything works (but only a form is displayed)
If I use:


I get the unexpected keyword argument error:

init() got an unexpected keyword argument ‘request’

What I’m I doing wrong?

Thanks for helping.


You pass this as form_kwargs=… parameter [Django-doc], so:

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('form_kwargs', {})['request'] = self.request
        return kwargs

Answered By – Willem Van Onsem

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published