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.
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.userfield nullable and set the user to
- 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
userfield and a foreign key to the new model or replace to a GenericForeignKey
CodenameTim Thanks for your details explanations.