Django Signals how to allow comment from unauthenticated user?

Ok, so using signals has nothing specifically to do with this. You’d have the same problem if you had this code processing in line in your view. There is no ID for an AnonymousUser. How you handle that condition - what you want to have happen - is your decision.

1 Like

KenWhitesell yeah the only solution for me using author id for AnonymousUser or only allow authenticated user for post commnet.

I’d be cautious of making the choice of defaulting an unauthenticated user commenting on the post to be the blog author. You can’t be sure that it is the author of the blog post, so it seems unreasonable to mark it as such.

You do have other options that are worth considering:

  • You could make the Comment.user field nullable and set the user to None when authenticated.
  • You can use a sentinel value for all unauthenticated users.
  • You could define a new way of identifying a user-adjacent model based on a cookie and/or IP address, and use either a nullable user field and a foreign key to the new model or replace to a GenericForeignKey
1 Like

CodenameTim Thanks for your details explanations.