Your loop might work, if you have nested modules, where the inner modules are the built-in layers.
If the model definition is more complicated (e.g. using mixed custom modules and layers), you might need to add conditions via if isinstance(module, nn.Conv2d) etc.