Skip to content

user_id always None for DRF requests when using RequestMiddleware #37

@immortaleeb

Description

@immortaleeb

In my Django application I'm using the django_structlog.middlewares.RequestMiddleware to log all of the requests to my application.
I noticed that user_id is always None for all the calls made to my Django Rest Framework (DRF) urls.

Now, I know this is because RequestMiddleware tries to access request.user, which is only filled in when you're going through the Django authentication mechanism, which DRF bypasses completely. Instead, it performs authentication in the view layer, so after all middleware code has run.

I guess other people have encountered this too so I was wondering if there is any workaround so that I can track which user performed which request from "the start" until "the end" of the request?

I tried several things, but all of them feel like I'm fighting with DRF to get it to do something it doesn't support.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions