Wednesday, May 27, 2020

RES: IntegrityError: NOT NULL constraint failed

In your Comment model you have a author field wich is a ForeignKey. The error is pointing exactly to that field, since it is a not null field. To overcome this you must pick the ID of the logged user and set the author field with the user ID. You can do that in your views.py the same way as you did in new_comment.post = post, only changing .post for .author and matching it with request.user. the line will look like this new_comment.author = request.user. remember to put it before new_comment.save()

 

De: sunday honesty
Enviado:quarta-feira, 27 de maio de 2020 19:36
Para: Django users
Assunto: IntegrityError: NOT NULL constraint failed

 

I try to let user add comments to blog post am making... When I run makemigrations and migrate, everything seemed fine . The form displayed well but shows the following error when I fill the form and click on the submit button. Django.db.utils.IntegrityError: NOT NULL constraint failed: blog_comment.author_id

 

Am new to Django and following a tutorial. The tutorial doesn't have users except the super user. I learnt about users and so I let user register to use the blog. The tutorial provided a name field in the form so commenter can enter their name. Here, I want to use the current user for this field(see my models.py below to see how I have done this). Any help to solve this will be appreciated.

 

PS: I have seen similar questions like this and deleted my migrations file and re-ran migrations but it didn't still work.

 

#models.py

class Comment(models.Model):

    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')

    author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE,)

    comment = models.TextField()  

    created = models.DateTimeField(auto_now_add=True) 

    updated = models.DateTimeField(auto_now=True)   

    active = models.BooleanField(default=True)

 

    class Meta:

        ordering = ('created',)

 

    def __str__(self):

        return f'Comment by {self.author} on {self.post}'

 

#forms.py

class CommentForm(forms.ModelForm):

 

    class Meta:

        model = Comment

        fields = ('comment',)

 

#views.py

login_required

def post_detail(request, post, pk):

    post = get_object_or_404(Post, id=pk, slug=post, status='published')

    comments = post.comments.filter(active=True)

    new_comment = None

 

    if request.method == 'POST':

        comment_form = CommentForm(data=request.POST)

        if comment_form.is_valid():

            new_comment = comment_form.save(commit=False)

            new_comment.post = post

            new_comment.save()

    else:

        comment_form = CommentForm()

    return render(request,

        'post_detail.html',

        {'post': post,

        'comments': comments,

        'new_comment': new_comment,

        'comment_form': comment_form})

 

--

You received this message because you are subscribed to the Google Groups "Django users" group.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/87349ba6-bb75-477c-a84b-9e15be428a5b%40googlegroups.com.

 

No comments:

Post a Comment