django 评论中 /comments/post/ 处的 IntegrityError
我已经使用 Django 内置注释框架有一段时间了,它工作得非常好。由于测试后我们最初处于开发阶段,因此我们没有尝试。
但昨天我发布了一条评论,只是为了好玩,我遇到了这个错误。
IntegrityError at /comments/post/
(1048, "Column 'content_type_id' cannot be null")
- 这并不特定于任何特定模型,但会发生在发布评论的任何模型上。
- 当评论以匿名用户身份发布时,不会出现此错误
- 评论正在发布
我无法理解是什么导致了此错误,我们一直在开发的其他内容没有以任何方式干扰评论应用程序。
我知道我提供的信息很少,但任何形式的帮助将不胜感激。
I have been using Django inbuilt comments framework, for some time and it was working absolutely fine. Since we are in development phase initially after testing it, we did not try it out.
But yesterday I posted a comment, just for the fun of it and I landed with this error
IntegrityError at /comments/post/
(1048, "Column 'content_type_id' cannot be null")
- This is not specific to any particular model, but happens on whichever model the comment is posted.
- This error does not come when the comment is posted as an anonymous user
- The comment is getting posted
I fail to understand what could have caused this error, the other things that we have been developing, have not in any way interfered with the comments app.
I know I have put very little information, but any kind of help will be really appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您提供的信息不确定为什么会收到该错误,但通常
comments
包使用contenttypes
包中的通用外键将评论链接到无论它“属于”什么。您收到的错误是因为(无论出于何种原因)保存评论时评论“所属”的内容未定义。提交评论的默认表单实际上包含它应该“所属”的上下文对象,并在提交评论时将此数据与 POST 一起传递。通常,您将使用
{% render_comment_form %}
模板标记显示此表单并传入所有者:其中
[owner]
评论所属的对象。或者您可以使用
{% get_comment_form %}
标记来自定义表单:如果您使用了完全自定义的方式来包含表单,则应该检查以确保您是传递这两个标签默认包含的所有隐藏值。
此外,如果您要自定义表单,请务必使用
{% comment_form_target %}
设置表单的操作。希望这足以帮助您进一步解决问题。
Not sure based on the information you've provided why you would be getting that error, but generally,
comments
package uses generic foreign keys from thecontenttypes
package to link the comment to whatever it "belongs" to. The error you're getting is because (for whatever reason) what the comment "belongs" to is undefined when saving the comment.The default form to submit comments actually includes the contextual object that it should "belong" and passes this data along with the POST when the comment is submitted. Normally, you would display this form using
{% render_comment_form %}
template tag and pass in the owner:Where
[owner]
the object that the comment would belong to.Or you might use the
{% get_comment_form %}
tag to be able to customize the form:If you've used an entirely custom way of including the form, you should check to make sure that you're passing all the hidden values that either of those two tags would include by default.
Additionally, if you are customizing the form, it's important to set the form's action with the
{% comment_form_target %}
.Hopefully, that will be enough to help you further troubleshoot the problem.