1👍
✅
The Task
model has a foreign key to host
. Since this is an update view, I don’t think you actually need to pass the host
to the model form’s __init__
method. Use self.instance.host
instead.
class TaskForm(forms.ModelForm):
...
def __init__(self, *args, **kwargs):
super(TaskForm, self).__init__(*args, **kwargs)
self.fields['event'].queryset = Event.objects.filter(host=self.instance.host)
...
If you really do need to pass host
to the model form, then you can override get_form_kwargs
. Since host
seems to be the logged in user, you would do:
class EditTask(UpdateView):
# since this is a class, EditTask is a better name than edit_task
...
def get_form_kwargs(self):
kwargs = super(EditTask, self).get_form_kwargs()
kwargs['host'] = self.request.user
return kwargs
Source:stackexchange.com