encode / django-rest-framework

Web APIs for Django. 🎸
https://www.django-rest-framework.org
Other
28.45k stars 6.84k forks source link

Issues sorting authtokens in django admin #8830

Closed FeralRobot closed 1 year ago

FeralRobot commented 1 year ago

Discussed in https://github.com/encode/django-rest-framework/discussions/8821

Originally posted by **FeralRobot** December 30, 2022 I want to be able to sort authtokens alphabetically by username in the django admin I am using django 3.2.13, django restframework 3.13.1, and I am using a custom user model based on AbstractUser. The custom user model has no modifications at this moment. This code, placed in the admin.py for the custom user app, allows me to get the alphabetic sort I want initially. There are two issues though. The first is that the search_fields line is required if I want the alphabetic sort. The second issue occurs when I click on the User column header (see green arrows). My expectation is clicking there would toggle back and forth between ascending and descending alphabetic order. Instead the sort order changes to something that looks like user_id or created by order, and I lose the alphabetic order. I can recover the alphabetic order by returning to the admin homepage and re-entering the tokens view. These behaviors look like bugs to me, and I would like to report them as a Github issue in the DRF repo. That repo refers me here first for discussion. After five days in the discussion forum I moved this into an issue here. ``` from rest_framework.authtoken.models import TokenProxy from rest_framework.authtoken.admin import TokenAdmin class CustomTokenAdmin(TokenAdmin): list_display = ('user', 'key', 'created') search_fields = ['user__email','user__username'] ordering = ('user__username',) admin.site.unregister(TokenProxy) admin.site.register(TokenProxy, CustomTokenAdmin) ``` ![image](https://user-images.githubusercontent.com/38778116/210096529-9c33fd56-33a2-4837-a3d4-77da7df55d88.png)
auvipy commented 1 year ago

what suggested fix do you have in mind?

OmerFI commented 1 year ago

I have attempted to reproduce the problem using both the most recent releases of Django and DRF, as well as the specific versions you have mentioned, but I have not been able to reproduce the issue.

https://github.com/encode/django-rest-framework/assets/54645034/505af224-b2ab-461b-a51b-b0503409f7a4

auvipy commented 1 year ago

I'm going to closing the issue as per the video proof but keeping the discussion open.