开发者

inserting a column on a django modelform

开发者 https://www.devze.com 2023-02-14 21:56 出处:网络
I\'m writing a small survey app and the main model looks like this: class survey(models.Model): ip_address = models.IPAddressField()

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号