The error appears to be occurring because the view isn't checking if the user is authenticated before accessing its attributes. Django uses AnonymousUser to handle unauthenticated user sessions, and this model doesn't have a user_id field, which is causing the 500 error.
Check your view to see if you're ensuring the user is authenticated before attempting to access the user_id. Something like this might help:
if not request.user.is_authenticated:
return JsonResponse({"error": "User not authenticated"}, status=401)
However, to help you better, could you share the code for your full view? Specifically, how you're getting the user and accessing their attributes.