Key error in django when using kwargs.pop

0

Good, I would appreciate the help, I seem to be blocked with this, and it should not be complicated.

I have a function in views.py that instantiates a form in the following way

form = BusquedaPresenciaForm(initial={'fecha_inicio': datetime.date.today(), 'fecha_fin': datetime.date.today()}, form_kwargs={'usuario_actual': request.user.id})

In forms.py I have the following form

class BusquedaPresenciaForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        usuario_actual = kwargs.pop('usuario_actual')
        super(BusquedaPresenciaForm, self).__init__(*args, **kwargs)
        self.fields['usuario'].queryset = Coordinacion.objects.filter(coordinador=usuario_actual.id)

It is a question of a filter of the user field of the form filtered by a field in the form, according to the current user.

The error that is returning me is: Exception Type: KeyError Exception Value:
'current_user'

On the line: current_user = kwargs.pop ('current_user')

    
asked by Cecilio Alonso 17.01.2017 в 12:14
source

1 answer

0

I have rethought it a bit and I have put it to work the changes are when the form is instantiated in views.py

form = BusquedaPresenciaForm(initial={'fecha_inicio': datetime.date.today(), 'fecha_fin': datetime.date.today()}, usuario_actual=request.user)

It seems that it is not necessary to use "form_kwargs=" to pass values to the form, I read something related to this in the documentation and it seems that I misunderstood it.

    
answered by 17.01.2017 / 12:45
source