IntegrityError at /forum/post/15/comment/new/: NOT NULL constraint failed: forum_comment.name_id

Viewed 20

I'm trying to implement a commenting feature for my blog style project, but I'm getting the following error:

IntegrityError at /forum/post/15/comment/new/
NOT NULL constraint failed: forum_comment.name_id

I suspect that the error has something to do with the comment author because I was reading similar posts where that was the problem, however I wasn't able to fully comprehend or adapt their problem onto mine because of the way I have setup my comments.

Here's how my project setup looks:

models.py:

class Post(models.Model):
    titulo  = models.CharField(max_length=150)
    contenido = MarkdownxField()
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    
    def formatted(self):
        return markdownify(self.contenido)

    def __str__(self):
        return self.titulo

    def get_absolute_url(self):
        return reverse("post-detail", kwargs={"pk": self.pk})
    
class Comment(models.Model):
    post = models.ForeignKey(Post, related_name="comments", on_delete=models.CASCADE)
    name = models.ForeignKey(User, on_delete=models.CASCADE)
    body = MarkdownxField()
    date_added = models.DateTimeField(default=timezone.now)
    
    def __str__(self):
        return '%s - %s' % (self.post.titulo, self.name)

views.py:

class CommentCreateView(CreateView):
    model = Comment
    form_class = CommentForm
    #fields = ['body']
    template_name = "forum/comment_form.html"

        
    class Meta:
        ordering=['-time']
    
    def form_valid(self, form):
        form.instance.post = Post.objects.get(pk=int(self.kwargs['pk']))
        return super().form_valid(form)
    
    def get_success_url(self):   
        return reverse_lazy('') 

forms.py:

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ['body']
        
        widgets = {
            'body': MarkdownxField()
        }

urls.py:

path('post/<int:pk>/comment/new/', CommentCreateView.as_view(), name='comment-create'),    

Any kind of help would be greatly appreciated, thanks in advance...

0 Answers
Related