Django admin action for third party model

若如初见. 提交于 2020-01-24 10:56:07

问题


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/


回答1:


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)


来源:https://stackoverflow.com/questions/29233962/django-admin-action-for-third-party-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!