1👍
✅
The Django request object isn’t available by default in the Form class so you have to pass it by yourself:
class UserChangeForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
# Call super() after deleting the extra kwargs.
super(UserChangeForm, self).__init__(*args, **kwargs)
# Other code
And pass it when you create the form in your view:
def update(request):
form = UserChangeForm(request=request)
Source:stackexchange.com