-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Error: An admin for model "User" has to be registered to be referenced by TokenAdmin.autocomplete_fields. #9300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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, 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, and doing so before registering the I do extend the AdminSite: 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: 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 |
I noticed that you have a custom
However, Django checks that the 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 |
Created a PR to revert. For those looking to apply the same logic and enable auto-complete once the revert PR is merged, here is the code: from rest_framework.authtoken.admin import TokenAdmin
TokenAdmin.autocomplete_fields = ("user",) |
I've got the same issue, reverted back to 3.14.0. |
temporary solution added search_fields in my custom admin class
|
Discussed in #9296
Originally posted by kevinrenskers March 18, 2024
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).
The text was updated successfully, but these errors were encountered: