[1.7.x] Fixed several flake8 errors

Backport of d17a4cb037 from master
[1.7.x] Added database migration for contrib.sites.

refs #22170.

Backport of ec6e2eac60 from master
[1.7.x] Added database migration for contrib.flatpages.

refs #22170.

Backport of 7da16b99df from master
[1.7.x] Added database migration for contrib.redirects.

refs #22170.

Backport of d7576bb27a from master
[1.7.x] Fixed #22842

Backport of 7a1f8414c3b71b6af03e5be9f5f8db115551c410 from master.
[1.7.x] Fixed #22844: Duplicate SQL for SQLite FKs
[1.7.x] Fixed #22771 -- Fixed test.Client.logout when using custom auth backend.

Backport of 50b9313e0a from master
[1.7.x] Removed usaged of contrib.sessions as a placeholder in migration tests.

Without this, we're unable to add actual migrations for the app.

Backport of 808388c28c from master
[1.7.x] Added database migration for contrib.sessions.

refs #22170.

Backport of 37a8f5aeed from master
[1.7.x] Fixed #22788 -- Ensured custom migration operations can be written.

This inspects the migration operation, and if it is not in the
django.db.migrations module, it adds the relevant imports to the
migration writer and uses the correct class name.

Backport of bb39037fcb from master
[1.7.x] Use __first__ not __latest__ for unknown app dependencies
[1.7.x] Fixed #22848: Ignore no-migrations errors during makemigrations only
[1.7.x] Fixed #22783: Make sure swappable models come first in creation
[1.7.x] Fixed #22847: Optimizer wasn't expecting unresolved FKs
[1.7.x] Added database migration for contrib.contenttypes.

Moved contenttypes tests to allow them to run correctly in the presence of
migrations. refs #22170.

Backport of eb8600a656 from master
[1.7.x] Fixed flake8 error.

Backport of a96dbda6d3 from master
[1.7.x] Added database migration for contrib.auth.

refs #22170.

Backport of b341f33697 from master
[1.7.x] Fixed #22845 -- Correctly handled memcached default timeout value.

Backport of 4529af9ecf from master
[1.7.x] Ignore through being None on some special cases of fields.
[1.7.x] Ignore more blank throughs on swapped-out M2Ms