[1.5.x] Fixed #19547 -- Caching of related instances.

When &'ing or |'ing querysets, wrong values could be cached, and crashes
could happen.

Thanks Marc Tamlyn for figuring out the problem and writing the patch.

Backport of 07fbc6a.
[1.5.x] Minor fixes in the known_related_objects tests.

* Fixed JSON indentation.
* Avoided relying on implicit ordering.

Backport of a7b7efe.