Skip to content

Instantly share code, notes, and snippets.

@thelonecabbage
Last active September 13, 2022 12:22
Show Gist options
  • Save thelonecabbage/ad30237a6962213ce6f9 to your computer and use it in GitHub Desktop.
Save thelonecabbage/ad30237a6962213ce6f9 to your computer and use it in GitHub Desktop.
django, current user

(cliped from http://stackoverflow.com/a/21786764/117292)

The least obstrusive way is to use a CurrentUserMiddleware to store the current user in a thread local object:

current_user.py

from threading import local

_user = local()

class CurrentUserMiddleware(object):
    def process_request(self, request):
        _user.value = request.user

def get_current_user():
    return _user.value

Now you only need to add this middleware to your MIDDLEWARE_CLASSES after the authentication middleware.

##settings.py

MIDDLEWARE_CLASSES = (
    ...
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    ...
    'current_user.CurrentUserMiddleware',
    ...
)

Your model can now use the get_current_user function to access the user without having to pass the request object around.

##models.py

from django.db import models
from current_user import get_current_user

class MyModel(models.Model):
    created_by = models.ForeignKey('auth.User', default=get_current_user)

##Hint:

If you are using Django CMS you do not even need to define your own CurrentUserMiddleware but can use cms.middleware.user.CurrentUserMiddleware and the cms.utils.permissions.get_current_user function to retrieve the current user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment