Deleting objects in django tastypie

我与影子孤独终老i 提交于 2019-12-01 09:22:56

This is best enforced with Authorization.

You need to implement the delete_detail method to return True or False, for example:

def delete_detail(self, object_list, bundle):
    return bundle.obj.user == bundle.request.user

As explained in the tastyie cookbook. Maybe you can do something like this:

class DeleteComment(ModelResource):

    def obj_delete(self, bundle, **kwargs):
         # get post id
         comment = Comment.objects.get(pk=bundle.data.id) # or or whatever way you can get the id
         # delete all comments with that post id
         Comment.objects.filter(post=comment.post).delete()
         return super(DeleteComment, self).obj_delete(bundle, user=bundle.request.user)

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