When I use select_for_update() and update() of a queryset together as shown below:
# "store/views.py"
from django.db import transaction
from .models import Person
from django.http import HttpResponse
@transaction.atomic
def test(request):
# Here # Here
print(Person.objects.select_for_update().filter(id=1).update(name="Tom"))
return HttpResponse("Test")
Only UPDATE
query is run without SELECT FOR UPDATE
query as shown below. *I use PostgreSQL and these logs below are the queries of PostgreSQL and you can check on PostgreSQL, how to log queries with transaction queries such as "BEGIN" and "COMMIT":
But, when I use select_for_update()
and update()
of a queryset separately then put print(qs)
between them as shown below:
# "store/views.py"
from django.db import transaction
from .models import Person
from django.http import HttpResponse
@transaction.atomic
def test(request):
qs = Person.objects.select_for_update().filter(id=1)
print(qs) # Here
qs.update(name="Tom")
return HttpResponse("Test")
SELECT FOR UPDATE
and UPDATE
queries are run as shown below:
Actually, this example above occurs because QuerySets are lazy according to the Django documentation below:
QuerySets are lazy – the act of creating a QuerySet doesn’t involve any database activity. You can stack filters together all day long, and Django won’t actually run the query until the QuerySet is evaluated.
But, this is not simple for me. I just want normal database behavior.
Now, is there non-lazy mode or strict mode for querysets in Django?