Reputation: 113
i'm having some trouble with forms in django 1.5. i'm trying to write a form that saves new comments on my database. But it doesn't matter what i write in the comment textspace, it's always considered null
this is my model
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)
this is the form
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
and in the end this is my view
@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))
anyone knows why?
Upvotes: 0
Views: 41
Reputation: 4576
You need to return your cleaned comment.
def clean_comment(self):
cleaned_data = self.cleaned_data
cleaned_comment = cleaned_data.get('comment',Decimal('0.00'))
return cleaned_comment
Upvotes: 2