擅长:python、mysql、java
<p>您应该在模型中如下设置字段可选:</p>
<pre><code>class MyComment(Comment):
thumbs_up = models.IntegerField(default=0)
thumbs_down = models.IntegerField(default=0)
</code></pre>
<p>查看<a href="https://docs.djangoproject.com/en/dev/ref/models/fields/#field-options" rel="nofollow">Field options</a>了解更多信息。
把你的姿势改成这样:</p>
<pre><code>class MyCommentForm(CommentForm):
thumbs_up = forms.IntegerField(required=False)
thumbs_down = forms.IntegerField(required=False)
</code></pre>
<p>把<code>get_comment_create_data</code>改成这样:</p>
<pre><code>def get_comment_create_data(self):
data = super(MyCommentForm, self).get_comment_create_data()
data['thumbs_up'] = self.cleaned_data.get('thumbs_up', 0)
data['thumbs_down'] = self.cleaned_data.get('thumbs_down', 0)
return data
</code></pre>