drf-api-tracking icon indicating copy to clipboard operation
drf-api-tracking copied to clipboard

Fix for 'NoneType' object has no attribute 'is_anonymous'

Open lgellert opened this issue 3 years ago • 0 comments

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

lgellert avatar Nov 02 '21 19:11 lgellert