Yes, this will work as seen here:
# setup
model = nn.Sequential(
    nn.Linear(1, 1),
    nn.Linear(1, 1),
    nn.Linear(1, 1)
)

# freeze middle layer
for param in model[1].parameters():
    param.requires_grad = False

# calculcate gradients
model(torch.randn(1, 1)).backward()

# check gradients
for name, param in model.named_parameters():
    print(name, param.grad)

> 0.weight tensor([[-0.0005]])
  0.bias tensor([-0.0335])
  1.weight None
  1.bias None
  2.weight tensor([[-0.6302]])
  2.bias tensor([1.])