【问题标题】:NOT NULL constraint failed: news_comment.post_idNOT NULL 约束失败:news_comment.post_id
【发布时间】:2017-12-31 17:08:44
【问题描述】:

我跳进了这个错误。 我的 Models.py 文件是

class Post(models.Model):
    user = models.ForeignKey(User,related_name='posts')
    created_at = models.DateTimeField(auto_now=True)
    message = models.TextField()
    message_html = models.TextField(editable=False)

    def __str__(self):
        return self.message

    def save(self,*args,**kwargs):
        self.message_html = misaka.html(self.message)
        super().save(*args,**kwargs)

    def get_absolute_url(self):
        return reverse('news:single',kwargs={'username':self.user.username,'pk':self.pk})

    class Meta():
        ordering = ['-created_at']

class Comment(models.Model):
    post = models.ForeignKey('news.Post',related_name='comments')
    aurthor = models.CharField(blank=False, max_length=100)
    comment = models.TextField(blank=True)
    created_date = models.DateTimeField(auto_now = True)
    comment_html = models.TextField(editable = False)

    def save(self,*args,**kwargs):
        self.comment_html = misaka.html(self.comment)
        super().save(*args,**kwargs)

    def get_absolute_url(self):
        return reverse('news:single',kwargs={'username':self.user.username,'pk':self.pk})

    def __str__(self):
        return self.comment

我的 Views.py 文件是

class CommentCreateView(LoginRequiredMixin,generic.CreateView):
    model = models.Comment
    fields = ('comment',)

    login_url = "/users/login"

    def form_valid(self,form,*args,**kwargs):
        self.object = form.save(commit = False)
        self.object.aurthor = self.request.user
        #self.object.post_id = self.kwargs['pk']
        #print(self.request,self.kwargs['pk'])
        self.object.save()
        return super().form_valid(form)

我得到的错误是

IntegrityError at /posts/4/comment/
NOT NULL constraint failed: news_comment.post_id
Request Method: POST
Request URL:    http://localhost:8000/posts/4/comment/
Django Version: 1.11.3
Exception Type: IntegrityError
Exception Value:    
NOT NULL constraint failed: news_comment.post_id
Exception Location: C:\Users\Sahil\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 328
Python Executable:  C:\Users\Sahil\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version: 3.6.0
Python Path:    
['C:\\Users\\Sahil\\Documents\\GitHub\\news-for-good\\my_app',
 'C:\\Users\\Sahil\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip',
 'C:\\Users\\Sahil\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
 'C:\\Users\\Sahil\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
 'C:\\Users\\Sahil\\AppData\\Local\\Programs\\Python\\Python36-32',
 'C:\\Users\\Sahil\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages']
Server time:    Tue, 25 Jul 2017 16:21:26 +0000

谁能告诉我错误是什么。

如果您需要其余代码This My Github Repo

在此先感谢... 谢谢你:)

【问题讨论】:

    标签: python django sqlite django-models django-views


    【解决方案1】:

    您忘记在视图中添加 Post foreignkey 字段保存。因为您没有在 Comment 模型的 foreignkey 后字段中定义 null=True。因此,您需要在保存评论模型实例时分配 Post 模型实例

    def form_valid(self,form,*args,**kwargs):
            self.object = form.save(commit = False)
            self.object.aurthor = self.request.user
            self.object.post = # Assign Post model instance
            self.object.save()
            return super().form_valid(form)
    

    【讨论】:

    • 我们如何评估后模型
    猜你喜欢
    • 2015-04-22
    • 2018-08-24
    • 2022-01-19
    • 2021-11-16
    • 2020-12-18
    • 2021-03-20
    • 2020-02-11
    • 2019-02-13
    • 2019-05-06
    相关资源
    最近更新 更多