diff --git a/pony/orm/core.py b/pony/orm/core.py index b5555c27..0665b822 100644 --- a/pony/orm/core.py +++ b/pony/orm/core.py @@ -5946,6 +5946,19 @@ def first(query): if not objects: return None return objects[0] @cut_traceback + def last(query): + translator = query._translator + if translator.order: + pass + elif type(translator.expr_type) is tuple: + query = query.order_by(*[-i-1 for i in range(len(query._translator.expr_type))]) + else: + query = query.order_by(-1) + objects = query.without_distinct()[:1] + if not objects: + return None + return objects[0] + @cut_traceback def without_distinct(query): return query._clone(_distinct=False) @cut_traceback