How to prevent external magnet rotate a motor
Two solutions that come to my mind:

Shielding around the motor.  Encase the entire motor, or its vulnerable side with a ferrous metal shield that will prevent outside interference.

Braking.  Add a mechanical brake to prevent movement when it's not desired.


A stepper motor can be left energized and will hold its position but DC motors don't offer this.