Search code examples
djangodjango-adminextend

Django admin action for third party model


How can I add actions to the admin interface of a third party app?

Example: I want to have a custom action for the model django.contrib.admin.Group.

With "action" I mean the batch action of the admin list view of a model.

Related docs: https://docs.djangoproject.com/en/1.7/ref/contrib/admin/actions/


Solution

  • Unregister the original model admin for Group model and then register it with your own ModelAdmin:

    from django.contrib.auth.admin import GroupAdmin
    from django.contrib.auth.models import Group    
    
    class MyGroupAdmin(GroupAdmin):
        actions = [...]
    
    admin.site.unregister(Group)
    admin.site.register(Group, MyGroupAdmin)
    

    UPDATE: If you want to add actions to the ModelAdmin from multiple apps then you have to directly access to the undocumented admin's registry:

    def some_action(modeladmin, request, queryset):
        pass
    
    admin.site._registry[Group].actions.append(some_action)