Hello,
I’m trying to pass request to a form like this, overriding get_form_class in CreateView class
def get_form_class(self):
class DemandeBaseForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super().__init__(*args, **kwargs)
def clean_montant(self):
montant = self.cleaned_data['montant']
base_limit = 1509
print(self.request.user.employee.motant)
if montant > base_limit:
raise forms.ValidationError(f'Montant exceeds the limit of {base_limit}.')
return montant
class Meta:
model = self.model
return DemandeBaseForm
....
i tried to pass the request to the form in get_context_data:
def get_context_data(self, **kwargs) -> dict:
context = super().get_context_data(**kwargs)
form_class = self.get_form_class()
form = form_class(self.request.POST or None, request=self.request)
context["form"] = form
return context
but i always receive the error when i access the value request.user
Exception Value:
'NoneType' object has no attribute 'user'
I’m doing something wrong but i can’t find it.
Thanks for help,