0

我是 Django 新手,我的 DeleteView 是这样写的:

class ObjectDelete(DeleteView):
    def post(self, request, *args, **kwargs):
        blah blah
        return super(ObjectDelete, self).post(request, *args, **kwargs)

我对如何删除该对象感到困惑,因为我object.delete在函数中的任何地方都没有看到。

4

1 回答 1

4

对象的实际删除是由 完成的,它DeletionMixin是使用的父级。django.views.generic.edit.BaseDeleteViewDeleteView

这是DeletionMixin

class DeletionMixin(object):
    """
    A mixin providing the ability to delete objects
    """
    success_url = None

    def delete(self, request, *args, **kwargs):
        """
        Calls the delete() method on the fetched object and then
        redirects to the success URL.
        """
        self.object = self.get_object()
        self.object.delete()
        return HttpResponseRedirect(self.get_success_url())

    # Add support for browsers which only accept GET and POST for now.
    def post(self, *args, **kwargs):
        return self.delete(*args, **kwargs)

    def get_success_url(self):
        if self.success_url:
            return self.success_url
        else:
            raise ImproperlyConfigured(
                "No URL to redirect to. Provide a success_url.")

它使用 获取要删除的对象get_object()

于 2012-10-09T09:35:47.297 回答