I understand your use case now. 
You can also do this:
fake = G(z)
for p in D.parameters():
  p.requires_grad = False
loss = criterion(D(fake))
loss.backward()
for p in D.paramters():
  p.requires_grad = True

And you can write a function instead of spelling out the two for loops.