我希望在我的模型表单中添加IP地址,但是我无法设置想要的IP地址,它只会返回“此字段是必需的”。
models.py
class SignUp(models.Model):
....
ip = models.IPAddressField()
views.py
def my_view(request):
form = SignUpForm(request.POST, ip_addr=get_client_ip(request))
forms.py
class SignUpForm(ModelForm):
class Meta:
model = SignUp
def __init__(self, *args, **kwargs):
self.ip_addr = kwargs.pop('ip_addr', None)
super(SignUpForm, self).__init__(*args, **kwargs)
def clean_ip(self):
return self.ip_addr