Skip to content
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

Error: An admin for model "User" has to be registered to be referenced by TokenAdmin.autocomplete_fields. #9300

Closed
tomchristie opened this issue Mar 18, 2024 Discussed in #9296 · 6 comments · Fixed by #9301
Labels

Comments

@tomchristie
Copy link
Member

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).

@alexdlaird
Copy link
Contributor

alexdlaird commented Mar 18, 2024

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 TokenAdmin:

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 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?

@browniebroke
Copy link
Contributor

I noticed that you have a custom User model, right? So I think what happens is:

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. If folks want auto-complete fields, they can fix it in user land.

@alexdlaird
Copy link
Contributor

alexdlaird commented Mar 18, 2024

Agreed I think that PR should be reverted. DRF can either set TokenAdmin.autocomplete_fields as it does here, or call admin.site.register(TokenProxy, TokenAdmin), as it does here.

It can't do both, making this decision is too implementation-specific and thus won't work in all cases.

@alexdlaird
Copy link
Contributor

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",)

@Mehourka
Copy link

I've got the same issue, reverted back to 3.14.0.

@hosamhamdy258
Copy link

temporary solution

added search_fields in my custom admin class

search_fields = ("USERNAME_FIELD",)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants