[1.7.x] Rewrote migration autodetector to involve actual computer science.

Fixes #22605, #22735; also lays the ground for some other fixes.

Conflicts:
	django/db/migrations/autodetector.py
[1.7.x] Implement Migration.run_before

This attribute (used for reverse dependencies) was previously declared
and mentioned in the code, but never actually used.
[1.7.x] Prevent operation pollution by later operations of fields
[1.7.x] Fixed #22773 -- Forced templatize() to return unicode

Backport of daaeb84158 from master.
[1.7.x] Fixed flake8 errors.

Backport of b5a54168f9 from master
[1.7.x] Added formatting and line wrapping to checks reference doc.

Backport of 5472bf4df0 from master