Django中默认的“删除已选择”管理操作

时间:2009-10-14 11:58:06

标签: django django-admin

如何在Django管理面板中删除或更改默认管理操作“删除所选X项目”的详细名称?

8 个答案:

答案 0 :(得分:23)

除了Googol的解决方案,并等待delete_model()到be implemented in current Django version,我建议使用以下代码。

它仅禁用当前AdminForm的默认删除操作。

class FlowAdmin(admin.ModelAdmin):
    actions = ['delete_model']

    def get_actions(self, request):
        actions = super(MyModelAdmin, self).get_actions(request)
        del actions['delete_selected']
        return actions

    def delete_model(self, request, obj):
        for o in obj.all():
            o.delete()
    delete_model.short_description = 'Delete flow'

admin.site.register(Flow, FlowAdmin)

答案 1 :(得分:20)

您可以禁止使用此代码显示操作。

from django.contrib import admin
admin.site.disable_action('delete_selected')

如果您选择了,则可以使用以下方法在单个模型上恢复它:

class FooAdmin(admin.ModelAdmin):
    actions = ['my_action', 'my_other_action', admin.actions.delete_selected]

答案 2 :(得分:12)

不确定这种猴子修补是不是一个好主意,但是在我的admin.py之一中为我推断这个:

from django.contrib.admin.actions import delete_selected
delete_selected.short_description = u'How\'s this for a name?'

这将更改所有您的管理网站的详细名称。如果您只想为一个特定型号的管理员更改它,我认为您需要写一个custom admin action

使用Django 1.1版测试:

>>> import django
>>> django.VERSION
(1, 1, 0, 'beta', 1)

答案 3 :(得分:4)

答案 4 :(得分:3)

答案 5 :(得分:3)

对于全局更改,delete_selected的short_description Dominic Rodgeranswer似乎最好。

但是,如果要更改单个模型的管理员的short_description,我认为Stéphaneanswer的替代方案更好:

def get_actions(self, request):
    actions = super(MyModelAdmin, self).get_actions(request)
    actions['delete_selected'][0].short_description = "Delete Selected"
    return actions

答案 6 :(得分:0)

from django.contrib.admin import sites
from django.contrib.admin.actions import delete_selected


class AdminSite(sites.AdminSite):
    """
    Represents the administration, where only authorized users have access.
    """
    def __init__(self, *args, **kwargs):
        super(AdminSite, self).__init__(*args, **kwargs)
        self.disable_action('delete_selected')
        self.add_action(self._delete_selected, 'delete_selected')

    @staticmethod
    def _delete_selected(modeladmin, request, queryset):
        _delete_qs = queryset.delete

        def delete():
            for obj in queryset:
                modeladmin.delete_model(request, obj)
            _delete_qs()

        queryset.delete = delete
        return delete_selected(modeladmin, request, queryset)

答案 7 :(得分:0)

class FooAdmin(sites.AdminSite):
        not_deleted = ['value1', 'value2']
        actions = ['delete_selected_values']

    def delete_selected_values(self, request, queryset):
        # my custom logic
        exist = queryset.filter(value__in=self.not_deleted).exists()
        if exist:
            error_message = "Error"
            self.message_user(request, error_message, level=messages.ERROR)
        else:
            delete_action = super().get_action('delete_selected')[0]
            return delete_action(self, request, queryset)
    delete_selected_values.short_description = 'delete selected'

admin.site.register(Foo, FooAdmin)