[Answered ]-Atomic transaction in django

1👍

Since this is a single update, you don’t need to make this atomic, you can just work with an .update(…) query [Django-doc]:

@api_view(['POST'])
def employee_increase(request):
    logger.info(f"Increase sallary: {request.data}")

    serializer = IdSerializer(data=request.data)
    serializer.is_valid(raise_exception=True)

    result = Employee.objects.filter(pk=request.data['id']).update(
        sallary=F('sallary') * (1 + int(request.data['increase_percent']) / 100)
    )
    if result:
        # updated (at least) one record
        pass

Leave a comment