Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么会出现空约束错误?_Python_Django_Postgresql - Fatal编程技术网

Python 为什么会出现空约束错误?

Python 为什么会出现空约束错误?,python,django,postgresql,Python,Django,Postgresql,我试图给事实(帖子)添加评论。当我试图提交评论时,会出现以下错误?我正在使用Postgres供参考 IntegrityError at /fc/2/comment/ null value in column "comment_id" violates not-null constraint DETAIL: Failing row contains (8, It has plugins too, 2018-10-03 07:41:25.249524+00, 1, null). Exceptio

我试图给事实(帖子)添加评论。当我试图提交评论时,会出现以下错误?我正在使用Postgres供参考

IntegrityError at /fc/2/comment/
null value in column "comment_id" violates not-null constraint
DETAIL:  Failing row contains (8, It has plugins too, 2018-10-03 07:41:25.249524+00, 1, null).

Exception Value:    
null value in column "comment_id" violates not-null constraint
DETAIL:  Failing row contains (8, It has plugins too, 2018-10-03 07:41:25.249524+00, 1, null).
型号:

class Fact(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=200)
    text = models.TextField()
    created_date = models.DateTimeField(
        default=timezone.now)
    published_date = models.DateTimeField(
        blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

class Comment(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    comment = models.ForeignKey('fc.Fact', on_delete=models.CASCADE, related_name='comments')
    text = models.TextField()
    created_date = models.DateTimeField(default=timezone.now)
视图:

表单视图:

{% extends 'base.html' %}

{% block content %}

    <h1>Check this fact</h1>
    <form method="POST" class="post-form">{% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="save btn btn-default">Save</button>
    </form>

{% endblock %}
为什么注释id为空,我本以为Django会像我的事实模型那样自动填充它

谢谢你在这方面的帮助

谢谢。

应该是

comment.comment = fc

因此,你的观点将是正确的

def add_comment_to_post(request, pk):
    fc = get_object_or_404(Fact, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.comment = fc # change is here <<<
            comment.save()
            return redirect('fc_detail', pk=fc.pk)
    else:
        form = CommentForm()
    return render(request, 'add_comment_to_post.html', {'form': form})
def将注释添加到帖子(请求,主键):
fc=获取对象或404(事实,pk=pk)
如果request.method==“POST”:
表单=评论表单(request.POST)
如果form.is_有效():
comment=form.save(commit=False)
comment.comment=fc#变化就在这里应该是这样的

comment.comment = fc

因此,你的观点将是正确的

def add_comment_to_post(request, pk):
    fc = get_object_or_404(Fact, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.comment = fc # change is here <<<
            comment.save()
            return redirect('fc_detail', pk=fc.pk)
    else:
        form = CommentForm()
    return render(request, 'add_comment_to_post.html', {'form': form})
def将注释添加到帖子(请求,主键):
fc=获取对象或404(事实,pk=pk)
如果request.method==“POST”:
表单=评论表单(request.POST)
如果form.is_有效():
comment=form.save(commit=False)

comment.comment=fc#变化就在这里,非常完美,谢谢。Is comment.comment=comment\u id?
comment.comment
需要一个整数,表示有效的
Fact
实例或有效的
Fact
实例的PK值。除此之外,
comment
comment\u id
几乎相同,都表示FK关系完美,谢谢。Is comment.comment=comment\u id?
comment.comment
需要一个整数,表示有效的
Fact
实例或有效的
Fact
instanceMoreover的PK值,
comment
comment\u id
几乎相同,都表示FK关系
def add_comment_to_post(request, pk):
    fc = get_object_or_404(Fact, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.comment = fc # change is here <<<
            comment.save()
            return redirect('fc_detail', pk=fc.pk)
    else:
        form = CommentForm()
    return render(request, 'add_comment_to_post.html', {'form': form})