13👍
✅
So give yourself a reference to it.
class MyModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(MyModelForm, self).__init__(*args, **kwargs)
def clean(self):
ip_address = self.request['META']['REMOTE_ADDR']
and in your view:
myform = MyModelForm(request.POST, request=request)
Source:stackexchange.com