I am trying to pass the data from the request to my form. Currently, that is resulting in an error:
BaseModelForm.init() got an unexpected keyword argument 'request'
My view:
class TaskCreate(LoginRequiredMixin, CreateView):
model = Task
template_name = "tasks/task_form.html"
form_class = DateInputForm
My view (get_form_kwargs function):
def get_form_kwargs(self, *args, **kwargs):
form_kwargs = super().get_form_kwargs()
form_kwargs['request'] = self.request
return form_kwargs
Init from my form :
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
request = kwargs.pop('request', None)
self.fields['tags'].queryset = Tag.objects.filter(user=request.user.id)
I know that I can find the user's ID in my request
CodePudding user response:
Solution: (1) My view (get_form_kwargs function):
def get_form_kwargs(self, *args, **kwargs):
form_kwargs = super().get_form_kwargs(*args, **kwargs)
form_kwargs['request'] = self.request
return form_kwargs
(2) Init from my form:
def __init__(self, request,*args, **kwargs):
self.request = request
super().__init__(*args, **kwargs)
self.fields['tags'].queryset = Tag.objects.filter(user=request.user.id)