Skip to content

Commit 5ff42dc

Browse files
committed
Update TokenAdmin to respect USERNAME_FIELD of the user model.
1 parent 365d409 commit 5ff42dc

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

rest_framework/authtoken/admin.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def url_for_result(self, result):
2424
class TokenAdmin(admin.ModelAdmin):
2525
list_display = ('key', 'user', 'created')
2626
fields = ('user',)
27-
search_fields = ('user__username',)
2827
search_help_text = _('Username')
2928
ordering = ('-created',)
3029
actions = None # Actions not compatible with mapped IDs.
@@ -45,6 +44,13 @@ def get_object(self, request, object_id, from_field=None):
4544
except (queryset.model.DoesNotExist, User.DoesNotExist, ValidationError, ValueError):
4645
return None
4746

47+
def get_search_fields(self, request):
48+
if hasattr(self, 'search_fields'):
49+
return self.search_fields
50+
user_model = Token._meta.get_field('user').related_model
51+
username_lookup = 'user__%s' % user_model.USERNAME_FIELD
52+
return (username_lookup,)
53+
4854
def delete_model(self, request, obj):
4955
# Map back to actual Token, since delete() uses pk.
5056
token = Token.objects.get(key=obj.key)

0 commit comments

Comments
 (0)