I have Person
model as shown below:
# "store/models.py"
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=30)
And, this is Person
admin below:
# "store/admin.py"
from django.contrib import admin
from .models import Person
@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
pass
Then, when clicking Go to go to delete the selected persons as shown below:
Then, clicking Yes I'm sure to delete the selected persons:
Only DELETE
query is run in transaction as shown below:
Now, how can I run SELECT FOR UPDATE
for the default "Delete selected" in Django Admin Actions?
You need to override delete_queryset() with
@transaction.atomic
as shown below to runSELECT FOR UPDATE
for the default "Delete selected" in Django Admin Actions. *print(qs)
is important to runSELECT FOR UPDATE
:Then, when clicking Go to go to delete the selected persons as shown below:
Then, clicking Yes I'm sure to delete the selected persons:
SELECT FOR UPDATE
query andDELETE
query are run in transaction as shown below:In addition, you can use raw queries as shown below if you want to run shorter queries:
Then, when clicking Go to go to delete the selected persons as shown below:
Then, clicking Yes I'm sure to delete the selected persons:
shorter
SELECT FOR UPDATE
query and shorterDELETE
query are run in transaction as shown below:In addition, you can check the default code of delete_queryset() which is not overridden as shown below: