I'm working on a Django/Wagtail project. I have a very customized feature in the admin view what will delete an object when hitting Save if certain conditions are met.
I have this in my class (that is a snippet):
def save(self, *args, **kwargs):
if condition:
self.delete()
else:
return super(MyModel, self).save(*args, **kwargs)
This works as expected. The object gets deleted, but after this I get an error page because the browser remains in the same URL /snippets/myapp/mymodel/1234/ but the object doesn't exist anymore.
I thought of two possible solutions for this.
Possible solution 1:
def save(self, *args, **kwargs):
if condition:
self.delete()
#### ===== > Redirect to objects list view
else:
return super(MyModel, self).save(*args, **kwargs)
Possible solution 2:
def save(self, *args, **kwargs):
if condition:
#### ===== > Don't delete and redirect to delete view (.../mymodel/1234/delete/)
else:
return super(MyModel, self).save(*args, **kwargs)
These are the two possible solutions I thought, but I don't know how to do any of them, even after reading docs here and there.
How can I do what I'm trying to achieve?