Deleting objects in django tastypie

前端 未结 2 863
青春惊慌失措
青春惊慌失措 2021-01-14 18:17

I have the following models:

class Poster(models.Model)
     user = models.OneToOneField(User, primary=True)
     userpicture = models.CharField(max_length =         


        
相关标签:
2条回答
  • 2021-01-14 19:05

    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
    
    0 讨论(0)
  • 2021-01-14 19:10

    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)
    
    0 讨论(0)
提交回复
热议问题