Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#21946 closed Uncategorized (worksforme)

Calling get_object() on DeleteView resets the success_url

Reported by: django@… Owned by: nobody
Component: Generic views Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I was trying to do something with the object in the def delete(self, request, *args, **kwargs) method for a DeleteView, but found that calling self.get_object() *after* calling through to super().delete(...) causes problems:

class MyDeleteView(DeleteView):
    success_url = "/finish/"

    def delete(self, request, *args, **kwargs):
        response = super(MyDeleteView, self).delete(request, *args, **kwargs)
        object = self.get_object()
        # ... do something with object (or not)
        return response

# urls.py:
...
url(r'^thing/delete/(?P<pk>\d+)/, views.MyDeleteView.as_view(), name="thing_delete")
...

I'd expect this code to work, and that upon returning response you are redirected to success_url (i.e. /finish/). Instead, you are redirected back to the delete page, so if my Thing has a pk=1, then I would be redirected to /thing/delete/1/

Now if I call get_object() *before* the call through to super everything works fine. It seems calling get_object() resets the success_url.

I'm not sure if what I'm trying to do is not normal behaviour, but I'd have thought that trying to do something once an object has successfully been delete (i.e. only *after* the call to super(...).delete(...) has been made) should be OK.
In this case, I'm trying to send an email saying "Successfully deleted {object}"

Change History (3)

comment:1 by django@…, 11 years ago

Note: perhaps the 'correct' way of retrieving the object is by using:

MyModel.objects.get(pk=kwargs['pk'])

but the get_object() method in this case is confusing, and still messes things up :/

comment:2 by Marc Tamlyn, 11 years ago

Resolution: worksforme
Status: newclosed

You can access self.object after you've called the super().

comment:3 by django@…, 11 years ago

...maybe, but if you call get_object() then it messes up. In this case, the get_object() call should probably raise some kind of exception if it's not supposed to be called *after* the call to super()

Note: See TracTickets for help on using tickets.
Back to Top