Many school districts have objectives regarding how students of different races, ethnicity or religious backgrounds should be distributed across schools. A growing literature in mechanism design is introducing school choice mechanisms that attempt to satisfy those requirements. We show that mechanisms based on the student-proposing deferred acceptance may fail to satisfy those objectives, but that by using instead the school-proposing deferred acceptance together with a choice function used by the schools, which incorporates a preference for satisfying them, can optimally approximate the diversity objectives while still satisfying an appropriate fairness criterion. We provide analytical results which show that the proposed mechanism has a general ability to satisfy those objectives, as opposed to some currently proposed mechanisms, which may yield segregated assignments.