Select For Update

Transaction

In [1]: from django.db import transaction

In [2]: from account.models import Profile

In [6]: with transaction.atomic():
   ...:     Profile.objects.select_for_update().get(uid='dUKzzQvx7nuGHQszA8xdkC')

Select_for_update

  • filter()

    try:
        profile = Profile.objects.select_for_update().filter(uid=uid)[0]
    except IndexError:
        return response(ProfileStatusCode.Profile_NOT_FOUND)
    
  • get()

    try:
        profile = Profile.objects.select_for_update().get(uid=uid)
    except Profile.DoesNotExist:
        return response(ProfileStatusCode.Profile_NOT_FOUND)
    
  • create()

    profile = Profile.objects.select_for_update().create(uid=uid)
    
  • get_or_create()

    profile, created = Profile.objects.select_for_update().get_or_create(uid=uid)
    
    • Tips: Kwargs for get_or_create should be unique. Use unique/unique_together

References

[1] Blog@NOAH KANTROWITZ, SELECT FOR UPDATE in Django

[2] Indradhanush Gupta@StackOverflow, How to use select_for_update to 'get' a Query in Django?

[3] Django@Github, django/tests/select_for_update/tests.py

results matching ""

    No results matching ""