inserting a column on a django modelform
I'm writing a small survey app and the main model looks like this:
class survey(models.Model):
ip_address = models.IPAddressField()
question1 = models.IntegerField()
question2 = models.IntegerField()
question3 = models.IntegerField()
I also have a modelform that looks like this:
class countingForm(forms.ModelForm):
question1 = forms.ChoiceField()
question2 = forms.ChoiceField()
question3 = forms.ChoiceField()
class Meta:
model=survey
exclude=['ip_address']
I'd like to record the IP address of the person submitting each form, but I am unsure how to add this after the m开发者_开发知识库odelform comes back. As an example, this does not work:
form.cleaned_data['ip_address']=request.META['REMOTE_ADDR']
form.save()
I'd rather not add the IP address in as a hidden field. Suggestions?
obj = form.save(commit=False)
obj.ip_address = request.META['REMOTE_ADDR']
obj.save()
on the separate note, request may not have a REMOTE_ADDR field, so you should check your specific installation.
精彩评论