为什么我在提交django表单时收到的NOTNULL约束失败?

为什么我在提交django表单时收到的NOTNULL约束失败?,django,django-models,django-forms,Django,Django Models,Django Forms,我试图在django中向我的帖子模型添加注释,但当我尝试提交表单时,它会抛出一个NOT NULL constraint failed错误,因为它找不到帖子,即使在保存表单时在视图中分配了帖子 以下是我的模型: class Post(models.Model): title = models.TextField('Title', max_length=300) slug = models.SlugField('Link') created_on = models.DateT

我试图在django中向我的帖子模型添加注释,但当我尝试提交表单时,它会抛出一个NOT NULL constraint failed错误,因为它找不到帖子,即使在保存表单时在视图中分配了帖子

以下是我的模型:

class Post(models.Model):
    title = models.TextField('Title', max_length=300)
    slug = models.SlugField('Link')
    created_on = models.DateTimeField('Creation date', auto_now_add=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
    content = models.TextField('Content', max_length=2000, default='!')

    class Meta:
        ordering = ['-created_on']

    def __str__(self):
        return self.title


class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comment_author')
    body = models.TextField()
    created_on = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created_on']

    def __str__(self):
        return 'Comment {} by {}'.format(self.body, self.user)
这是我的表格:

class CommentForm(forms.ModelForm):

    class Meta:
        model = Comment
        fields = ['user', 'body']
如果我将“post”字段添加到字段中,它会将其显示为下拉列表,并将注释添加到正确的帖子中,即使下拉选项将其指向不同的帖子,但我根本不希望它在表单中显示“post”字段

这是一种观点:

def post_detail(request, slug):
    template_name = 'post_detail.html'
    post = get_object_or_404(Post, slug=slug)
    comments = post.comments.all()
    new_comment = None

    if request.method == 'POST':
        comment_form = CommentForm(data=request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save()
            new_comment.post = post <- this part is being left out for some reason
            new_comment.save()
    else:
        comment_form = CommentForm()

    return render(request, template_name, {'post': post,
                                           'comments': comments,
                                           'new_comment': new_comment,
                                           'comment_form': comment_form})
def post_详细信息(请求、slug):
模板名称='post\u detail.html'
post=获取对象或404(post,slug=slug)
comments=post.comments.all()
新注释=无
如果request.method==“POST”:
评论形式=评论形式(数据=request.POST)
如果注释形式有效():
new\u comment=comment\u form.save()

new_comment.post=post在您的查看功能中,您正在分配帖子之前保存评论表单

所以试着这样做

if request.method == 'POST':
    comment_form = CommentForm(data=request.POST)
    if comment_form.is_valid():
        new_comment = comment_form.save(commit=False) # saves the form but do not save the record to the database
        new_comment.post = post
        new_comment.save() # Now it saves records to the database
if request.method == 'POST':
    comment_form = CommentForm(data=request.POST)
    if comment_form.is_valid():
        new_comment = comment_form.save(commit=False) # saves the form but do not save the record to the database
        new_comment.post = post
        new_comment.save() # Now it saves records to the database