使用额外参数覆盖django model.py的delete方法

时间:2011-01-28 07:37:57

标签: django-admin

我有一个带有删除方法的模型,它接受2个参数。我需要覆盖什么来确保我的自定义删除方法是从django admin调用的?我使用的版本仍然没有delete_model所以我无法使用它。

2 个答案:

答案 0 :(得分:0)

我最终做的是编写自己的自定义删除操作,该操作使用了我的自定义删除方法,而不是使用查询集的批量删除

class MyModelAdmin(admin.ModelAdmin):
    actions  = ['custom_delete_selected',]

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

    def custom_delete_selected(self, request, queryset):
        #Meant to cancel the default delete operation in admin page
        #so that the system uses the model's delete method instead of queryset.delete 
        for obj in queryset:
            obj.delete("param1", "param2")

        if queryset.count() == 1:
            message_bit = "1 item was"
        else:
            message_bit = "%s items were" % queryset.count()
        self.message_user(request, "%s successfully deleted." % message_bit)

    custom_delete_selected.short_description = "Delete selected items"

答案 1 :(得分:-1)

你需要使用Django 1.3,其中delete_model方法有3个参数