[1.7.x] Improved patching of sys.stdout in refs #23078.

Backport of 936fba5878 from master
[1.7.x] Silenced a Python 2 ImportWarning in a migrations test.

Backport of 66211b4b75 from master
[1.7.x] Corrected headings in migration docs.

Backport of e01fa71142 from master
[1.7.x] Silenced test warning for deprecated IPAddressField.

Backport of 99cd80dc74 from master
[1.7.x] Fixed #23094 -- Removed redundant argument in select_related() example.

Thanks thegeekofalltrades at gmail.com for the report.

Backport of 5f919b9c81 from master
[1.7.x] Fixed #23014 -- Infinite migration regression with unique/index_together.

Backport of 64e75c47ef from master
[1.7.x] Fixed #23089 -- Fixed transaction handling in two management commands.

Previously, when createcachetable and flush operated on non-default
databases, they weren't atomic.

Backport of 753a22a635 from master
[1.7.x] Avoided transactional DDL on castrated databases.

Fixed a test failure that appeared after 753a22a6, although the bug
existed before that commit.

Refs #22308 and #23089.

Backport of 0757e0f30d from master
[1.7.x] Avoided transactional truncates on amateur databases.

Fixed a test failure that appeared after 753a22a6, although the bug
existed before that commit.

Refs #22308.

Backport of 6877a9d4 from master