简体   繁体   English

Django ForeignKey null=True IntegrityError

[英]Django ForeignKey null=True IntegrityError

I am making a comment and reply system, where the comments should have parent as null in the database and the replies have the id of the parent comment.我正在制作一个评论和回复系统,其中评论在数据库中的父级应为 null,并且回复具有父级评论的 id。 Below is my comment model, for your reference.下面是我的评论model,供大家参考。 When I am trying to post the comment via my form, I get IntegrityError .当我试图通过我的表单发表评论时,我得到IntegrityError I tried putting both blank=True and null=True , but the error is still there.我尝试同时放置blank=Truenull=True ,但错误仍然存在。 Also, I have figured out from the documentation and other articles that null=True should work, not blank=True .另外,我从文档和其他文章中发现null=True应该起作用,而不是blank=True I cannot understand why I am getting the error and how to fix it.我不明白为什么我会收到错误以及如何解决它。 Thank you in advance.先感谢您。

class BlogComment(models.Model):
    sno= models.AutoField(primary_key=True)
    comment=models.TextField()
    user=models.ForeignKey(User, on_delete=models.CASCADE)
    post=models.ForeignKey(Post, on_delete=models.CASCADE)
    parent=models.ForeignKey('self', on_delete=models.CASCADE, null=True)
    timestamp= models.DateTimeField(default=now)

This was not working because of typo in the name attribute of my form input.由于我的表单输入的名称属性中的拼写错误,这不起作用。 I fixed that and now it is working.我解决了这个问题,现在它正在工作。

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

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