diff --git a/rest_framework/authentication.py b/rest_framework/authentication.py index f0702286c..8b5c20090 100644 --- a/rest_framework/authentication.py +++ b/rest_framework/authentication.py @@ -8,6 +8,7 @@ from django.middleware.csrf import CsrfViewMiddleware from django.utils.translation import ugettext_lazy as _ from rest_framework import exceptions, HTTP_HEADER_ENCODING from rest_framework.authtoken.models import Token +from rest_framework.compat import get_user_model def get_authorization_header(request): @@ -85,7 +86,12 @@ class BasicAuthentication(BaseAuthentication): """ Authenticate the userid and password against username and password. """ - user = authenticate(username=userid, password=password) + username_field = getattr(get_user_model(), 'USERNAME_FIELD', 'username') + credentials = { + username_field: userid, + 'password': password + } + user = authenticate(**credentials) if user is None: raise exceptions.AuthenticationFailed(_('Invalid username/password.')) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index c6a4a8698..1ba907314 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -119,6 +119,14 @@ def get_model_name(model_cls): return model_cls._meta.module_name +# Support custom user models in Django 1.5+ +try: + from django.contrib.auth import get_user_model +except ImportError: + from django.contrib.auth.models import User + get_user_model = lambda: User + + # View._allowed_methods only present from 1.5 onwards if django.VERSION >= (1, 5): from django.views.generic import View