Ticket #27524: 0001-Use-get_user_model-instead-of-user-instance-to-call-.patch

File 0001-Use-get_user_model-instead-of-user-instance-to-call-.patch, 1.5 KB (added by Andy Martin, 7 years ago)

Patch file for proposed fix (will also be submitted as a PR on Github)

  • django/contrib/auth/__init__.py

    From 156d7dfed3c26eae214e85909f93e41682dcc22e Mon Sep 17 00:00:00 2001
    From: Andy Martin <andy.martin@schrodinger.com>
    Date: Tue, 22 Nov 2016 11:44:56 -0500
    Subject: [PATCH] Use get_user_model() instead of user instance to call class
     method
    
    Using get_user_model() instead of the user instance avoids issues
    when a custom user model is provided.
    
    For example, when the user model was overridden, we were seeing
    errors like this:
    
    File "[...snip...]/django/contrib/auth/__init__.py", line 111, in login
        request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
    AttributeError: 'MetaDict' object has no attribute 'pk'
    
    Going through the object returned by get_user_model() also matches
    what's done in other places in the codebase (for example, in
    _get_user_session_key(request)).
    ---
     django/contrib/auth/__init__.py | 2 +-
     1 file changed, 1 insertion(+), 1 deletion(-)
    
    diff --git a/django/contrib/auth/__init__.py b/django/contrib/auth/__init__.py
    index 7adb8a0..99560cd 100644
    a b def login(request, user, backend=None):  
    133133                '`backend` attribute on the user.'
    134134            )
    135135
    136     request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
     136    request.session[SESSION_KEY] = get_user_model()._meta.pk.value_to_string(user)
    137137    request.session[BACKEND_SESSION_KEY] = backend
    138138    request.session[HASH_SESSION_KEY] = session_auth_hash
    139139    if hasattr(request, 'user'):
Back to Top