If you cannot access self.request
, here's one way to solve it:
class SignUpForm(forms.ModelForm):
fullname = forms.CharField(label="Full name", widget=forms.TextInput(attrs={'placeholder': 'Full name', 'class': 'form-control'}))
class Meta:
model = SignUps
fields = ['eventname','fullname','ip']
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None) # Now you use self.request to access request object.
super(SignUpForm, self).__init__(*args, **kwargs)
def save(self, commit=True):
instance = super(SignUpForm, self).save(commit=False)
instance.fullname = fullname
instance.ip = get_ip(self.request)
if commit:
instance.save()
return instance
http://brunobastos.net/how-to-access-the-httprequest-object-in-django-forms/