Added support to fetch latest object from mongoengine queryset
Solving: https://github.com/MongoEngine/mongoengine/issues/2756
How come you are approving your own PRs?
I'm not sure that it will work good in case of sort that already created on the queryset recommended to add tester to that case
I'm not sure about the use case for such method but given that it's present in django, it's something that I'm ok to consider.
That being said, I see multiple issues with this proposal
-
in case no order_by is present, existing
firstmethod will rely on no-sorting (i.e natural sorting). Iflastwould rely on primary key sorting silently, it would mean that you could get inconsistencies (e.g get the same record) if you use first() and last(). -
if an existing sorting is specified on the queryset, it should be re-used and not overwritten by the primary key ordering.
Fixing 2 can be easily done in this PR but if we want to tackle 1) correctly, we would have to add the same default ordering on first which I'm not necessarily keen to do...
What would be acceptable (and less disruptive) to do is the following:
- add
lastbut solely rely on the existing order_by (no letting it take any argument) - if no order_by is set on the queryset, raise an error to avoid any inconsistent behavior