3

I basically have 2 external Django app that are overriding the UserAdmin model. Each one of these will first unregister the UserAdmin model and then register their own, something like this:

admin.site.unregister(get_user_model())
admin.site.register(get_user_model(), ExternalAppUserAdmin)

I also, in turn, override the UserAdmin:

admin.site.unregister(get_user_model())
admin.site.register(get_user_model(), MyAppUserAdmin)

The problem is that depending on the order of the INSTALLED_APPS in the settings file only the last app will actually override the UserAdmin.

Most of the times these overriding are just InlineModelAdmin inside the AdminUser, so what I've done so far is to import the external app's InlineModelAdmin models and insert them in my own admin.py. For example:

from relationships.admin import RelationshipInline
....
class BaseProfileAdminStacked( NestedModelAdmin, GuardedModelAdmin):
    inlines = [BaseProfileInline, RelationshipInline,]
....
admin.site.unregister(get_user_model())
admin.site.register(get_user_model(), BaseProfileAdminStacked)

But this seems a little hacky to me, because for example:

  • The first external app will unregister the UserAdmin, then register it again with its own customization
  • The second external app will do the same, actually overriding the first one
  • My own app should then import all the AdminInline from the external apps and include them in my own registration of the UserAdmin

Isn't this a waste of effort? Do you have a better idea to do so?

Leonardo
  • 4,046
  • 5
  • 44
  • 85

0 Answers0