Skip to content

Commit e5e67b2

Browse files
authored
Refactor admin registration for AccessToken and APIKey
1 parent dbe0aac commit e5e67b2

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

cvat/apps/access_tokens/admin.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
# SPDX-License-Identifier: MIT
44

55
from 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

810
from .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+
2737
admin.site.register(AccessToken, AccessTokenAdmin)

0 commit comments

Comments
 (0)