33# SPDX-License-Identifier: MIT
44
55from django .contrib import admin
6- from rest_framework_api_key .admin import APIKey , APIKeyAdmin
6+ from django .contrib .admin .sites import NotRegistered
7+ from rest_framework_api_key .admin import APIKeyAdmin
8+ from rest_framework_api_key .models import APIKey as APIKeyModel
79
810from .models import AccessToken
911
@@ -19,9 +21,17 @@ class AccessTokenAdmin(APIKeyAdmin):
1921 "last_used_date" ,
2022 ]
2123 list_filter = ("created" , "updated_date" , "last_used_date" , "read_only" )
22- search_fields = ("id" , "name" , "prefix" , "owner__username" )
24+ # Use exact-match on id to avoid inefficient substring search on integers
25+ search_fields = ("=id" , "name" , "prefix" , "owner__username" )
2326 autocomplete_fields = ("owner" ,)
2427
2528
26- admin .site .unregister (APIKey )
29+ # Unregister the upstream APIKey model admin if it was registered.
30+ # Guard against NotRegistered to avoid import-time errors if it's absent.
31+ try :
32+ admin .site .unregister (APIKeyModel )
33+ except NotRegistered :
34+ # If not registered, nothing to do.
35+ pass
36+
2737admin .site .register (AccessToken , AccessTokenAdmin )
0 commit comments