Fixed #23555 -- IndexError problem in QuerySet.first() and .last()