Fixed #22650 -- Fixed regression on prefetch_related.

Thanks Aymeric Augustin for the report and tests. Refs #17001.
Fixed the ordering of prefetch lookups so that latter lookups can refer to former lookups.

Thanks Anssi Kääriäinen and Tim Graham for the reviews. Refs #17001 and #22650.