Skip to content

Commit dd3e82d

Browse files
committed
Updates for TokenAdmin.
- Respect USERNAME_FIELD of the user model. - Default ordering by username. - Filter by creation date.
1 parent 365d409 commit dd3e82d

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

rest_framework/authtoken/admin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ def url_for_result(self, result):
2323

2424
class TokenAdmin(admin.ModelAdmin):
2525
list_display = ('key', 'user', 'created')
26+
list_filter = ('created',)
2627
fields = ('user',)
27-
search_fields = ('user__username',)
28+
search_fields = ('user__%s' % User.USERNAME_FIELD,)
2829
search_help_text = _('Username')
29-
ordering = ('-created',)
30+
ordering = ('user__%s' % User.USERNAME_FIELD,)
3031
actions = None # Actions not compatible with mapped IDs.
3132

3233
def get_changelist(self, request, **kwargs):

tests/test_authtoken.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import importlib
22
from io import StringIO
3+
from unittest.mock import patch
34

45
import pytest
56
from django.contrib.admin import site
@@ -11,11 +12,10 @@
1112
from rest_framework.authtoken.admin import TokenAdmin
1213
from rest_framework.authtoken.management.commands.drf_create_token import \
1314
Command as AuthTokenCommand
14-
from rest_framework.authtoken.models import Token
15+
from rest_framework.authtoken.models import Token, TokenProxy
1516
from rest_framework.authtoken.serializers import AuthTokenSerializer
1617
from rest_framework.exceptions import ValidationError
1718

18-
1919
class AuthTokenTests(TestCase):
2020

2121
def setUp(self):
@@ -36,6 +36,28 @@ def test_model_admin_displayed_fields(self):
3636
token_admin = TokenAdmin(self.token, self.site)
3737
assert token_admin.get_fields(mock_request) == ('user',)
3838

39+
def test_model_admin__username_field(self):
40+
import rest_framework.authtoken.admin as authtoken_admin_m
41+
42+
class EmailUser(User):
43+
USERNAME_FIELD = 'email'
44+
username = None
45+
46+
with patch('django.contrib.admin.site.register'): # avoid duplicate registrations
47+
for user_model in (User, EmailUser):
48+
with (
49+
self.subTest(user_model=user_model),
50+
patch('django.contrib.auth.get_user_model', return_value=user_model) as get_user_model
51+
):
52+
importlib.reload(authtoken_admin_m) # reload after patching
53+
assert get_user_model.call_count == 1
54+
55+
mock_request = object()
56+
token_admin = authtoken_admin_m.TokenAdmin(TokenProxy, self.site)
57+
assert token_admin.get_search_fields(mock_request) == (f'user__{user_model.USERNAME_FIELD}',)
58+
assert token_admin.get_ordering(mock_request) == (f'user__{user_model.USERNAME_FIELD}',)
59+
importlib.reload(authtoken_admin_m) # restore after testing
60+
3961
def test_token_string_representation(self):
4062
assert str(self.token) == 'test token'
4163

0 commit comments

Comments
 (0)