drf-api-tracking
drf-api-tracking copied to clipboard
Fix for 'NoneType' object has no attribute 'is_anonymous'
With LoggingMixin set on my ViewSet, if an anonymous user hits an endpoint, drf_api_tracking throws an error:
'NoneType' object has no attribute 'is_anonymous'
It comes from BaseLoggingMixin._get_user()
, specifically the line where it does if user.is_anonymous:
since user is None in this case it fails.
A temporary work around for now is to do a monkey patch like so:
# monkey patch a bug in rest_framework_tracking
def fixed_get_user(self, request):
user = request.user
if not user or user.is_anonymous:
return None
return user
BaseLoggingMixin._get_user = fixed_get_user