开发者

Overriding django model.py's delete method with extra arguments

I have a Model with a delete method that accepts 2 arguments. What do I need to override to make sure my custom delete method is what is called from django admin? The version am using still doesn't have delete_model开发者_JS百科 so I cant use that.


What I ended up doing was writing my own custom delete action that made use of my custom delete method instead of using the queryset's bulk delete

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"


you need to use Django 1.3 which has a delete_model method with 3 arguments

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜