Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 603
dowjones123 Avatar asked Sep 21 '14 21:09

dowjones123


People also ask

What is admin register?

Administrative register is a Register used for administrative purposes in an administrative information system. An Administrative register should contain all objects to be administrated, the objects are identifiable, are updated, and the Variables in the Administrative register are used for administrative purposes.

What is the purpose of the admin site in a Django project?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.


1 Answers

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
like image 192
karthikr Avatar answered Sep 28 '22 02:09

karthikr