1. 程式人生 > >django中批量刪除資料的方法

django中批量刪除資料的方法

class TaskDeleteSelectView(View):
    def post(self, request):
        if not request.user.is_authenticated:
            # 判斷使用者登入狀態
            return HttpResponse('{"status":"fail", "msg":"使用者未登入"}', content_type='application/json')
        array = request.POST.getlist('ids')  # django接收陣列
        idstring = ','.join(array)
        try:
            Tasklist.objects.extra(where=['id IN (' + idstring + ')']).delete()
        except:
            return HttpResponse('{"status":"fail", "msg":"ID不存在"}', content_type='application/json')
        return HttpResponse('{"status":"success", "msg":"操作成功"}', content_type='application/json')

https://blog.csdn.net/liangpz521/article/details/8108793