简体   繁体   English

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

[英]Why am I getting a null constraint error?

I'm Trying to add comments to Facts(posts). 我正在尝试向事实添加评论。 When I try to submit a comment I get the following error? 当我尝试提交评论时,出现以下错误? I'm using Postgres FYI 我正在使用Postgres FYI

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).

Model: 模型:

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)

View: 视图:

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.fc = fc
        comment.save()
        return redirect('fc_detail',pk=fc.pk)
else:
    form =CommentForm()
return render(request,'add_comment_to_post.html',{'form':form})

Form view: 表格检视:

{% 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 %}

Form: 形成:

class FcForm(forms.ModelForm):

class Meta:
    model = Fact
    fields = ('title', 'text',)

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('author', 'text',)

Why is the comment_id null, I would have thought Django auto-populates this as it did with my Fact model. 为什么comment_id为null,我会认为Django会像使用Fact模型那样自动填充它。

Appreciate help on this. 对此有帮助。

Thank you. 谢谢。

It should be 它应该是

comment.comment = fc

instead of 代替

comment.fc = fc


hence your view will be 因此您的看法将是

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})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM