Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin - override already registered model

Tags:

python

django

I need to override django-eav app admin class. In eav/admin.py model is registered: admin.site.register(Value). I need to add list_display to this model. I know that it is bad practise to modify installed app code, so i need to override it. But, not sure how. In my own app/admin.py i have:

class EavValueAdmin(ModelAdmin):
    list_display = ('__unicode__', 'value_text', )

#unregistering class from eav.admin
admin.site.unregister(Value)
admin.site.register(Value, EavValueAdmin)

This gives me an error: NotRegistered: The model Value is not registered. If i try to comment this line: admin.site.unregister(Value), also error: AlreadyRegistered: The model Value is already registered. How can i overcome this problem?

like image 935
Mažas Avatar asked Mar 13 '23 02:03

Mažas


1 Answers

Django apps are loaded in the order they are listed in INSTALLED_APPS in your settings.py. Therefore your app must come after django-eav to be able to unregister it:

INSTALLED_APPS = [...
   'django-eav',
   ...
   'my_app',
]

Usually your apps must come after built-in and 3rd party apps. You have to test your project to see if everything works smoothly after changing the order.

like image 143
Selcuk Avatar answered Mar 15 '23 16:03

Selcuk