Skip to content

Commit 6019cbe

Browse files
committed
Change admin search_fields to favor USERNAME_FIELD instead of "email".
First nothing guarantees that the user model has a field named "email" as it can be set to a different name using `EMAIL_FIELD`. At the very least the `get_email_field_name` should have been used. Secondly nothing guarantees that `EMAIL_FIELD` is going to be indexed and thus suitable for search purposes. On the other hand `USERNAME_FIELD` must be unique and thus indexed to enforce the constraint and unique identifies users. For these reasons `USERNAME_FIELD` represents a better choice to allow the different toolkit models to be searched by through the admin.
1 parent fd2bcec commit 6019cbe

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Sandro Rodrigues
102102
Shaheed Haque
103103
Shaun Stanworth
104104
Silvano Cerza
105+
Simon Charette
105106
Sora Yanai
106107
Spencer Carroll
107108
Stéphane Raimbault

oauth2_provider/admin.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
)
1616

1717

18-
has_email = hasattr(get_user_model(), "email")
18+
username_field = get_user_model().USERNAME_FIELD
1919

2020

2121
class ApplicationAdmin(admin.ModelAdmin):
@@ -25,36 +25,36 @@ class ApplicationAdmin(admin.ModelAdmin):
2525
"client_type": admin.HORIZONTAL,
2626
"authorization_grant_type": admin.VERTICAL,
2727
}
28-
search_fields = ("name",) + (("user__email",) if has_email else ())
28+
search_fields = ("name", f"user__{username_field}")
2929
raw_id_fields = ("user",)
3030

3131

3232
class AccessTokenAdmin(admin.ModelAdmin):
3333
list_display = ("token", "user", "application", "expires")
3434
list_select_related = ("application", "user")
3535
raw_id_fields = ("user", "source_refresh_token")
36-
search_fields = ("token",) + (("user__email",) if has_email else ())
36+
search_fields = ("token", f"user__{username_field}")
3737
list_filter = ("application",)
3838

3939

4040
class GrantAdmin(admin.ModelAdmin):
4141
list_display = ("code", "application", "user", "expires")
4242
raw_id_fields = ("user",)
43-
search_fields = ("code",) + (("user__email",) if has_email else ())
43+
search_fields = ("code", f"user__{username_field}")
4444

4545

4646
class IDTokenAdmin(admin.ModelAdmin):
4747
list_display = ("jti", "user", "application", "expires")
4848
raw_id_fields = ("user",)
49-
search_fields = ("user__email",) if has_email else ()
49+
search_fields = ("user__email", f"user__{username_field}")
5050
list_filter = ("application",)
5151
list_select_related = ("application", "user")
5252

5353

5454
class RefreshTokenAdmin(admin.ModelAdmin):
5555
list_display = ("token", "user", "application")
5656
raw_id_fields = ("user", "access_token")
57-
search_fields = ("token",) + (("user__email",) if has_email else ())
57+
search_fields = ("token", f"user__{username_field}")
5858
list_filter = ("application",)
5959

6060

0 commit comments

Comments
 (0)