Replies: 1 comment 1 reply
-
That might be a bug :) I always prepend primary key ordering in that case: expr = self.build_select_expression(
limit=1,
order_bys=[
OrderAction(
order_str=f"{self.model.Meta.pkname}",
model_cls=self.model_cls,
)
]
+ self.order_bys,
) I guess that should happen only if you do not provide any ordering for the main model. |
Beta Was this translation helpful? Give feedback.
-
Hi!
I'm trying to get the latest item from a table, wrt. a datetime-field, and I was a little surprised to learn how the
first
method works.What I expected
I've used Django a bit, so I was expecting .first() to get the first item in my queryset, with respect to existing constaints. I had already ordered the queryset the way
I wanted, so I was expecting this to work:
When that didn't work, I thought I might try to limit the size of the queryset myself, then use
.first()
as a shortcut over.all()
since this will raiseNoMatch
etc. when the object doesn't exist:Question
Is it correct that
.first()
disregards the queryset it's being called on and just returns the first item, sorted by pk? Why is this? Am I missing alternative syntax for a Django-style.first()
method?This is what I ended up with:
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions