我在django 1.5中遇到了一些问题。 我正在尝试编写一个在我的数据库中保存新注释的表单。 但是我在评论文本空间中所写的内容并不重要,它始终被视为空
这是我的模特
class Comment(BaseModel):
auction_event = models.ForeignKey(AuctionEvent, related_name='comments')
commenter = models.ForeignKey(User, related_name='comments')
comment = models.CharField(max_length=200, default='commento', null=True, blank=True)
def __unicode__(self):
return u'Placed on %s by %s' % (self.auction_event.item.title, self.commenter.username)
这是表格
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['comment']
def __init__(self, data=None, auction_event=None, commenter=None, *args, **kwargs):
self.auction_event = auction_event
self.commenter = commenter
super(CommentForm, self).__init__(data, *args, **kwargs)
def clean_comment(self):
cleaned_data = self.cleaned_data
cleaned_comment = cleaned_data.get('comment',Decimal('0.00'))
def clean(self):
cleaned_data = self.cleaned_data
current_time = timezone.now()
if current_time > self.auction_event.end_time:
raise ValidationError('This auction event has expired.')
return cleaned_data
def save(self, force_insert=False, force_update=False, commit=False):
comment = super(CommentForm, self).save(commit=False)
comment.auction_event = self.auction_event
comment.commenter = self.commenter
comment.save()
self.auction_event.save()
return comment
最后这是我的观点
@login_required
def view_comment_history(request, auction_event_id):
try:
auction_event = AuctionEvent.objects.get(pk=auction_event_id)
except AuctionEvent.DoesNotExist:
raise Http404
comments = auction_event.comments.all()
if request.method == 'POST':
form = CommentForm(data=request.POST, auction_event=auction_event, commenter=request.user.user,)
if form.is_valid():
comments = form.save()
return HttpResponseRedirect(request.get_full_path())
else:
form = CommentForm()
return render_to_response('lebay/view_comment_history.html', {
'auction_event': auction_event,
'form': form,
'comments': comments,
}, context_instance=RequestContext(request))
谁知道为什么?
答案 0 :(得分:2)
您需要返回已清理的评论。
def clean_comment(self):
cleaned_data = self.cleaned_data
cleaned_comment = cleaned_data.get('comment',Decimal('0.00'))
return cleaned_comment