I would like to make a queryset where the current user is used as a filter in a ModelForm:
class BookSubmitForm(ModelForm):
book = forms.ModelChoiceField
Extending AdamKG answer to class based views - override the get_form_kwargs method:
class PassRequestToFormViewMixin:
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['request'] = self.request
return kwargs
from django.views.generic.edit import CreateView
class BookSubmitCreateView(PassRequestToFormViewMixin, CreateView):
form_class = BookSubmitForm
# same for EditView
and then in forms:
from django.forms import ModelForm
class BookSubmitForm(ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request")
super().__init__(*args, **kwargs)
...