DELETEing a resource in tastypie

109 views Asked by At

I have a CommentResource model in which I want to ensure that a user can delete a comment only authored by him (by sending a DELETE reqeust). So I use follow the answer of this SO question.

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

But django gives me this error:

delete_detail() got an unexpected keyword argument 'pk'

1

There are 1 answers

0
Rahul Gupta On

To ensure that a user can delete a comment only authored by him (by sending a DELETE reqeust), you need to implement your authorization class like below.

from tastypie.authorization import Authorization
from tastypie.exceptions import Unauthorized

class MyAuthorization(Authorization)

    def delete_detail(self, object_list, bundle):
        """
        Returns True or false based on authorized after applying
        your logic. You can even raise an exception if unauthorized.
        """
        if authorized:
            return True
        else:
             #raise Unauthorized("Sorry, can't delete other user's comments.")
             return False