This is an excellent question, and in fact one that plagues many small satellite designs! There are a number of means that are typically done to adjust for the effect of a magnetorquer/ magnetometer reading issues.
- Don't use the magnetometer while the magnetorquer is being used. If you don't require a 100% duty cycle of the magnetorquers, this can be the simplest solution.
- Bias out the magnetorquerBias out the magnetorquer by measuring it's effect on the ground, and using math to remove the effect. This isn't as perfect, but is useful in particular with permanent magnets.
- Put the magnetometer as far away from the main body of the spacecraft as possible. This is often done by sticking it on the end of a boom, sometimes shared by the antenna, other times as a stand alone boon. This is usually used in conjunction with the other items.
- Use more than one, and figure out the correct reading.
- Place the magnetometer outside of the Faraday cage of the satellite. This will prevent the inner electronics from affecting things. This won't protect against permanent magnets, as blocking magnetism is really hard, but it can protect against some spurious readings created by electric fields, to a degree.
In practice, a combination of these is typically done. The best bet is to place it outside of the spacecraft as far away from any magnetic materials as you can do, and test it in a number of orientations on the ground to try and determine the truth from whatever it is reading.