400mg is really the bare bones minimum. The best way to find the right dose of magnesium for you is to titrate up until you experience a loose stool. Then drop it 50-100mg below whatever that dosage was. A teaspoon of magnesium sulfate (epsom salt) dissolved in water (followed by plenty of pure water afterwards) is a great way to quickly and safely up your magnesium levels to get things moving. Also, be sure you are getting your supplement in either a citrate or glycinate form, because magnesium oxide is junk.
However, there could be many additional reasons you are experiencing constipation. Do you get enough fiber and water in your diet? Enough probiotics? Psyllium husks with plenty of water is also a great way to move things along. Organic papaya is another one.
Vitamin D cannot cause constipation, as it is a laxative factor. Besides the deficiencies of Vitamin D and Magnesium, you may be having deficiencies of other laxative factors like Potassium, Sodium, Iodine, Manganese, Phosphate, Zinc, Chromium, Vitamin B1 (Thiamine), Vitamin C, etc.
You should avoid foods rich in CITRIC ACID, toothpastes containing FLUORIDE and taking EXCESSIVE Exercise, as they tend to cause constipation. Eating LESS food or FASTING also causes constipation.