IntegrityError at /comments/post/ in django comments

357 views Asked by At

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")
  1. This is not specific to any particular model, but happens on whichever model the comment is posted.
  2. This error does not come when the comment is posted as an anonymous user
  3. 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.

1

There are 1 answers

2
Chris Pratt On

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 the contenttypes 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:

{% render_comment_form for [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:

{% get_comment_form for [owner] as 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.