Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
Django: Is it necessary to call select_for_update for recently created object in atomic transaction?

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
about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda