Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all with latest timestamp in django?

This seems like it should be obvious but I can't get it to work.

I'm using django to query a simple table into which a bunch of data gets inserted periodically. I'd like to write a view that only pulls data with the latest timestamp, something like

select * from mytable
where event_time = (
    select max(event_time) from mytable);

What's the proper syntax for that?

like image 722
Patrick McCarthy Avatar asked Nov 22 '25 11:11

Patrick McCarthy


1 Answers

You can try with latest method. Here documentation

MyModel.objects.latest('event_time')

assuming event_time has a date value, this will return the latest object (the max date)

like image 158
Gocht Avatar answered Nov 25 '25 01:11

Gocht



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!