Repository: encode/django-rest-framework

Issue:
Error: An admin for model "User" has to be registered to be referenced by TokenAdmin.autocomplete_fields.
### Discussed in https://github.com/encode/django-rest-framework/discussions/9296

<div type='discussions-op-text'>

<sup>Originally posted by **kevinrenskers** March 18, 2024</sup>
After updating to DRF 3.15.0 I am now getting the following error when starting my Django server:

`<class 'rest_framework.authtoken.admin.TokenAdmin'>: (admin.E039) An admin for model "User" has to be registered to be referenced by TokenAdmin.autocomplete_fields.`

I am using a custom AdminSite instance where only the ModelAdmins that I specifically register to it are shown in my admin site. I have not registered TokenAdmin to my AdminSite, because it's not something we want to see in our admin site. I also have a custom User model, but its ModelAdmin is registered with my custom AdminSite.

It seems that DRF can't handle this situation. I guess that it only looks through the default AdminSite for what is registered there? How can I make DRF just ignore the TokenAdmin? I don't want it in our admin site, as we don't use tokens (only sessions).</div>


Discussion:
I am also now seeing this error after bumping from 3.14.0 to 3.15.0. From Google, it appears this is coming from Django's command validation, not DRF specifically, but I believe it was introduced to DRF via [this pull request](https://github.com/encode/django-rest-framework/pull/8534/files), and it's unclear how to resolve the issue in DRF. The error suggests registering an admin model for "User", but [I already have been](https://github.com/HeliumEdu/platform/blob/1b5560eef0c90c29ae601804165266e113a54e09/helium/auth/admin.py#L123), and doing so before registering the `TokenAdmin`:

I do extend the AdminSite:

```python
class PlatformAdminSite(AdminSite):
    """
    Creates a base AdminSite. Models and URLs should be attached to an instance of this class.
    """
    site_header = settings.PROJECT_NAME + ' Administration'
    site_title = site_header
    index_title = settings.PROJECT_NAME

admin_site = PlatformAdminSite()
```

And register against that:

```python
from django.contrib.auth import admin, get_user_model
from rest_framework.authtoken import admin as drf_admin
from rest_framework.authtoken.models import Token

class UserAdmin(admin.UserAdmin, BaseModelAdmin):
    # ... My extended UserAdmin code

class TokenAdmin(drf_admin.TokenAdmin):
    # ... My extended TokenAdmin code

# Then register the models to the admin
admin_site.register(get_user_model(), UserAdmin)
admin_site.register(Token, TokenAdmin)
```

It fails with this error. If I register directly against `admin.site.register` instead of my extended `AdminSite`, it works again. So what has changed with this release, and what do we need to change? Do we need to register something elsewhere, or has the way we need to extend `AdminSite` changed?
I noticed that you have a custom `User` model, right? So I _think_ what happens is:

- Django register the default User model with the default admin
- DRF register the TokenProxy model with the default admin. But this model references your custom User model: https://github.com/encode/django-rest-framework/blob/337ba211e82628c0a0bfc7340c45b22e725f8162/rest_framework/authtoken/admin.py#L11

However, Django checks that the `autocomplete_fields` matches a model which is registered in the admin, but the user model registered in the default admin is the default one, not the custom one.

The simplest thing might be to revert the PR that caused it (I was waiting for this specific fix, but it doesn't work in all cases) and perhaps expand the documentation, [which already explains how to patch the `TokenAdmin`](https://www.django-rest-framework.org/api-guide/authentication/#with-django-admin). If folks want auto-complete fields, they can fix it in user land.

Resolving patch:
diff --git a/rest_framework/authtoken/admin.py b/rest_framework/authtoken/admin.py
--- a/rest_framework/authtoken/admin.py
+++ b/rest_framework/authtoken/admin.py
@@ -28,7 +28,6 @@ class TokenAdmin(admin.ModelAdmin):
     search_help_text = _('Username')
     ordering = ('-created',)
     actions = None  # Actions not compatible with mapped IDs.
-    autocomplete_fields = ("user",)
 
     def get_changelist(self, request, **kwargs):
         return TokenChangeList


Test patch: