Home > Mobile >  How to run "SELECT FOR UPDATE" instead of "SELECT" when changing and deleting da
How to run "SELECT FOR UPDATE" instead of "SELECT" when changing and deleting da

Time:12-15

I have the code below:

# "store/models.py"

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=30)
# "store/admin.py"

from django.contrib import admin
from .models import Person

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    pass

Then, when changing data as shown below:

enter image description here

SELECT is run instead of SELECT FOR UPDATE as shown below. *I use PostgreSQL and these logs below are the queries of PostgreSQL and you can check enter image description here

And, when clicking Delete button of Change person as shown below:

enter image description here

Then clicking Yes, I'm sure button to delete data as shown below:

enter image description here

SELECT is run instead of SELECT FOR UPDATE as shown below:

enter image description here

Now, I want to run SELECT FOR UPDATE instead of SELECT for both cases as shown above.

So, how can I do this?

CodePudding user response:

You need to override enter image description here

SELECT FOR UPDATE is run instead of SELECT as shown below:

enter image description here

And, when clicking Delete button of Change person as shown below:

enter image description here

Then clicking Yes, I'm sure button to delete data as shown below:

enter image description here

SELECT FOR UPDATE is run instead of SELECT as shown below:

enter image description here

CodePudding user response:

To run a SELECT FOR UPDATE query instead of a regular SELECT query in Django Admin, you can use the select_for_update() method on your queryset. This method will lock the rows in the queryset until the transaction is committed or rolled back, preventing other transactions from modifying the data in those rows.

  • Related