I want to retry if exception occurs in Django Admin:
- When trying to add data:
- When trying to change data:
- When clicking on Delete on "Change" page:
Then clicking on Yes, I'm sure to try deleting data:
- When clicking on Go on "Select to change" page:
Then clicking on Yes, I'm sure to try deleting data:
I have Person model below:
# "store/models.py"
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=30)
And, Person admin below:
# "store/admin.py"
from django.contrib import admin
from .models import Person
@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
pass
So, how can I retry when exception occurs in Django Admin?






You can use
retry()below to retry Django Admin 0 or more times with 0 or more interval seconds:Then, using
@retry(4, 2)for the overridden changeform_view() can retry adding and changing data 4 times with 2 interval seconds in addition to a normal try each time exception occurs as shown below. *@retry(4, 2)must be the 1st decorator from the top to work properly if there are multiple decorators:So, let's raise OperationalError exception in the overridden response_add() and response_change() as shown below:
Now, if trying to add data:
Adding data is retried 4 times in addition to a normal try as shown below:
And, transaction is run 5 times (The light blue one is a normal try and the yellow ones are 4 retries) according to the PostgreSQL query logs below. *You can check how to log PostgreSQL queries:
Then finally, the page is redirected to "Select to change" page without adding data as shown below:
Next, if trying to change data from
JohntoDavid:Changing data is retried 4 times in addition to a normal try as shown below:
And, transaction is run 5 times (The light blue one is a normal try and the yellow ones are 4 retries) according to the PostgreSQL query logs below:
Then finally, the page is redirected to "Select to change" page without changing data as shown below:
Next, using
@retryfor the overridden delete_view() can retry deleting data 2 times with 1 interval second by default in addition to a normal try each time exception occurs as shown below. *@retrymust be the 1st decorator from the top if there are multiple decorators:So, let's raise
OperationalErrorexception in the overridden response_delete() as shown below:Now, if clicking on Delete on "Change" page:
Then clicking on Yes, I'm sure to try deleting data:
Deleting data is retried 2 times in addition to a normal try as shown below:
And, transaction is run 3 times (The light blue one is a normal try and the yellow ones are 2 retries) according to the PostgreSQL query logs below:
Then finally, the page is redirected to "Select to change" page without deleting data as shown below:
Finally, using
@retryfor the overridden delete_queryset() can retry deleting data 2 times with 1 interval second by default in addition to a normal try each time exception occurs as shown below. *By default, transaction is not used for Django Admin Actions and@retrymust be the 1st decorator from the top to work properly if there are multiple decorators:So, let's raise
OperationalErrorexception in the overriddendelete_queryset()as shown below:Now, if clicking on Go on "Change" page on "Select to change" page:
Then clicking on Yes, I'm sure to try deleting data:
Deleting data is retried 2 times in addition to a normal try as shown below:
And, transaction is run 3 times (The light blue one is a normal try and the yellow ones are 2 retries) according to the PostgreSQL query logs below:
Then finally, the page is redirected to "Select to change" page without deleting data but I don't know how to remove the default message "Successfully deleted 2 persons." as shown below:
In addition, if you want to retry 3 times with 0.5 interval seconds by default with
@retry, you need to changecount = 2tocount = 3andinterval=1tointerval=0.5respectively as shown below: