Coder Social home page Coder Social logo

Error: An admin for model "User" has to be registered to be referenced by TokenAdmin.autocomplete_fields. about django-rest-framework HOT 6 CLOSED

tomchristie avatar tomchristie commented on June 12, 2024 7
Error: An admin for model "User" has to be registered to be referenced by TokenAdmin.autocomplete_fields.

from django-rest-framework.

Comments (6)

Mehourka avatar Mehourka commented on June 12, 2024 2

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

from django-rest-framework.

alexdlaird avatar alexdlaird commented on June 12, 2024 1

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?

from django-rest-framework.

browniebroke avatar browniebroke commented on June 12, 2024 1

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.

from django-rest-framework.

alexdlaird avatar alexdlaird commented on June 12, 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.

from django-rest-framework.

alexdlaird avatar alexdlaird commented on June 12, 2024

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

from django-rest-framework.

hosamhamdy258 avatar hosamhamdy258 commented on June 12, 2024

temporary solution

added search_fields in my custom admin class

search_fields = ("USERNAME_FIELD",)

from django-rest-framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.