[1.5.x] Fixed #19607 - prefetch_related crash

Thanks to av@rdf.ru and flarno11@yahoo.de for the report.

Backport of 4fd94969d8 from master
[1.5.x] Optimisation in prefetch_related_objects

Backport of 17559e6eb0 from master