I have a view with atomic transaction, where one object is created then updated:
def buy(request):
with transaction.atomic():
# get request.user for update
user = User.objects.select_for_update().get(id=request.user.id)
user.order_count += 1
# get other stuff for update
items = Item.objects.select_for_update().filter(sold=False)
items.update(user=user)
# DO I NEED select_for_update SOMEWHERE HERE FOR NEXT UPDATE?
new_sell = Sell.objects.create(user=user)
# some_operations()
new_sell.some_field = something
new_sell.save()
HttpResponse('Done')
Is it safe to update newly created objects without select_for_update() in atomic transactions?
Or how do normal people implement select_for_update in such cases?
I guess, this will be 100% safe, but does not look like a good plan:
new_sell = Sell.objects.create(user=user)
sell = Sell.objects.select_for_update().get(id=new_sell.id)
# Now we can do everything with sell object