How does Django's Object-Relational Mapping (ORM) handle database migrations, and how can you optimize queries to avoid the N+1 select problem?