How do I register a model that is already registered in admin?

dowjones123 picture dowjones123 · Sep 21, 2014 · Viewed 12k times · Source

I am trying to change the field order in the admin of a field in a django package (rest framework)

I define a new adminmanager, but get a 'Model Already Registered' Error. Surely there must be a way to do it?

from rest_framework.authtoken.models import Token
class AuthTokenAdmin(admin.ModelAdmin):
    list_display = ('user', 'key',)


admin.site.register(Token, AuthTokenAdmin)

Answer

karthikr picture karthikr · Sep 21, 2014

The reason this error occurs is, the class Token has already been registered with an admin class like this:

from django.contrib import admin
from rest_framework.authtoken.models import Token


class TokenAdmin(admin.ModelAdmin):
    list_display = ('key', 'user', 'created')
    fields = ('user',)
    ordering = ('-created',)


admin.site.register(Token, TokenAdmin)

To change this, you first need to unregister the old admin registration against the given class, and then register the new one.

Try this:

admin.site.unregister(Token) #First unregister the old class
admin.site.register(Token, AuthTokenAdmin) #Then register the new class