The cause was assuming that an unordered queryset returns the values always in the same order. Backport of 33dd8f54